-
Notifications
You must be signed in to change notification settings - Fork 411
/
options.go
116 lines (101 loc) · 3 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 wknet
import (
"runtime"
"time"
"github.com/WuKongIM/WuKongIM/pkg/wknet/crypto/tls"
)
type Options struct {
// Addr is the listen addr example: tcp://127.0.0.1:5100
Addr string
// TcpTlsConfig tcp tls config
TCPTLSConfig *tls.Config
WSTLSConfig *tls.Config
// WsAddr is the listen addr example: ws://127.0.0.1:5200或 wss://127.0.0.1:5200
WsAddr string
WssAddr string // wss addr
// WSTlsConfig ws tls config
// MaxOpenFiles is the maximum number of open files that the server can
MaxOpenFiles int
// SubReactorNum is sub reactor numver it's set to runtime.NumCPU() by default
SubReactorNum int
// OnCreateConn allow custom conn
// ReadBuffSize is the read size of the buffer each time from the connection
ReadBufferSize int
// MaxWriteBufferSize is the write maximum size of the buffer for each connection
MaxWriteBufferSize int
// MaxReadBufferSize is the read maximum size of the buffer for each connection
MaxReadBufferSize int
// SocketRecvBuffer sets the maximum socket receive buffer in bytes.
SocketRecvBuffer int
// SocketSendBuffer sets the maximum socket send buffer in bytes.
SocketSendBuffer int
// TCPKeepAlive sets up a duration for (SO_KEEPALIVE) socket option.
TCPKeepAlive time.Duration
}
func NewOptions() *Options {
return &Options{
Addr: "tcp://127.0.0.1:5100",
MaxOpenFiles: GetMaxOpenFiles(),
SubReactorNum: runtime.NumCPU(),
ReadBufferSize: 1024 * 32,
MaxWriteBufferSize: 1024 * 1024 * 50,
MaxReadBufferSize: 1024 * 1024 * 5,
}
}
type Option func(opts *Options)
// WithAddr set listen addr
func WithAddr(v string) Option {
return func(opts *Options) {
opts.Addr = v
}
}
func WithWSAddr(v string) Option {
return func(opts *Options) {
opts.WsAddr = v
}
}
func WithWSSAddr(v string) Option {
return func(opts *Options) {
opts.WssAddr = v
}
}
func WithTCPTLSConfig(v *tls.Config) Option {
return func(opts *Options) {
opts.TCPTLSConfig = v
}
}
func WithWSTLSConfig(v *tls.Config) Option {
return func(opts *Options) {
opts.WSTLSConfig = v
}
}
// WithMaxOpenFiles the maximum number of open files that the server can
func WithMaxOpenFiles(v int) Option {
return func(opts *Options) {
opts.MaxOpenFiles = v
}
}
// WithSubReactorNum set sub reactor number
func WithSubReactorNum(v int) Option {
return func(opts *Options) {
opts.SubReactorNum = v
}
}
// WithSocketRecvBuffer sets the maximum socket receive buffer in bytes.
func WithSocketRecvBuffer(recvBuf int) Option {
return func(opts *Options) {
opts.SocketRecvBuffer = recvBuf
}
}
// WithSocketSendBuffer sets the maximum socket send buffer in bytes.
func WithSocketSendBuffer(sendBuf int) Option {
return func(opts *Options) {
opts.SocketSendBuffer = sendBuf
}
}
// WithTCPKeepAlive sets up a duration for (SO_KEEPALIVE) socket option.
func WithTCPKeepAlive(v time.Duration) Option {
return func(opts *Options) {
opts.TCPKeepAlive = v
}
}