Skip to content

Commit 35e2a4e

Browse files
committed
fix: use defer cancel to cancel context waiting for state changes
1 parent 1debd99 commit 35e2a4e

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

pkg/controller/registry/grpc/source.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,27 +142,30 @@ func (s *SourceStore) watch(ctx context.Context, key resolver.CatalogKey, source
142142
case <-ctx.Done():
143143
return
144144
default:
145-
timer, _ := context.WithTimeout(ctx, s.stateTimeout(state))
146-
if source.Conn.WaitForStateChange(timer, state) {
147-
newState := source.Conn.GetState()
148-
state = newState
149-
150-
// update connection state
151-
src := s.Get(key)
152-
if src == nil {
153-
// source was removed, cleanup this goroutine
154-
return
145+
func() {
146+
timer, cancel := context.WithTimeout(ctx, s.stateTimeout(state))
147+
defer cancel()
148+
if source.Conn.WaitForStateChange(timer, state) {
149+
newState := source.Conn.GetState()
150+
state = newState
151+
152+
// update connection state
153+
src := s.Get(key)
154+
if src == nil {
155+
// source was removed, cleanup this goroutine
156+
return
157+
}
158+
159+
src.LastConnect = metav1.Now()
160+
src.ConnectionState = newState
161+
s.sourcesLock.Lock()
162+
s.sources[key] = *src
163+
s.sourcesLock.Unlock()
164+
165+
// notify subscriber
166+
s.notify <- SourceState{Key: key, State: newState}
155167
}
156-
157-
src.LastConnect = metav1.Now()
158-
src.ConnectionState = newState
159-
s.sourcesLock.Lock()
160-
s.sources[key] = *src
161-
s.sourcesLock.Unlock()
162-
163-
// notify subscriber
164-
s.notify <- SourceState{Key: key, State: newState}
165-
}
168+
}()
166169
}
167170
}
168171
}

0 commit comments

Comments
 (0)