MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / Subscribe

Method Subscribe

pkg/natsconn/natsconn.go:101–120  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

99// The subscription is automatically removed when ctx is cancelled.
100// Nil-receiver safe: returns a closed channel.
101func (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
122func (rc *ReloadableConnection) unsubscribe(ch chan struct{}) {
123 rc.mu.Lock()

Callers 8

TestBroadcastNonBlockingFunction · 0.95
TestNilReceiverSafetyFunction · 0.95
NewFunction · 0.80
NewFunction · 0.80
NewAuditLogPublisherFunction · 0.80

Calls 1

unsubscribeMethod · 0.95

Tested by 5

TestBroadcastNonBlockingFunction · 0.76
TestNilReceiverSafetyFunction · 0.76