This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
options.go
116 lines (93 loc) · 3.12 KB
/
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
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
package etcd
import (
"context"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
"github.com/cr-mao/lorig/conf"
)
const (
defaultAddr = "127.0.0.1:2379"
defaultDialTimeout = 5
defaultNamespace = "services"
defaultTimeout = 3
defaultRetryTimes = 3
defaultRetryInterval = 10
)
const (
defaultAddrsKey = "config.registry.etcd.addrs"
defaultDialTimeoutKey = "config.registry.etcd.dialTimeout"
defaultNamespaceKey = "config.registry.etcd.namespace"
defaultTimeoutKey = "config.registry.etcd.timeout"
defaultRetryTimesKey = "config.registry.etcd.retryTimes"
defaultRetryIntervalKey = "config.registry.etcd.retryInterval"
)
type Option func(o *options)
type options struct {
// 客户端连接地址
// 内建客户端配置,默认为[]string{"localhost:2379"}
addrs []string
// 客户端拨号超时时间
// 内建客户端配置,默认为5秒
dialTimeout time.Duration
// 外部客户端
// 外部客户端配置,存在外部客户端时,优先使用外部客户端,默认为nil
client *clientv3.Client
// 上下文
// 默认context.Background
ctx context.Context
// 命名空间
// 默认为services
namespace string
// 上下文超时时间
// 默认为3秒
timeout time.Duration
// 心跳重试次数
// 默认为3次
retryTimes int
// 心跳重试间隔
// 默认为10秒
retryInterval time.Duration
}
func defaultOptions() *options {
return &options{
ctx: context.Background(),
addrs: conf.GetStrings(defaultAddrsKey, []string{defaultAddr}),
dialTimeout: time.Duration(conf.GetInt64(defaultDialTimeoutKey, defaultDialTimeout)) * time.Second,
namespace: conf.GetString(defaultNamespaceKey, defaultNamespace),
timeout: time.Duration(conf.GetInt64(defaultTimeoutKey, defaultTimeout)) * time.Second,
retryTimes: conf.GetInt(defaultRetryTimesKey, defaultRetryTimes),
retryInterval: time.Duration(conf.GetInt64(defaultRetryIntervalKey, defaultRetryInterval)) * time.Second,
}
}
// WithAddrs 设置客户端连接地址
func WithAddrs(addrs ...string) Option {
return func(o *options) { o.addrs = addrs }
}
// WithDialTimeout 设置客户端拨号超时时间
func WithDialTimeout(dialTimeout time.Duration) Option {
return func(o *options) { o.dialTimeout = dialTimeout }
}
// WithClient 设置外部客户端
func WithClient(client *clientv3.Client) Option {
return func(o *options) { o.client = client }
}
// WithContext 设置上下文
func WithContext(ctx context.Context) Option {
return func(o *options) { o.ctx = ctx }
}
// WithNamespace 设置命名空间
func WithNamespace(namespace string) Option {
return func(o *options) { o.namespace = namespace }
}
// WithTimeout 设置上下文超时时间
func WithTimeout(timeout time.Duration) Option {
return func(o *options) { o.timeout = timeout }
}
// WithRetryTimes 设置心跳重试次数
func WithRetryTimes(retryTimes int) Option {
return func(o *options) { o.retryTimes = retryTimes }
}
// WithRetryInterval 设置心跳重试间隔时间
func WithRetryInterval(retryInterval time.Duration) Option {
return func(o *options) { o.retryInterval = retryInterval }
}