Skip to content

fix: avoid deleting peers on graceful close #14165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 14, 2024
Prev Previous commit
Next Next commit
delete peers once
  • Loading branch information
sreya committed Aug 14, 2024
commit 4754554056d414ddc43cbcbf24e3b690a8cf0005
33 changes: 22 additions & 11 deletions enterprise/tailnet/pgcoord.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ type binder struct {
workQ *workQ[bKey]

workerWG sync.WaitGroup
close chan struct{}
}

func newBinder(ctx context.Context,
Expand Down Expand Up @@ -503,6 +504,26 @@ func newBinder(ctx context.Context,
go b.worker()
}
}()

go func() {
defer close(b.close)
<-ctx.Done()
b.logger.Debug(b.ctx, "binder exiting, waiting for workers")

b.workerWG.Wait()

b.logger.Debug(b.ctx, "updating peers to lost")

ctx, cancel := context.WithTimeout(ctx, time.Second*15)
defer cancel()
err := b.store.UpdateTailnetPeerStatusByCoordinator(ctx, database.UpdateTailnetPeerStatusByCoordinatorParams{
CoordinatorID: b.coordinatorID,
Status: database.TailnetStatusLost,
})
if err != nil {
b.logger.Error(b.ctx, "update peer status to lost", slog.Error(err))
}
}()
return b
}

Expand Down Expand Up @@ -623,17 +644,7 @@ func (b *binder) retrieveBinding(bk bKey) binding {
}

func (b *binder) wait() {
b.workerWG.Wait()

b.logger.Debug(b.ctx, "binder exiting, updating peers to lost", slog.Error(b.ctx.Err()))

err := b.store.UpdateTailnetPeerStatusByCoordinator(context.Background(), database.UpdateTailnetPeerStatusByCoordinatorParams{
CoordinatorID: b.coordinatorID,
Status: database.TailnetStatusLost,
})
if err != nil {
b.logger.Error(b.ctx, "update peer status to lost", slog.Error(err))
}
<-b.close
}

// mapper tracks data sent to a peer, and sends updates based on changes read from the database.
Expand Down
Loading