Subscribe registers a callback that will receive resource update events. The same callback function will not be invoked concurrently. The callback function is invoked under a lock and must not call the controller.
(ctx context.Context, fn SubscribeCallback)
| 462 | // The same callback function will not be invoked concurrently. |
| 463 | // The callback function is invoked under a lock and must not call the controller. |
| 464 | func (c *Controller) Subscribe(ctx context.Context, fn SubscribeCallback) error { |
| 465 | if err := c.checkRunning(); err != nil { |
| 466 | return err |
| 467 | } |
| 468 | |
| 469 | c.mu.Lock() |
| 470 | id := c.nextSubscriberID |
| 471 | c.nextSubscriberID++ |
| 472 | c.subscribers[id] = fn |
| 473 | c.mu.Unlock() |
| 474 | |
| 475 | defer func() { |
| 476 | c.mu.Lock() |
| 477 | delete(c.subscribers, id) |
| 478 | c.mu.Unlock() |
| 479 | }() |
| 480 | |
| 481 | for { |
| 482 | select { |
| 483 | case <-c.closedCh: |
| 484 | return ErrControllerClosed |
| 485 | case <-ctx.Done(): |
| 486 | return ctx.Err() |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // Create creates a resource and enqueues it for reconciliation. |
| 492 | // If a resource with the same name is currently being deleted, the deletion will be cancelled. |
nothing calls this directly
no test coverage detected