Skip to content

Commit

Permalink
fix: avoid race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
0x416e746f6e committed Sep 9, 2024
1 parent adc0c69 commit 725c6f9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
26 changes: 17 additions & 9 deletions bridge/events_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func (s *Server) eventBridgeDeactivated(ctx context.Context, _ *event.BridgeDeac
l.Info("Bridge deactivated")

if r := s.cfg.Reconcile.BridgeActivate.Reapply; r.Enabled() {
ba := s.reapply.bridgeActivate
ba.Count = 0
ba.Next = time.Time{} // disable re-activations of an inactive bridge
reapply := s.reapply.bridgeActivate
reapply.Count = 0
reapply.Next = time.Time{} // disable re-activations of an inactive bridge
}
}

Expand All @@ -98,24 +98,32 @@ func (s *Server) eventBridgeActivated(ctx context.Context, e *event.BridgeActiva
s.reconciler.BridgeActivate(ctx, e, failureSink)

if r := s.cfg.Reconcile.BridgeActivate.Reapply; r.Enabled() {
ba := s.reapply.bridgeActivate
ba.Count = 0
ba.Next = e.Timestamp.Add(r.InitialDelay)
reapply := s.reapply.bridgeActivate
reapply.Count = 0
reapply.Next = e.Timestamp.Add(r.InitialDelay)
}
}

func (s *Server) eventBridgeReactivated(ctx context.Context, e *event.BridgeReactivated, failureSink chan<- error) {
l := logutils.LoggerFromContext(ctx)

s.mxStatus.Lock()
if !s.status.Active {
l.Info("Skipping bridge reactivation since it's already inactive by now")
s.mxStatus.Unlock()
return
}
defer s.mxStatus.Unlock()

l.Info("Bridge reactivating...",
zap.Int("iteration", e.Iteration),
)

s.reconciler.BridgeActivate(ctx, e, failureSink)

if r := s.cfg.Reconcile.BridgeActivate.Reapply; r.Enabled() {
ba := s.reapply.bridgeActivate
ba.Count++
ba.Next = e.Timestamp.Add(r.DelayOnIteration(ba.Count))
reapply := s.reapply.bridgeActivate
reapply.Count++
reapply.Next = e.Timestamp.Add(r.DelayOnIteration(reapply.Count))
}
}
12 changes: 6 additions & 6 deletions bridge/events_tunnel_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ func (s *Server) eventTunnelInterfaceActivated(ctx context.Context, e *event.Tun
s.reconciler.InterfaceActivate(ctx, e, failureSink)

if r := s.cfg.Reconcile.InterfaceActivate.Reapply; r.Enabled() {
ia := s.reapply.interfaceActivate
ia.Count = 0
ia.Next = e.Timestamp.Add(r.InitialDelay)
reapply := s.reapply.interfaceActivate
reapply.Count = 0
reapply.Next = e.Timestamp.Add(r.InitialDelay)
}
}

Expand All @@ -167,8 +167,8 @@ func (s *Server) eventTunnelInterfaceReactivated(ctx context.Context, e *event.T
s.reconciler.InterfaceActivate(ctx, e, failureSink)

if r := s.cfg.Reconcile.InterfaceActivate.Reapply; r.Enabled() {
ia := s.reapply.interfaceActivate
ia.Count++
ia.Next = e.Timestamp.Add(r.DelayOnIteration(ia.Count))
reapply := s.reapply.interfaceActivate
reapply.Count++
reapply.Next = e.Timestamp.Add(r.DelayOnIteration(reapply.Count))
}
}
34 changes: 18 additions & 16 deletions bridge/reapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,33 @@ import (
)

func (s *Server) reapplyUpdates(_ context.Context, _ chan<- error) {
if ba := s.reapply.bridgeActivate; ba != nil {
if !ba.Next.IsZero() && time.Now().After(ba.Next) {
ba.Next = time.Time{} // avoid re-fire
s.mxStatus.Lock()
defer s.mxStatus.Unlock()

s.events <- &event.BridgeReactivated{ // emit event
BridgeInterface: s.cfg.BridgeInterface,
BridgePeerCIDRs: s.cfg.BridgePeerCIDRs(),
Iteration: ba.Count,
Timestamp: time.Now(),
if reapply := s.reapply.bridgeActivate; reapply != nil {
if !reapply.Next.IsZero() && time.Now().After(reapply.Next) {
if s.status.Active {
reapply.Next = time.Time{} // avoid re-fire

s.events <- &event.BridgeReactivated{ // emit event
BridgeInterface: s.cfg.BridgeInterface,
BridgePeerCIDRs: s.cfg.BridgePeerCIDRs(),
Iteration: reapply.Count,
Timestamp: time.Now(),
}
}
}
}

if ia := s.reapply.interfaceActivate; ia != nil {
if !ia.Next.IsZero() && time.Now().After(ia.Next) {
ia.Next = time.Time{} // avoid re-fire

s.mxStatus.Lock()
defer s.mxStatus.Unlock()

if reapply := s.reapply.interfaceActivate; reapply != nil {
if !reapply.Next.IsZero() && time.Now().After(reapply.Next) {
if activeInterface := s.status.ActiveInterface(); activeInterface != "" {
reapply.Next = time.Time{} // avoid re-fire

s.events <- &event.TunnelInterfaceReactivated{ // emit event
BridgeInterface: s.cfg.BridgeInterface,
BridgePeerCIDRs: s.cfg.BridgePeerCIDRs(),
Iteration: ia.Count,
Iteration: reapply.Count,
TunnelInterface: activeInterface,
Timestamp: time.Now(),
}
Expand Down

0 comments on commit 725c6f9

Please sign in to comment.