forked from kelseyhightower/confd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
85 lines (77 loc) · 1.9 KB
/
client.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
75
76
77
78
79
80
81
82
83
84
85
package redis
import (
"fmt"
"github.com/garyburd/redigo/redis"
"os"
"strings"
"time"
)
// Client is a wrapper around the redis client
type Client struct {
client redis.Conn
}
// NewRedisClient returns an *redis.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
func NewRedisClient(machines []string) (*Client, error) {
var err error
for _, address := range machines {
var conn redis.Conn
network := "tcp"
if _, err = os.Stat(address); err == nil {
network = "unix"
}
conn, err = redis.DialTimeout(network, address, time.Second, time.Second, time.Second)
if err != nil {
continue
}
return &Client{conn}, nil
}
return nil, err
}
// GetValues queries redis for keys prefixed by prefix.
func (c *Client) GetValues(keys []string) (map[string]string, error) {
vars := make(map[string]string)
for _, key := range keys {
key = strings.Replace(key, "/*", "", -1)
value, err := redis.String(c.client.Do("GET", key))
if err == nil {
vars[key] = value
continue
}
if err != redis.ErrNil {
return vars, err
}
if key == "/" {
key = "/*"
} else {
key = fmt.Sprintf("%s/*", key)
}
idx := 0
for {
values, err := redis.Values(c.client.Do("SCAN", idx, "MATCH", key, "COUNT", "1000"))
if err != nil && err != redis.ErrNil {
return vars, err
}
idx, _ = redis.Int(values[0], nil)
items, _ := redis.Strings(values[1], nil)
for _, item := range items {
var newKey string
if newKey, err = redis.String(item, nil); err != nil {
return vars, err
}
if value, err = redis.String(c.client.Do("GET", newKey)); err == nil {
vars[newKey] = value
}
}
if idx == 0 {
break
}
}
}
return vars, nil
}
// WatchPrefix is not yet implemented.
func (c *Client) WatchPrefix(prefix string, waitIndex uint64, stopChan chan bool) (uint64, error) {
<-stopChan
return 0, nil
}