forked from lonng/nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
147 lines (127 loc) · 3.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
package nano
import (
"net/http"
"time"
"github.com/lonng/nano/cluster"
"github.com/lonng/nano/component"
"github.com/lonng/nano/internal/env"
"github.com/lonng/nano/internal/log"
"github.com/lonng/nano/internal/message"
"github.com/lonng/nano/pipeline"
"github.com/lonng/nano/serialize"
"google.golang.org/grpc"
)
type Option func(*cluster.Options)
func WithPipeline(pipeline pipeline.Pipeline) Option {
return func(opt *cluster.Options) {
opt.Pipeline = pipeline
}
}
// WithAdvertiseAddr sets the advertise address option, it will be the listen address in
// master node and an advertise address which cluster member to connect
func WithAdvertiseAddr(addr string, retryInterval ...time.Duration) Option {
return func(opt *cluster.Options) {
opt.AdvertiseAddr = addr
if len(retryInterval) > 0 {
opt.RetryInterval = retryInterval[0]
}
}
}
// WithMemberAddr sets the listen address which is used to establish connection between
// cluster members. Will select an available port automatically if no member address
// setting and panic if no available port
func WithClientAddr(addr string) Option {
return func(opt *cluster.Options) {
opt.ClientAddr = addr
}
}
// WithMaster sets the option to indicate whether the current node is master node
func WithMaster() Option {
return func(opt *cluster.Options) {
opt.IsMaster = true
}
}
// WithGrpcOptions sets the grpc dial options
func WithGrpcOptions(opts ...grpc.DialOption) Option {
return func(_ *cluster.Options) {
env.GrpcOptions = append(env.GrpcOptions, opts...)
}
}
// WithComponents sets the Components
func WithComponents(components *component.Components) Option {
return func(opt *cluster.Options) {
opt.Components = components
}
}
// WithHeartbeatInterval sets Heartbeat time interval
func WithHeartbeatInterval(d time.Duration) Option {
return func(_ *cluster.Options) {
env.Heartbeat = d
}
}
// WithCheckOriginFunc sets the function that check `Origin` in http headers
func WithCheckOriginFunc(fn func(*http.Request) bool) Option {
return func(opt *cluster.Options) {
env.CheckOrigin = fn
}
}
// WithDebugMode let 'nano' to run under Debug mode.
func WithDebugMode() Option {
return func(_ *cluster.Options) {
env.Debug = true
}
}
// SetDictionary sets routes map
func WithDictionary(dict map[string]uint16) Option {
return func(_ *cluster.Options) {
message.SetDictionary(dict)
}
}
func WithWSPath(path string) Option {
return func(_ *cluster.Options) {
env.WSPath = path
}
}
// SetTimerPrecision sets the ticker precision, and time precision can not less
// than a Millisecond, and can not change after application running. The default
// precision is time.Second
func WithTimerPrecision(precision time.Duration) Option {
if precision < time.Millisecond {
panic("time precision can not less than a Millisecond")
}
return func(_ *cluster.Options) {
env.TimerPrecision = precision
}
}
// WithSerializer customizes application serializer, which automatically Marshal
// and UnMarshal handler payload
func WithSerializer(serializer serialize.Serializer) Option {
return func(opt *cluster.Options) {
env.Serializer = serializer
}
}
// WithLabel sets the current node label in cluster
func WithLabel(label string) Option {
return func(opt *cluster.Options) {
opt.Label = label
}
}
// WithIsWebsocket indicates whether current node WebSocket is enabled
func WithIsWebsocket(enableWs bool) Option {
return func(opt *cluster.Options) {
opt.IsWebsocket = enableWs
}
}
// WithTSLConfig sets the `key` and `certificate` of TSL
func WithTSLConfig(certificate, key string) Option {
return func(opt *cluster.Options) {
opt.TSLCertificate = certificate
opt.TSLKey = key
}
}
// WithLogger overrides the default logger
func WithLogger(l log.Logger) Option {
return func(opt *cluster.Options) {
log.SetLogger(l)
}
}