(ctx context.Context,
logger slog.Logger,
id uuid.UUID,
store database.Store,
ps pubsub.Pubsub,
bindings <-chan binding,
startWorkers <-chan struct{},
)
| 506 | } |
| 507 | |
| 508 | func newBinder(ctx context.Context, |
| 509 | logger slog.Logger, |
| 510 | id uuid.UUID, |
| 511 | store database.Store, |
| 512 | ps pubsub.Pubsub, |
| 513 | bindings <-chan binding, |
| 514 | startWorkers <-chan struct{}, |
| 515 | ) *binder { |
| 516 | b := &binder{ |
| 517 | ctx: ctx, |
| 518 | logger: logger, |
| 519 | coordinatorID: id, |
| 520 | store: store, |
| 521 | pubsub: ps, |
| 522 | bindings: bindings, |
| 523 | latest: make(map[bKey]binding), |
| 524 | workQ: newWorkQ[bKey](ctx), |
| 525 | close: make(chan struct{}), |
| 526 | } |
| 527 | go b.handleBindings() |
| 528 | // add to the waitgroup immediately to avoid any races waiting for it before |
| 529 | // the workers start. |
| 530 | b.workerWG.Add(numBinderWorkers) |
| 531 | go func() { |
| 532 | <-startWorkers |
| 533 | for i := 0; i < numBinderWorkers; i++ { |
| 534 | go b.worker() |
| 535 | } |
| 536 | }() |
| 537 | |
| 538 | go func() { |
| 539 | defer close(b.close) |
| 540 | <-b.ctx.Done() |
| 541 | b.logger.Debug(b.ctx, "binder exiting, waiting for workers") |
| 542 | |
| 543 | b.workerWG.Wait() |
| 544 | |
| 545 | b.logger.Debug(b.ctx, "updating peers to lost") |
| 546 | |
| 547 | ctx, cancel := context.WithTimeout(dbauthz.As(context.Background(), pgCoordSubject), time.Second*15) |
| 548 | defer cancel() |
| 549 | peerIDs, err := b.store.UpdateTailnetPeerStatusByCoordinator(ctx, database.UpdateTailnetPeerStatusByCoordinatorParams{ |
| 550 | CoordinatorID: b.coordinatorID, |
| 551 | Status: database.TailnetStatusLost, |
| 552 | }) |
| 553 | if err != nil { |
| 554 | b.logger.Error(b.ctx, "update peer status to lost", slog.Error(err)) |
| 555 | } |
| 556 | for _, peerID := range peerIDs { |
| 557 | publishPeerUpdate(ctx, b.pubsub, b.logger, peerID) |
| 558 | } |
| 559 | }() |
| 560 | return b |
| 561 | } |
| 562 | |
| 563 | func (b *binder) handleBindings() { |
| 564 | for { |
no test coverage detected