-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
76 lines (59 loc) · 1.07 KB
/
conn.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
package etcd
import (
"context"
"time"
"github.com/freshcn/log"
"github.com/coreos/etcd/clientv3"
)
var (
// etcd 连接
conn *clientv3.Client
// RetryNum 重试次数
RetryNum = 5
// closeETCD 关闭etcd
closeETCD bool
)
// getConn 获取etcd连接
// retry 是否为重试连接
func getConn(retry ...bool) *clientv3.Client {
var (
err error
)
if conn != nil {
return conn
}
conn, err = clientv3.New(config)
if err != nil {
if len(retry) == 0 { // 当不为重试连接时
for i := 0; i < RetryNum; i++ {
conn = getConn(true)
if conn != nil {
break
}
}
if conn == nil {
log.Panic(err)
}
}
}
return conn
}
// getCtx 获取上下文
func getCtx() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), time.Second*5)
}
// stringSliceEqual 对比string的slice是否一样
func stringSliceEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
if (a == nil) != (b == nil) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}