Skip to content

Commit

Permalink
Fix QueueSubscribeSync and backoff errors (#146)
Browse files Browse the repository at this point in the history
Core NATS subscriptions were incorrectly using `SubscribeSync` instead
of `QueueSubscribeSync` and both core and JetStream subscriptions were
waiting for the backoff period (default 5 seconds) for all iterations of
the worker loop, not just error conditions. This PR fixes both issues.

---------

Signed-off-by: John Schaeffer <jschaeffer@equinix.com>
  • Loading branch information
jnschaeffer authored Aug 30, 2023
1 parent f294a5b commit f271e55
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions events/nats_subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (c *NATSConnection) coreSubscribe(ctx context.Context, subject string) (<-c
"nats.subject", subject,
)

sub, err := c.conn.SubscribeSync(subject)
sub, err := c.conn.QueueSubscribeSync(subject, NATSConsumerDurableName(c.cfg.QueueGroup, subject))
if err != nil {
return nil, err
}
Expand All @@ -24,10 +24,12 @@ func (c *NATSConnection) coreSubscribe(ctx context.Context, subject string) (<-c
go func() {
for {
if err := c.nextMessage(ctx, sub, msgCh); err != nil {
if !errors.Is(err, context.DeadlineExceeded) {
logger.Errorw("error fetching messages", "error", err)
if errors.Is(err, context.DeadlineExceeded) {
continue
}

logger.Errorw("error fetching messages", "error", err)

select {
case <-ctx.Done():
case <-time.After(c.cfg.SubscriberFetchBackoff):
Expand Down Expand Up @@ -70,10 +72,12 @@ func (c *NATSConnection) jsSubscribe(ctx context.Context, subject string) (<-cha
go func() {
for {
if err := c.fetchMessages(ctx, sub, msgCh); err != nil {
if !errors.Is(err, context.DeadlineExceeded) {
logger.Errorw("error fetching messages", "error", err)
if errors.Is(err, context.DeadlineExceeded) {
continue
}

logger.Errorw("error fetching messages", "error", err)

select {
case <-ctx.Done():
case <-time.After(c.cfg.SubscriberFetchBackoff):
Expand Down

0 comments on commit f271e55

Please sign in to comment.