forked from lonng/nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
172 lines (149 loc) · 4.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package nano
import (
"time"
"github.com/aura-studio/nano/cluster"
"github.com/aura-studio/nano/codec"
"github.com/aura-studio/nano/component"
"github.com/aura-studio/nano/env"
"github.com/aura-studio/nano/log"
"github.com/aura-studio/nano/message"
"github.com/aura-studio/nano/persist"
"github.com/aura-studio/nano/pipeline"
"github.com/aura-studio/nano/serialize"
"google.golang.org/grpc"
)
// Option defines a type for option, an option is a func operate cluster.Options
type Option func(*cluster.Options)
// WithPipeline sets the pipeline option.
func WithPipeline(pipeline pipeline.Pipeline) Option {
return func(opt *cluster.Options) {
opt.Pipeline = pipeline
}
}
// WithConvention sets the convention between
func WithConvention(convention cluster.Convention) Option {
return func(opt *cluster.Options) {
opt.Convention = convention
}
}
// 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]
}
}
}
// WithTCPAddr 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 WithTCPAddr(addr string) Option {
return func(opt *cluster.Options) {
opt.TCPAddr = addr
}
}
// WithDebugAddr works with debug http addr
func WithDebugAddr(addr string) Option {
return func(opt *cluster.Options) {
opt.DebugAddr = addr
}
}
func WithMemberAddr(addr string) Option {
return func(opt *cluster.Options) {
opt.MemberAddr = 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
}
}
// WithDebugMode makes 'nano' run under Debug mode.
func WithDebugMode(debug bool) Option {
return func(_ *cluster.Options) {
env.Debug = debug
}
}
// WithSafeMode makes 'nano' run under Safe mode.
func WithSafeMode(safe bool) Option {
return func(_ *cluster.Options) {
env.Safe = safe
}
}
// WithTimerPrecision 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
env.SerializerType = message.GetSerializerType(serializer)
}
}
// WithLabel sets the current node label in cluster
func WithLabel(label string) Option {
return func(opt *cluster.Options) {
opt.Label = label
}
}
// WithVersion sets the current node version in cluster
func WithVersion(version string) Option {
return func(opt *cluster.Options) {
env.Version = version
env.ShortVersion = message.ShortVersion(version)
}
}
// WithHttpAddr sets the independent http address
func WithHttpAddr(httpAddr string) Option {
return func(opt *cluster.Options) {
opt.HttpAddr = httpAddr
}
}
// WithMasterPersist sets the persist of cluster
func WithMasterPersist(persist persist.Persist) Option {
return func(opt *cluster.Options) {
opt.MasterPersist = persist
}
}
// 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) {
opt.Logger = l
}
}
func WithCodec(c codec.Codec) Option {
return func(opt *cluster.Options) {
opt.Codec = c
}
}