-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth_checker.go
74 lines (64 loc) · 1.57 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package hive
import (
"context"
"fmt"
"time"
"github.com/beltran/gohive"
)
type HealthChecker struct {
Connection *gohive.Connection
name string
timeout time.Duration
}
func NewHiveHealthChecker(connection *gohive.Connection, name string, timeouts ...time.Duration) *HealthChecker {
var timeout time.Duration
if len(timeouts) >= 1 {
timeout = timeouts[0]
} else {
timeout = 4 * time.Second
}
return &HealthChecker{Connection: connection, name: name, timeout: timeout}
}
func NewHealthChecker(connection *gohive.Connection, options ...string) *HealthChecker {
var name string
if len(options) >= 1 && len(options[0]) > 0 {
name = options[0]
} else {
name = "hive"
}
return NewHiveHealthChecker(connection, name, 4*time.Second)
}
func (s *HealthChecker) Name() string {
return s.name
}
func (s *HealthChecker) Check(ctx context.Context) (map[string]interface{}, error) {
cancel := func() {}
if s.timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, s.timeout)
}
defer cancel()
res := make(map[string]interface{})
checkerChan := make(chan error)
go func() {
cursor := s.Connection.Cursor()
query := "SELECT CURRENT_TIMESTAMP"
cursor.Exec(ctx, query)
checkerChan <- cursor.Err
}()
select {
case err := <-checkerChan:
return res, err
case <-ctx.Done():
return res, fmt.Errorf("timeout")
}
}
func (s *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
}