forked from plgd-dev/go-coap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.go
333 lines (272 loc) · 8.47 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package udp
import (
"context"
"net"
"time"
"github.com/matrix-org/go-coap/v2/net/blockwise"
"github.com/matrix-org/go-coap/v2/net/monitor/inactivity"
"github.com/matrix-org/go-coap/v2/shared"
"github.com/matrix-org/go-coap/v2/udp/client"
)
// HandlerFuncOpt handler function option.
type HandlerFuncOpt struct {
h HandlerFunc
}
func (o HandlerFuncOpt) apply(opts *serverOptions) {
opts.handler = o.h
}
func (o HandlerFuncOpt) applyDial(opts *dialOptions) {
opts.handler = o.h
}
// WithHandlerFunc set handle for handling request's.
func WithHandlerFunc(h HandlerFunc) HandlerFuncOpt {
return HandlerFuncOpt{h: h}
}
// ContextOpt handler function option.
type ContextOpt struct {
ctx context.Context
}
func (o ContextOpt) apply(opts *serverOptions) {
opts.ctx = o.ctx
}
func (o ContextOpt) applyDial(opts *dialOptions) {
opts.ctx = o.ctx
}
// WithContext set's parent context of server.
func WithContext(ctx context.Context) ContextOpt {
return ContextOpt{ctx: ctx}
}
// MaxMessageSizeOpt handler function option.
type MaxMessageSizeOpt struct {
maxMessageSize int
}
func (o MaxMessageSizeOpt) apply(opts *serverOptions) {
opts.maxMessageSize = o.maxMessageSize
}
func (o MaxMessageSizeOpt) applyDial(opts *dialOptions) {
opts.maxMessageSize = o.maxMessageSize
}
// WithMaxMessageSize limit size of processed message.
func WithMaxMessageSize(maxMessageSize int) MaxMessageSizeOpt {
return MaxMessageSizeOpt{maxMessageSize: maxMessageSize}
}
// ErrorsOpt errors option.
type ErrorsOpt struct {
errors ErrorFunc
}
func (o ErrorsOpt) apply(opts *serverOptions) {
opts.errors = o.errors
}
func (o ErrorsOpt) applyDial(opts *dialOptions) {
opts.errors = o.errors
}
// WithErrors set function for logging error.
func WithErrors(errors ErrorFunc) ErrorsOpt {
return ErrorsOpt{errors: errors}
}
// GoPoolOpt gopool option.
type GoPoolOpt struct {
goPool GoPoolFunc
}
func (o GoPoolOpt) apply(opts *serverOptions) {
opts.goPool = o.goPool
}
func (o GoPoolOpt) applyDial(opts *dialOptions) {
opts.goPool = o.goPool
}
// WithGoPool sets function for managing spawning go routines
// for handling incoming request's.
// Eg: https://github.com/panjf2000/ants.
func WithGoPool(goPool GoPoolFunc) GoPoolOpt {
return GoPoolOpt{goPool: goPool}
}
// KeepAliveOpt keepalive option.
type KeepAliveOpt struct {
maxRetries uint32
timeout time.Duration
onInactive inactivity.OnInactiveFunc
}
func (o KeepAliveOpt) apply(opts *serverOptions) {
opts.createInactivityMonitor = func() inactivity.Monitor {
keepalive := inactivity.NewKeepAlive(o.maxRetries, o.onInactive, func(cc inactivity.ClientConn, receivePong func()) (func(), error) {
return cc.(*client.ClientConn).AsyncPing(receivePong)
})
return inactivity.NewInactivityMonitor(o.timeout/time.Duration(o.maxRetries+1), keepalive.OnInactive)
}
}
func (o KeepAliveOpt) applyDial(opts *dialOptions) {
opts.createInactivityMonitor = func() inactivity.Monitor {
keepalive := inactivity.NewKeepAlive(o.maxRetries, o.onInactive, func(cc inactivity.ClientConn, receivePong func()) (func(), error) {
return cc.(*client.ClientConn).AsyncPing(receivePong)
})
return inactivity.NewInactivityMonitor(o.timeout/time.Duration(o.maxRetries+1), keepalive.OnInactive)
}
}
// WithKeepAlive monitoring's client connection's.
func WithKeepAlive(maxRetries uint32, timeout time.Duration, onInactive inactivity.OnInactiveFunc) KeepAliveOpt {
return KeepAliveOpt{
maxRetries: maxRetries,
timeout: timeout,
onInactive: onInactive,
}
}
// InactivityMonitorOpt notifies when a connection was inactive for a given duration.
type InactivityMonitorOpt struct {
duration time.Duration
onInactive inactivity.OnInactiveFunc
}
func (o InactivityMonitorOpt) apply(opts *serverOptions) {
opts.createInactivityMonitor = func() inactivity.Monitor {
return inactivity.NewInactivityMonitor(o.duration, o.onInactive)
}
}
func (o InactivityMonitorOpt) applyDial(opts *dialOptions) {
opts.createInactivityMonitor = func() inactivity.Monitor {
return inactivity.NewInactivityMonitor(o.duration, o.onInactive)
}
}
// WithInactivityMonitor set deadline's for read operations over client connection.
func WithInactivityMonitor(duration time.Duration, onInactive inactivity.OnInactiveFunc) InactivityMonitorOpt {
return InactivityMonitorOpt{
duration: duration,
onInactive: onInactive,
}
}
// NetOpt network option.
type NetOpt struct {
net string
}
func (o NetOpt) applyDial(opts *dialOptions) {
opts.net = o.net
}
// WithNetwork define's udp version (udp4, udp6, udp) for client.
func WithNetwork(net string) NetOpt {
return NetOpt{net: net}
}
// HeartBeatOpt heatbeat of read/write operations over connection.
type HeartBeatOpt struct {
heartbeat time.Duration
}
func (o HeartBeatOpt) applyDial(opts *dialOptions) {
opts.heartBeat = o.heartbeat
}
// WithHeartBeat set deadline's for read/write operations over client connection.
func WithHeartBeat(heartbeat time.Duration) HeartBeatOpt {
return HeartBeatOpt{heartbeat: heartbeat}
}
// BlockwiseOpt network option.
type BlockwiseOpt struct {
enable bool
szx blockwise.SZX
transferTimeout time.Duration
}
func (o BlockwiseOpt) apply(opts *serverOptions) {
opts.blockwiseEnable = o.enable
opts.blockwiseSZX = o.szx
opts.blockwiseTransferTimeout = o.transferTimeout
}
func (o BlockwiseOpt) applyDial(opts *dialOptions) {
opts.blockwiseEnable = o.enable
opts.blockwiseSZX = o.szx
opts.blockwiseTransferTimeout = o.transferTimeout
}
// WithBlockwise configure's blockwise transfer.
func WithBlockwise(enable bool, szx blockwise.SZX, transferTimeout time.Duration) BlockwiseOpt {
return BlockwiseOpt{
enable: enable,
szx: szx,
transferTimeout: transferTimeout,
}
}
// OnNewClientConnOpt network option.
type OnNewClientConnOpt struct {
onNewClientConn OnNewClientConnFunc
}
func (o OnNewClientConnOpt) apply(opts *serverOptions) {
opts.onNewClientConn = o.onNewClientConn
}
// WithOnNewClientConn server's notify about new client connection.
func WithOnNewClientConn(onNewClientConn OnNewClientConnFunc) OnNewClientConnOpt {
return OnNewClientConnOpt{
onNewClientConn: onNewClientConn,
}
}
// TransmissionOpt transmission options.
type TransmissionOpt struct {
transmissionNStart time.Duration
transmissionAcknowledgeTimeout time.Duration
transmissionMaxRetransmit int
}
func (o TransmissionOpt) apply(opts *serverOptions) {
opts.transmissionNStart = o.transmissionNStart
opts.transmissionAcknowledgeTimeout = o.transmissionAcknowledgeTimeout
opts.transmissionMaxRetransmit = o.transmissionMaxRetransmit
}
func (o TransmissionOpt) applyDial(opts *dialOptions) {
opts.transmissionNStart = o.transmissionNStart
opts.transmissionAcknowledgeTimeout = o.transmissionAcknowledgeTimeout
opts.transmissionMaxRetransmit = o.transmissionMaxRetransmit
}
// WithTransmission set options for (re)transmission for Confirmable message-s.
func WithTransmission(transmissionNStart time.Duration,
transmissionAcknowledgeTimeout time.Duration,
transmissionMaxRetransmit int) TransmissionOpt {
return TransmissionOpt{
transmissionNStart: transmissionNStart,
transmissionAcknowledgeTimeout: transmissionAcknowledgeTimeout,
transmissionMaxRetransmit: transmissionMaxRetransmit,
}
}
// GetMIDOpt get message ID option.
type GetMIDOpt struct {
getMID GetMIDFunc
}
func (o GetMIDOpt) apply(opts *serverOptions) {
opts.getMID = o.getMID
}
func (o GetMIDOpt) applyDial(opts *dialOptions) {
opts.getMID = o.getMID
}
// WithGetMID allows to set own getMID function to server/client.
func WithGetMID(getMID GetMIDFunc) GetMIDOpt {
return GetMIDOpt{getMID: getMID}
}
// CloseSocketOpt close socket option.
type CloseSocketOpt struct {
}
func (o CloseSocketOpt) applyDial(opts *dialOptions) {
opts.closeSocket = true
}
// WithCloseSocket closes socket at the close connection.
func WithCloseSocket() CloseSocketOpt {
return CloseSocketOpt{}
}
// DialerOpt dialer option.
type DialerOpt struct {
dialer *net.Dialer
}
func (o DialerOpt) applyDial(opts *dialOptions) {
if o.dialer != nil {
opts.dialer = o.dialer
}
}
// WithDialer set dialer for dial.
func WithDialer(dialer *net.Dialer) DialerOpt {
return DialerOpt{
dialer: dialer,
}
}
// LoggerOpt adds logging to this library
type LoggerOpt struct {
logger shared.Logger
}
func (o LoggerOpt) apply(opts *serverOptions) {
opts.logger = o.logger
}
func (o LoggerOpt) applyDial(opts *dialOptions) {
opts.logger = o.logger
}
// WithLogger adds logging
func WithLogger(logger shared.Logger) LoggerOpt {
return LoggerOpt{logger: logger}
}