-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmq_check.go
51 lines (45 loc) · 969 Bytes
/
rmq_check.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
51
package godoctor
import (
"context"
"errors"
"github.com/streadway/amqp"
"time"
)
type rmqChecker struct {
userName string
password string
host string
port string
}
const rabbitMqCheckerName = "rabbit_mq"
func (c *rmqChecker) getName() checkerName {
return rabbitMqCheckerName
}
func (c *rmqChecker) Check(ctx context.Context, timeout time.Duration) error {
errChan := make(chan error)
go func() {
conn, err := amqp.DialConfig("amqp://"+c.userName+":"+c.password+"@"+c.host+":"+c.port+"/", amqp.Config{
Heartbeat: 2 * timeout,
Locale: "en_US",
})
if err != nil {
errChan <- err
}
errChan <- conn.Close()
}()
select {
case <-time.After(timeout):
return errors.New("ping timed out")
case err := <-errChan:
close(errChan)
return err
}
}
func RabbitMqChecker(userName, password, host, port string) IChecker {
return &rmqChecker{
userName: userName,
password: password,
host: host,
port: port,
}
}