Subscribe registers for reconnection notifications. The returned channel receives a signal each time the NATS connection is re-established. The subscription is automatically removed when ctx is cancelled. Nil-receiver safe: returns a closed channel.
(ctx context.Context)
| 99 | // The subscription is automatically removed when ctx is cancelled. |
| 100 | // Nil-receiver safe: returns a closed channel. |
| 101 | func (rc *ReloadableConnection) Subscribe(ctx context.Context) <-chan struct{} { |
| 102 | if rc == nil { |
| 103 | ch := make(chan struct{}) |
| 104 | close(ch) |
| 105 | return ch |
| 106 | } |
| 107 | |
| 108 | ch := make(chan struct{}, 1) |
| 109 | |
| 110 | rc.mu.Lock() |
| 111 | rc.subscribers = append(rc.subscribers, ch) |
| 112 | rc.mu.Unlock() |
| 113 | |
| 114 | go func() { |
| 115 | <-ctx.Done() |
| 116 | rc.unsubscribe(ch) |
| 117 | }() |
| 118 | |
| 119 | return ch |
| 120 | } |
| 121 | |
| 122 | func (rc *ReloadableConnection) unsubscribe(ch chan struct{}) { |
| 123 | rc.mu.Lock() |