-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopts.go
148 lines (132 loc) · 4.33 KB
/
opts.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
package client
import (
"crypto/tls"
"time"
)
type Option func(cfg *Cfg)
// WithServerAddr configures the server to
// connect to.
func WithServerAddr(addr string) Option {
return func(cfg *Cfg) {
cfg.TargetServer = addr
}
}
// WithOnStatusChangeHook allows the user registering function
// hooks for each status changes. Possible values for statuses
// can be found at go.eloylp.dev/goomerang/ws package.
//
// Sending multiple times this option in the constructor will lead
// to function concatenation, so multiple hooks can be configured.
//
// IMPORTANT NOTE: There's no panic catcher implementation here,
// ensure to implement it or ensure the underlying code does not
// panic.
func WithOnStatusChangeHook(h func(status uint32)) Option {
return func(cfg *Cfg) {
cfg.hooks.AppendOnStatusChange(h)
}
}
// WithHeartbeatInterval introduces how often the Ping/Pong
// operation should take place. Right now, this is intended
// to keep alive connections. Defaults to 5 seconds.
func WithHeartbeatInterval(interval time.Duration) Option {
return func(cfg *Cfg) {
cfg.HeartbeatInterval = interval
}
}
// WithOnCloseHook allows the user registering function
// hooks for when the client reaches the ws.StatusClosed status.
//
// Sending multiple times this option in the constructor will lead
// to function concatenation, so multiple hooks can be configured.
//
// IMPORTANT NOTE: There's no panic catcher implementation here,
// ensure to implement it or ensure the underlying code does not
// panic.
func WithOnCloseHook(h func()) Option {
return func(cfg *Cfg) {
cfg.hooks.AppendOnClose(h)
}
}
// WithOnErrorHook allows the user registering function
// hooks for when an internal error happens, but cannot be
// returned in any way to the client. Like in processing loops.
//
// IMPORTANT NOTE: There's no panic catcher implementation here,
// ensure to implement it or ensure the underlying code does not
// panic.
func WithOnErrorHook(h func(err error)) Option {
return func(cfg *Cfg) {
cfg.hooks.AppendOnError(h)
}
}
// WithOnConfiguration allows the user registering hook
// functions which will be invoked once the client is configured.
func WithOnConfiguration(h func(cfg *Cfg)) Option {
return func(cfg *Cfg) {
cfg.hooks.AppendOnConfiguration(h)
}
}
// WithOnWorkerStart allows the user registering hook
// functions which will be invoked whenever a new
// handler goroutine is invoked. Only if concurrency
// is enabled by the WithMaxConcurrency option.
func WithOnWorkerStart(h func()) Option {
return func(cfg *Cfg) {
cfg.hooks.AppendOnWorkerStart(h)
}
}
// WithOnWorkerEnd allows the user registering hook
// functions which will be invoked whenever a
// handler goroutine ends its operations. Only if concurrency
// is enabled by using WithMaxConcurrency option.
func WithOnWorkerEnd(h func()) Option {
return func(cfg *Cfg) {
cfg.hooks.AppendOnWorkerEnd(h)
}
}
// WithMaxConcurrency sets the concurrency level for handler
// execution. Values <= 1 means no concurrency, which means
// no goroutine scheduling takes place. Defaults to 10.
func WithMaxConcurrency(n int) Option {
return func(cfg *Cfg) {
cfg.MaxConcurrency = n
}
}
// WithTLSConfig allows the user to pass a *tls.Config
// full setup to the client, in which encryption and authentication
// could be configured, by setting up a PKI (public key infrastructure).
func WithTLSConfig(tlsCfg *tls.Config) Option {
return func(cfg *Cfg) {
cfg.TLSConfig = tlsCfg
}
}
// WithReadBufferSize configures the size in bytes of the
// read buffers for each connection. Defaults on 4096 bytes.
func WithReadBufferSize(s int) Option {
return func(cfg *Cfg) {
cfg.ReadBufferSize = s
}
}
// WithWriteBufferSize configures the size in bytes of the
// write buffers for each connection. Defaults on 4096 bytes.
func WithWriteBufferSize(s int) Option {
return func(cfg *Cfg) {
cfg.WriteBufferSize = s
}
}
// WithCompressionEnabled specifies if the client should try
// to negotiate compression (see https://datatracker.ietf.org/doc/html/rfc7692).
// Enabling this does not guarantee its success.
func WithCompressionEnabled(b bool) Option {
return func(cfg *Cfg) {
cfg.EnableCompression = b
}
}
// WithHandShakeTimeout set the time limit in which the websocket
// handshake should take place.
func WithHandShakeTimeout(d time.Duration) Option {
return func(cfg *Cfg) {
cfg.HandshakeTimeout = d
}
}