-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimple_client.go
More file actions
106 lines (89 loc) · 2.85 KB
/
simple_client.go
File metadata and controls
106 lines (89 loc) · 2.85 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package etcd
// EVERYTHING IN THIS FILE IS DEPRECATED.
// It is here only for compatibility with older code, and will be removed
// as soon as we can purge builder.
import (
"time"
"github.com/Masterminds/cookoo"
"github.com/coreos/etcd/client"
)
var (
retryCycles = 2
retrySleep = 200 * time.Millisecond
)
// Getter describes the Get behavior of an Etcd client.
//
// Usually you will want to use go-etcd/etcd.Client to satisfy this.
//
// We use an interface because it is more testable.
type Getter interface {
Get(string, bool, bool) (*client.Response, error)
}
// DirCreator describes etcd's CreateDir behavior.
//
// Usually you will want to use go-etcd/etcd.Client to satisfy this.
type DirCreator interface {
CreateDir(string, uint64) (*client.Response, error)
}
// Setter sets a value in Etcd.
type Setter interface {
Set(string, string, uint64) (*client.Response, error)
}
// GetterSetter performs get and set operations.
type GetterSetter interface {
Getter
Setter
}
// CreateSimpleClient creates a legacy simple client.
//
// DO NOT USE unless you must for backward compatibility.
//
// Params:
// - url (string): A server to connect to. This runs through os.ExpandEnv().
// - retries (int): Number of times to retry a connection to the server
// - retrySleep (time.Duration): How long to sleep between retries
//
// Returns:
// This puts a simpleEtcdClient into context (implements Getter, Setter, etc.)
func CreateSimpleClient(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
r, e := CreateClient(c, p)
if e != nil {
return c, e
}
return &simpleEtcdClient{
realClient: r.(client.Client),
}, nil
}
// Provides a simple wrapper around the old API.
//
// DO NOT USE for new code. Instead, use NewClient().
func NewSimpleClient(hosts []string) (*simpleEtcdClient, error) {
cfg := client.Config{
Endpoints: hosts,
}
r, err := client.New(cfg)
if err != nil {
return nil, err
}
return &simpleEtcdClient{
realClient: r,
}, nil
}
// simpleEtcdClient provides an interface compatible with the old Etcd client.
type simpleEtcdClient struct {
realClient client.Client
}
func (c *simpleEtcdClient) Get(key string, sort bool, rec bool) (*client.Response, error) {
k := client.NewKeysAPI(c.realClient)
return k.Get(dctx(), key, &client.GetOptions{Sort: sort, Recursive: rec})
}
func (c *simpleEtcdClient) Set(key, val string, ttl uint64) (*client.Response, error) {
k := client.NewKeysAPI(c.realClient)
// We're banking on people not using really uge ttls. In the code base, the
// highest is only a few hundred.
return k.Set(dctx(), key, val, &client.SetOptions{TTL: time.Duration(ttl) * time.Second})
}
func (c *simpleEtcdClient) CreateDir(name string, ttl uint64) (*client.Response, error) {
k := client.NewKeysAPI(c.realClient)
return k.Set(dctx(), name, "", &client.SetOptions{TTL: time.Duration(ttl) * time.Second, Dir: true})
}