-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
134 lines (111 loc) · 3.3 KB
/
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
125
126
127
128
129
130
131
132
133
134
package gnet
import (
"github.com/panjf2000/gnet/v2"
"time"
)
// LoadBalancing represents the type of load-balancing algorithm.
type LoadBalancing int
const (
// RoundRobin assigns the next accepted connection to the event-loop by polling event-loop list.
RoundRobin LoadBalancing = iota
// LeastConnections assigns the next accepted connection to the event-loop that is
// serving the least number of active connections at the current time.
LeastConnections
// SourceAddrHash assigns the next accepted connection to the event-loop by hashing the remote address.
SourceAddrHash
)
// TCPSocketOpt is the type of TCP socket options.
type TCPSocketOpt int
// Available TCP socket options.
const (
TCPNoDelay TCPSocketOpt = iota
TCPDelay
)
type Option func(*option)
type option struct {
gnet.Options
}
// WithMulticore sets up multi-cores in gnet engine.
func WithMulticore(multicore bool) Option {
return func(opts *option) {
opts.Multicore = multicore
}
}
// WithLockOSThread sets up LockOSThread mode for I/O event-loops.
func WithLockOSThread(lockOSThread bool) Option {
return func(opts *option) {
opts.LockOSThread = lockOSThread
}
}
// WithReadBufferCap sets up ReadBufferCap for reading bytes.
func WithReadBufferCap(readBufferCap int) Option {
return func(opts *option) {
opts.ReadBufferCap = readBufferCap
}
}
// WithWriteBufferCap sets up WriteBufferCap for pending bytes.
func WithWriteBufferCap(writeBufferCap int) Option {
return func(opts *option) {
opts.WriteBufferCap = writeBufferCap
}
}
// WithLoadBalancing sets up the load-balancing algorithm in gnet engine.
func WithLoadBalancing(lb LoadBalancing) Option {
return func(opts *option) {
opts.LB = gnet.LoadBalancing(lb)
}
}
// WithNumEventLoop sets up NumEventLoop in gnet engine.
func WithNumEventLoop(numEventLoop int) Option {
return func(opts *option) {
opts.NumEventLoop = numEventLoop
}
}
// WithReusePort sets up SO_REUSEPORT socket option.
func WithReusePort(reusePort bool) Option {
return func(opts *option) {
opts.ReusePort = reusePort
}
}
// WithReuseAddr sets up SO_REUSEADDR socket option.
func WithReuseAddr(reuseAddr bool) Option {
return func(opts *option) {
opts.ReuseAddr = reuseAddr
}
}
// WithTCPKeepAlive sets up the SO_KEEPALIVE socket option with duration.
func WithTCPKeepAlive(tcpKeepAlive time.Duration) Option {
return func(opts *option) {
opts.TCPKeepAlive = tcpKeepAlive
}
}
// WithTCPNoDelay enable/disable the TCP_NODELAY socket option.
func WithTCPNoDelay(tcpNoDelay TCPSocketOpt) Option {
return func(opts *option) {
opts.TCPNoDelay = gnet.TCPSocketOpt(tcpNoDelay)
}
}
// WithSocketRecvBuffer sets the maximum socket receive buffer in bytes.
func WithSocketRecvBuffer(recvBuf int) Option {
return func(opts *option) {
opts.SocketRecvBuffer = recvBuf
}
}
// WithSocketSendBuffer sets the maximum socket send buffer in bytes.
func WithSocketSendBuffer(sendBuf int) Option {
return func(opts *option) {
opts.SocketSendBuffer = sendBuf
}
}
// WithTicker indicates that a ticker is set.
func WithTicker(ticker bool) Option {
return func(opts *option) {
opts.Ticker = ticker
}
}
// WithMulticastInterfaceIndex sets the interface name where UDP multicast sockets will be bound to.
func WithMulticastInterfaceIndex(idx int) Option {
return func(opts *option) {
opts.MulticastInterfaceIndex = idx
}
}