-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimple_client.go
More file actions
123 lines (105 loc) · 3.19 KB
/
simple_client.go
File metadata and controls
123 lines (105 loc) · 3.19 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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 (
"fmt"
//"time"
"github.com/Masterminds/cookoo"
"github.com/coreos/etcd/clientv3"
)
// 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) (*clientv3.GetResponse, 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) (*clientv3.PutResponse, 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.(clientv3.Client),
}, nil
}
// NewSimpleClient Provides a simple wrapper around the old API.
//
// DO NOT USE for new code. Instead, use NewClient().
func NewSimpleClient(hosts []string) (*SimpleEtcdClient, error) {
cfg := clientv3.Config{
Endpoints: hosts,
}
r, err := clientv3.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 clientv3.Client
}
// Get clientv3.GetResponse
func (c *SimpleEtcdClient) Get(key string, sort bool, rec bool) (*clientv3.GetResponse, error) {
k := clientv3.NewKV(&c.realClient)
if rec {
return k.Get(dctx(), key, clientv3.WithPrefix())
} else {
return k.Get(dctx(), key)
}
}
// Set clientv3.GetResponse
func (c *SimpleEtcdClient) Set(key, val string, ttl uint64) (*clientv3.PutResponse, error) {
l := clientv3.Lease(&c.realClient)
k := clientv3.NewKV(&c.realClient)
// We're banking on people not using really uge ttls. In the code base, the
// highest is only a few hundred.
if ttl > 0 {
leaseGrantResp, err := l.Grant(dctx(), int64(ttl))
if err != nil {
fmt.Println(err)
return nil, err
}
opts := []clientv3.OpOption{clientv3.WithLease(leaseGrantResp.ID)}
return k.Put(dctx(), key, val, opts...)
} else {
return k.Put(dctx(), key, val)
}
}
// clientv2
// CreateDir by name
//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})
//}