/
router_options.go
133 lines (118 loc) · 3.71 KB
/
router_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package router
import (
"crypto/tls"
"fmt"
"git.golaxy.org/core"
"git.golaxy.org/core/util/option"
clientv3 "go.etcd.io/etcd/client/v3"
"net"
"strings"
"time"
)
type RouterOptions struct {
EtcdClient *clientv3.Client
EtcdConfig *clientv3.Config
KeyPrefix string
WatchChanSize int
GroupTTL time.Duration
GroupAutoRefreshTTL bool
GroupSendDataChanSize int
GroupSendEventChanSize int
CustomUsername string
CustomPassword string
CustomAddresses []string
CustomTLSConfig *tls.Config
}
var With _Option
type _Option struct{}
// Default 默认值
func (_Option) Default() option.Setting[RouterOptions] {
return func(options *RouterOptions) {
With.EtcdClient(nil)(options)
With.EtcdConfig(nil)(options)
With.KeyPrefix("/golaxy/groups/")(options)
With.WatchChanSize(128)(options)
With.GroupTTL(30*time.Second, true)(options)
With.GroupSendDataChanSize(128)(options)
With.GroupSendEventChanSize(0)(options)
With.CustomAuth("", "")(options)
With.CustomAddresses("127.0.0.1:2379")(options)
With.CustomTLSConfig(nil)(options)
}
}
// EtcdClient etcd客户端,最优先使用
func (_Option) EtcdClient(cli *clientv3.Client) option.Setting[RouterOptions] {
return func(o *RouterOptions) {
o.EtcdClient = cli
}
}
// EtcdConfig etcd配置,次优先使用
func (_Option) EtcdConfig(config *clientv3.Config) option.Setting[RouterOptions] {
return func(o *RouterOptions) {
o.EtcdConfig = config
}
}
// KeyPrefix 所有key的前缀
func (_Option) KeyPrefix(prefix string) option.Setting[RouterOptions] {
return func(options *RouterOptions) {
if prefix != "" && !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
options.KeyPrefix = prefix
}
}
// WatchChanSize 监控服务变化的channel大小
func (_Option) WatchChanSize(size int) option.Setting[RouterOptions] {
return func(options *RouterOptions) {
if size < 0 {
panic(fmt.Errorf("%w: option WatchChanSize can't be set to a value less than 0", core.ErrArgs))
}
options.WatchChanSize = size
}
}
// GroupTTL 分组默认TTL
func (_Option) GroupTTL(ttl time.Duration, auto bool) option.Setting[RouterOptions] {
return func(options *RouterOptions) {
if ttl < 3*time.Second {
panic(fmt.Errorf("%w: option GroupTTL can't be set to a value less than 3 second", core.ErrArgs))
}
options.GroupTTL = ttl
options.GroupAutoRefreshTTL = auto
}
}
// GroupSendDataChanSize 分组默认发送数据的channel的大小,<=0表示不使用channel
func (_Option) GroupSendDataChanSize(size int) option.Setting[RouterOptions] {
return func(options *RouterOptions) {
options.GroupSendDataChanSize = size
}
}
// GroupSendEventChanSize 分组默认发送自定义事件的channel的大小,<=0表示不使用channel
func (_Option) GroupSendEventChanSize(size int) option.Setting[RouterOptions] {
return func(options *RouterOptions) {
options.GroupSendEventChanSize = size
}
}
// CustomAuth 自定义设置etcd鉴权信息
func (_Option) CustomAuth(username, password string) option.Setting[RouterOptions] {
return func(options *RouterOptions) {
options.CustomUsername = username
options.CustomPassword = password
}
}
// CustomAddresses 自定义设置etcd服务地址
func (_Option) CustomAddresses(addrs ...string) option.Setting[RouterOptions] {
return func(options *RouterOptions) {
for _, addr := range addrs {
if _, _, err := net.SplitHostPort(addr); err != nil {
panic(fmt.Errorf("%w: %w", core.ErrArgs, err))
}
}
options.CustomAddresses = addrs
}
}
// CustomTLSConfig 自定义设置加密etcd连接的配置
func (_Option) CustomTLSConfig(conf *tls.Config) option.Setting[RouterOptions] {
return func(o *RouterOptions) {
o.CustomTLSConfig = conf
}
}