-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_option.go
124 lines (105 loc) · 2.58 KB
/
client_option.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
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
124
package nacos
import (
"net/http"
"time"
)
type clientOptions struct {
maxCacheTime time.Duration
log LogInterface
httpClient *httpClient
listenInterval time.Duration
defautNameSpaceID string
defaultTenant string
discoveryIP string
appName string
maxRetryTimes int
}
type ClientOption interface {
apply(*clientOptions)
}
type funcClientOption struct {
f func(*clientOptions)
}
func newFuncClientOption(f func(*clientOptions)) *funcClientOption {
return &funcClientOption{
f: f,
}
}
func (fco *funcClientOption) apply(do *clientOptions) {
fco.f(do)
}
func HTTPTimeout(s time.Duration) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.httpClient.client.Timeout = s
})
}
func ListenInterval(s time.Duration) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.httpClient.listenClient.Timeout = s + 10*time.Second
o.listenInterval = s
})
}
func Log(log LogInterface) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.log = log
o.httpClient.log = o.log
})
}
func LogLevel(s string) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.log.SetLevel(s)
o.httpClient.log.SetLevel(s)
})
}
// Creds returns a ServerOption that sets credentials for server connections.
func HTTPTransport(c http.RoundTripper) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.httpClient.client.Transport = c
o.httpClient.listenClient.Transport = c
})
}
func Auth(user, password string) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.httpClient.username = user
o.httpClient.password = password
})
}
func MaxCacheTime(s time.Duration) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.maxCacheTime = s
})
}
func DefaultNameSpaceID(s string) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
if s != "" {
o.defautNameSpaceID = s
}
})
}
func DiscoveryIP(s string) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
if s != "" {
o.discoveryIP = s
}
})
}
func EnableHTTPRequestLog(b bool) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.httpClient.enableLog = b
})
}
func AppName(s string) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.appName = s
})
}
func DefaultTenant(s string) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.defaultTenant = s
})
}
func MaxHeartBeatRetryTimes(c int) ClientOption {
return newFuncClientOption(func(o *clientOptions) {
o.maxRetryTimes = c
})
}