forked from Allenxuxu/gev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
91 lines (76 loc) · 1.45 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
package gev
import (
"time"
"github.com/Allenxuxu/gev/connection"
)
// Options 服务配置
type Options struct {
Network string
Address string
NumLoops int
ReusePort bool
tick time.Duration
wheelSize int64
IdleTime time.Duration
Protocol connection.Protocol
}
// Option ...
type Option func(*Options)
func newOptions(opt ...Option) *Options {
opts := Options{}
for _, o := range opt {
o(&opts)
}
if len(opts.Network) == 0 {
opts.Network = "tcp"
}
if len(opts.Address) == 0 {
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 = &connection.DefaultProtocol{}
}
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
}
}
// Protocol 数据包处理
func Protocol(p connection.Protocol) Option {
return func(o *Options) {
o.Protocol = p
}
}
// IdleTime 最大空闲时间(秒)
func IdleTime(t time.Duration) Option {
return func(o *Options) {
o.IdleTime = t
}
}