Skip to content

Commit

Permalink
jetstream: support async acknowledgement in subscriber (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexCuse authored Mar 22, 2024
1 parent 69da773 commit 423b241
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/jetstream/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type SubscriberConfig struct {

// ConsumeOptions is the option that adjusts consume behavior
ConsumeOptions []natsJS.PullConsumeOpt

// AckAsync enables asynchronous acknowledgement
AckAsync bool
}

// setDefaults sets default values needed for a subscriber if unset
Expand Down
7 changes: 5 additions & 2 deletions pkg/jetstream/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ func (s *Subscriber) handleMsg(ctx context.Context, msg jetstream.Msg, output ch

select {
case <-m.Acked():
// TODO: enable async ack via config
err = msg.DoubleAck(ctx)
if s.ackAsync {
err = msg.Ack()
} else {
err = msg.DoubleAck(ctx)
}

if err != nil {
s.logger.Error("Cannot send ack", err, messageLogFields)
Expand Down
2 changes: 2 additions & 0 deletions pkg/jetstream/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Subscriber struct {
configureStream StreamConfigurator
configureConsumer ConsumerConfigurator
consumeOptions []jetstream.PullConsumeOpt
ackAsync bool
}

// NewSubscriber creates a new watermill JetStream subscriber.
Expand Down Expand Up @@ -72,6 +73,7 @@ func newSubscriber(nc *nats.Conn, config *SubscriberConfig) (*Subscriber, error)
configureStream: config.ConfigureStream,
configureConsumer: config.ConfigureConsumer,
consumeOptions: config.ConsumeOptions,
ackAsync: config.AckAsync,
}, nil
}

Expand Down

0 comments on commit 423b241

Please sign in to comment.