Update comments

This commit is contained in:
Neil Alexander 2022-02-02 12:25:04 +00:00
parent 667fcf2834
commit 36a718aa11
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -33,24 +33,24 @@ func JetStreamConsumer(
} }
go func() { go func() {
for { for {
// For reasons that don't make any sense to me, the nats.Context is // The context behaviour here is surprising — we supply a context
// actually not completely limiting. We can't supply context.Background // so that we can interrupt the fetch if we want, but NATS will still
// because that is not allowed. If we supply a context without a deadline // enforce its own deadline (roughly 5 seconds by default). Therefore
// then it will just enforce the JetStream timeout of 5 seconds. So it // it is our responsibility to check whether our context expired or
// is quite likely that we will end up looping here at that timeout. // not when a context error is returned. Footguns. Footguns everywhere.
msgs, err := sub.Fetch(1, nats.Context(ctx)) msgs, err := sub.Fetch(1, nats.Context(ctx))
if err != nil { if err != nil {
if err == context.Canceled || err == context.DeadlineExceeded { if err == context.Canceled || err == context.DeadlineExceeded {
// Work out whether it was the JetStream context that // Work out whether it was the JetStream context that expired
// expired, or whether it was the supplied context. // or whether it was our supplied context.
select { select {
case <-ctx.Done(): case <-ctx.Done():
// The supplied context expired, so we want to stop the // The supplied context expired, so we want to stop the
// consumer altogether. // consumer altogether.
return return
default: default:
// The JetStream context expired, so the fetch just timed // The JetStream context expired, so the fetch probably
// out and we should try again. // just timed out and we should try again.
continue continue
} }
} else { } else {