-
Notifications
You must be signed in to change notification settings - Fork 1
/
register_options.go
64 lines (53 loc) · 1.14 KB
/
register_options.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
package etcdkv
import (
"github.com/coreos/etcd/clientv3"
"log"
"time"
)
var registerErrorHandler = func(err error) {
log.Printf("etcdkv register error:%v \n", err)
}
func SetRegisterErrorHandler(fn func(error)) {
if fn != nil {
registerErrorHandler = fn
}
}
type kvs struct {
k string
v string
}
// register注册器选项
type registerOption struct {
client *clientv3.Client
namespace string
kvs []kvs
ttl time.Duration
}
type RegisterOption func(*registerOption)
func RegisterClient(opts ...ClientOption) RegisterOption {
_, client, err := newClientV3(opts...)
if err != nil {
registerErrorHandler(err)
}
return RegisterSetClient(client)
}
func RegisterSetClient(client *clientv3.Client) RegisterOption {
return func(o *registerOption) {
o.client = client
}
}
func RegisterNamespace(namespace string) RegisterOption {
return func(o *registerOption) {
o.namespace = namespace
}
}
func RegisterKvs(k, v string) RegisterOption {
return func(o *registerOption) {
o.kvs = append(o.kvs, kvs{k: k, v: v})
}
}
func RegisterTTL(ttl time.Duration) RegisterOption {
return func(o *registerOption) {
o.ttl = ttl
}
}