-
Notifications
You must be signed in to change notification settings - Fork 0
/
redisprobe.go
54 lines (43 loc) · 939 Bytes
/
redisprobe.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
package redisprobe
import (
"context"
"net/url"
redis "github.com/redis/go-redis/v9"
errgo "gopkg.in/errgo.v1"
)
type RedisProbe struct {
name string
password string
host string
}
func NewRedisProbeFromURL(name, serviceUrl string) RedisProbe {
url, _ := url.Parse(serviceUrl)
password := ""
if url.User != nil {
password, _ = url.User.Password()
}
return NewRedisProbe(name, url.Host, password)
}
func NewRedisProbe(name, host, password string) RedisProbe {
return RedisProbe{
name: name,
password: password,
host: host,
}
}
func (p RedisProbe) Name() string {
return p.name
}
func (p RedisProbe) Check(ctx context.Context) error {
client := redis.NewClient(&redis.Options{
Addr: p.host,
Password: p.password,
DB: 0,
})
defer client.Close()
_, err := client.Ping(ctx).Result()
if err != nil {
return errgo.Notef(err, "unable to contact Redis host")
}
return nil
}