Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consumer: close gracefully #166

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions consume.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rabbitmq

import (
"context"
"errors"
"fmt"
"sync"
Expand Down Expand Up @@ -32,6 +33,7 @@ type Consumer struct {
reconnectErrCh <-chan error
closeConnectionToManagerCh chan<- struct{}
options ConsumerOptions
handlerMux *sync.RWMutex

isClosedMux *sync.RWMutex
isClosed bool
Expand Down Expand Up @@ -89,6 +91,14 @@ func (consumer *Consumer) Run(handler Handler) error {
return err
}

handler = func(d Delivery) (action Action) {
if !consumer.handlerMux.TryRLock() {
return NackRequeue
}
defer consumer.handlerMux.RUnlock()
return handler(d)
}

for err := range consumer.reconnectErrCh {
consumer.options.Logger.Infof("successful consumer recovery from: %v", err)
err = consumer.startGoroutines(
Expand All @@ -104,10 +114,26 @@ func (consumer *Consumer) Run(handler Handler) error {
}

// Close cleans up resources and closes the consumer.
// It waits for handler to finish before returning by default
// (use WithConsumerOptionsForceShutdown option to disable this behavior).
// Use CloseWithContext to specify a context to cancel the handler completion.
// It does not close the connection manager, just the subscription
// to the connection manager and the consuming goroutines.
// Only call once.
func (consumer *Consumer) Close() {
if consumer.options.CloseGracefully {
consumer.options.Logger.Infof("waiting for handler to finish...")
err := consumer.waitForHandlerCompletion(context.Background())
if err != nil {
consumer.options.Logger.Warnf("error while waiting for handler to finish: %v", err)
}
}

consumer.cleanupResources()

}

func (consumer *Consumer) cleanupResources() {
consumer.isClosedMux.Lock()
defer consumer.isClosedMux.Unlock()
consumer.isClosed = true
Expand All @@ -124,6 +150,24 @@ func (consumer *Consumer) Close() {
}()
}

// CloseWithContext cleans up resources and closes the consumer.
// It waits for handler to finish before returning
// (use WithConsumerOptionsForceShutdown option to disable this behavior).
// Use the context to cancel the handler completion.
// CloseWithContext does not close the connection manager, just the subscription
// to the connection manager and the consuming goroutines.
// Only call once.
func (consumer *Consumer) CloseWithContext(ctx context.Context) {
if consumer.options.CloseGracefully {
err := consumer.waitForHandlerCompletion(ctx)
if err != nil {
consumer.options.Logger.Warnf("error while waiting for handler to finish: %v", err)
}
}

consumer.cleanupResources()
}

// startGoroutines declares the queue if it doesn't exist,
// binds the queue to the routing key(s), and starts the goroutines
// that will consume from the queue
Expand Down Expand Up @@ -211,3 +255,23 @@ func handlerGoroutine(consumer *Consumer, msgs <-chan amqp.Delivery, consumeOpti
}
consumer.options.Logger.Infof("rabbit consumer goroutine closed")
}

func (consumer *Consumer) waitForHandlerCompletion(ctx context.Context) error {
if ctx == nil {
ctx = context.Background()
} else if ctx.Err() != nil {
return ctx.Err()
}
c := make(chan struct{})
go func() {
consumer.handlerMux.Lock()
defer consumer.handlerMux.Unlock()
close(c)
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-c:
return nil
}
}
8 changes: 8 additions & 0 deletions consumer_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func getDefaultConsumerOptions(queueName string) ConsumerOptions {
},
ExchangeOptions: []ExchangeOptions{},
Concurrency: 1,
CloseGracefully: true,
Logger: stdDebugLogger{},
QOSPrefetch: 10,
QOSGlobal: false,
Expand Down Expand Up @@ -64,6 +65,7 @@ func getDefaultBindingOptions() BindingOptions {
type ConsumerOptions struct {
RabbitConsumerOptions RabbitConsumerOptions
QueueOptions QueueOptions
CloseGracefully bool
ExchangeOptions []ExchangeOptions
Concurrency int
Logger logger.Logger
Expand Down Expand Up @@ -311,6 +313,12 @@ func WithConsumerOptionsQOSGlobal(options *ConsumerOptions) {
options.QOSGlobal = true
}

// WithConsumerOptionsForceShutdown tells the consumer to not wait for
// the handler to complete in consumer.Close
func WithConsumerOptionsForceShutdown(options *ConsumerOptions) {
options.CloseGracefully = false
}

// WithConsumerOptionsQueueQuorum sets the queue a quorum type, which means
// multiple nodes in the cluster will have the messages distributed amongst them
// for higher reliability
Expand Down
Loading