-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_checker.go
57 lines (51 loc) · 1.36 KB
/
health_checker.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
52
53
54
55
56
57
package sqs
import (
"context"
"github.com/aws/aws-sdk-go/service/sqs"
"time"
)
type HealthChecker struct {
Client *sqs.SQS
QueueName *string
Service string
Timeout time.Duration
}
func NewHealthChecker(client *sqs.SQS, queueName string, options ...string) *HealthChecker {
var name string
if len(options) > 0 && len(options[0]) > 0 {
name = options[0]
} else {
name = "sqs"
}
return NewSQSHealthChecker(client, name, queueName)
}
func NewSQSHealthChecker(client *sqs.SQS, name string, queueName string, options ...time.Duration) *HealthChecker {
var timeout time.Duration
if len(options) >= 1 && options[0] > 0 {
timeout = options[0]
} else {
timeout = 4 * time.Second
}
return &HealthChecker{Client: client, QueueName: &queueName, Service: name, Timeout: timeout}
}
func (h *HealthChecker) Name() string {
return h.Service
}
func (h *HealthChecker) Check(ctx context.Context) (map[string]interface{}, error) {
res := make(map[string]interface{})
h.Client.Config.HTTPClient.Timeout = h.Timeout
_, err := h.Client.GetQueueUrl(&sqs.GetQueueUrlInput{
QueueName: h.QueueName,
})
return res, err
}
func (h *HealthChecker) Build(ctx context.Context, data map[string]interface{}, err error) map[string]interface{} {
if err == nil {
return data
}
if data == nil {
data = make(map[string]interface{}, 0)
}
data["error"] = err.Error()
return data
}