Skip to content

Commit

Permalink
Merge pull request #61 from shmel1k/feature/rmq_timeout_checks
Browse files Browse the repository at this point in the history
[rabbitmq] add context to healthcheck
  • Loading branch information
vgarvardt authored Jul 9, 2021
2 parents 67072ba + 0c6be35 commit aed9fad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
coverage.txt
.idea/
15 changes: 14 additions & 1 deletion checks/rabbitmq/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
)

var (
defaultDialTimeout = 200 * time.Millisecond
defaultConsumeTimeout = time.Second * 3
)

Expand All @@ -34,6 +35,9 @@ type (
// ConsumeTimeout is the duration that health check will try to consume published test message.
// If not set - 3 seconds
ConsumeTimeout time.Duration
// DialTimeout is the duration that health check will try to dial to RabbitMQ.
// If not set - 200 milliseconds.
DialTimeout time.Duration
}
)

Expand All @@ -49,7 +53,9 @@ func New(config Config) func(ctx context.Context) error {
(&config).defaults()

return func(ctx context.Context) (checkErr error) {
conn, err := amqp.Dial(config.DSN)
conn, err := amqp.DialConfig(config.DSN, amqp.Config{
Dial: amqp.DefaultDial(config.DialTimeout),
})
if err != nil {
checkErr = fmt.Errorf("RabbitMQ health check failed on dial phase: %w", err)
return
Expand Down Expand Up @@ -119,6 +125,10 @@ func New(config Config) func(ctx context.Context) error {
case <-time.After(config.ConsumeTimeout):
checkErr = fmt.Errorf("RabbitMQ health check failed due to consume timeout: %w", err)
return
case <-ctx.Done():
checkErr = fmt.Errorf("RabbitMQ health check failed due "+
"to health check listener disconnect: %w", ctx.Err())
return
case <-done:
return
}
Expand Down Expand Up @@ -146,4 +156,7 @@ func (c *Config) defaults() {
if c.ConsumeTimeout == 0 {
c.ConsumeTimeout = defaultConsumeTimeout
}
if c.DialTimeout == 0 {
c.DialTimeout = defaultDialTimeout
}
}

0 comments on commit aed9fad

Please sign in to comment.