Skip to content

Commit 8a34e8e

Browse files
authored
ateredis: wrap the SUBSCRIBE confirmation wait with a timeout (#407)
Should have a timeout to make the fix in #353 non-blocking. Originated from #379 - [x] Tests pass - [x] Appropriate changes to documentation are included in the PR
1 parent b66b5c2 commit 8a34e8e

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

cmd/ateapi/internal/store/ateredis/ateredis.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ func unmarshalWorkerEvent(payload string) (store.WorkerEvent, error) {
240240

241241
const workerPubSubChannel = "worker-changes"
242242

243+
// subscribeConfirmTimeout bounds WatchWorkers' wait for the SUBSCRIBE
244+
// confirmation.
245+
const subscribeConfirmTimeout = 5 * time.Second
246+
243247
func (s *Persistence) publishWorkerEvent(ctx context.Context, eventType store.WorkerEventType, worker *ateapipb.Worker) {
244248
payload, err := marshalWorkerEvent(eventType, worker)
245249
if err != nil {
@@ -259,7 +263,9 @@ func (s *Persistence) WatchWorkers(ctx context.Context) (*store.WorkerWatch, err
259263
// Subscribe sends the SUBSCRIBE command asynchronously; wait for the
260264
// confirmation reply so that events published after WatchWorkers returns
261265
// are guaranteed to be delivered to this subscription.
262-
if _, err := pubsub.Receive(watchCtx); err != nil {
266+
receiveCtx, receiveCancel := context.WithTimeout(watchCtx, subscribeConfirmTimeout)
267+
defer receiveCancel()
268+
if _, err := pubsub.Receive(receiveCtx); err != nil {
263269
pubsub.Close()
264270
cancel()
265271
return nil, fmt.Errorf("while confirming worker subscription: %w", err)

0 commit comments

Comments
 (0)