-
Notifications
You must be signed in to change notification settings - Fork 193
/
options.go
107 lines (91 loc) · 1.78 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
package gev
import (
"time"
)
// Options 服务配置
type Options struct {
Network string
Address string
NumLoops int
ReusePort bool
IdleTime time.Duration
Protocol Protocol
Strategy LoadBalanceStrategy
tick time.Duration
wheelSize int64
metricsPath, metricsAddress string
}
// Option ...
type Option func(*Options)
func newOptions(opt ...Option) *Options {
opts := Options{}
for _, o := range opt {
o(&opts)
}
if opts.Network == "" {
opts.Network = "tcp"
}
if opts.Address == "" {
opts.Address = ":1388"
}
if opts.tick == 0 {
opts.tick = 1 * time.Millisecond
}
if opts.wheelSize == 0 {
opts.wheelSize = 1000
}
if opts.Protocol == nil {
opts.Protocol = &DefaultProtocol{}
}
if opts.Strategy == nil {
opts.Strategy = RoundRobin()
}
return &opts
}
// ReusePort 设置 SO_REUSEPORT
func ReusePort(reusePort bool) Option {
return func(o *Options) {
o.ReusePort = reusePort
}
}
// Network [tcp] 暂时只支持tcp
func Network(n string) Option {
return func(o *Options) {
o.Network = n
}
}
// Address server 监听地址
func Address(a string) Option {
return func(o *Options) {
o.Address = a
}
}
// NumLoops work eventloop 的数量
func NumLoops(n int) Option {
return func(o *Options) {
o.NumLoops = n
}
}
// CustomProtocol 数据包处理
func CustomProtocol(p Protocol) Option {
return func(o *Options) {
o.Protocol = p
}
}
// IdleTime 最大空闲时间(秒)
func IdleTime(t time.Duration) Option {
return func(o *Options) {
o.IdleTime = t
}
}
func LoadBalance(strategy LoadBalanceStrategy) Option {
return func(o *Options) {
o.Strategy = strategy
}
}
func MetricsServer(path, address string) Option {
return func(o *Options) {
o.metricsPath = path
o.metricsAddress = address
}
}