/
rabbitmq.go
50 lines (41 loc) · 1.01 KB
/
rabbitmq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package checkers
import (
"context"
"errors"
"time"
"github.com/moeryomenko/healing"
"github.com/streadway/amqp"
)
func AQMPProber(conn *amqp.Connection, heartbeatPeriod time.Duration) (
liveness, readiness func(context.Context) healing.CheckResult,
) {
blocked := make(chan amqp.Blocking, 0)
blocked = conn.NotifyBlocked(blocked)
closed := make(chan *amqp.Error, 0)
closed = conn.NotifyClose(closed)
return func(ctx context.Context) healing.CheckResult {
return CheckHelper(func() error {
select {
case c := <-closed:
close(closed)
closed = make(chan *amqp.Error, 0)
closed = conn.NotifyClose(closed)
return errors.New(c.Reason)
default:
}
return nil
})
}, func(ctx context.Context) healing.CheckResult {
return CheckHelper(func() error {
select {
case b := <-blocked:
close(blocked)
blocked = make(chan amqp.Blocking, 0)
blocked = conn.NotifyBlocked(blocked)
return errors.New(b.Reason)
default:
}
return nil
})
}
}