-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathoptions.go
309 lines (256 loc) · 7.97 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
package connection
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"os"
"time"
"github.com/moov-io/iso8583"
)
type Options struct {
// ConnectTimeout sets the timeout for establishing new connections.
// The default is 10 seconds.
ConnectTimeout time.Duration
// SendTimeout sets the timeout for a Send operation.
// The default is 30 seconds.
SendTimeout time.Duration
// IdleTime is the period at which the client will be sending ping
// message to the server.
// The default is 5 seconds.
IdleTime time.Duration
// ReadTimeout is the maximum time between read events before the
// ReadTimeoutHandler is called.
// The default is 60 seconds.
ReadTimeout time.Duration
// PingHandler is called when no message was sent during idle time
// it should be safe for concurrent use
PingHandler func(c *Connection)
// ReadTimeoutHandler is called when no message has been received within
// the ReadTimeout interval
ReadTimeoutHandler func(c *Connection)
// InboundMessageHandler is called when a message from the server is
// received and no matching request for it was found.
// InboundMessageHandler should be safe for concurrent use. Use it
// for the following use cases:
// * to log timed out responses
// * to handle network management messages (echo, heartbeat, etc.)
InboundMessageHandler func(c *Connection, message *iso8583.Message)
// ConnectionClosedHandlers is called after connection is closed by us,
// by the server or when there are network errors during network
// read/write
ConnectionClosedHandlers []func(c *Connection)
// ConnectionEstablishedHandler is called when connection is
// established with the server
ConnectionEstablishedHandler func(c *Connection)
TLSConfig *tls.Config
// ErrorHandler is called in a goroutine with the errors that can't be
// returned to the caller
ErrorHandler func(err error)
// If both OnConnect and OnConnectCtx are set, OnConnectCtx will be used
// OnConnect is called synchronously when a connection is established
OnConnect func(c *Connection) error
// OnConnectCtx is called synchronously when a connection is established
OnConnectCtx func(ctx context.Context, c *Connection) error
// If both OnClose and OnCloseCtx are set, OnCloseCtx will be used
// OnClose is called synchronously before a connection is closed
OnClose func(c *Connection) error
// OnCloseCtx is called synchronously before a connection is closed
OnCloseCtx func(ctx context.Context, c *Connection) error
// RequestIDGenerator is used to generate a unique identifier for a request
// so that responses from the server can be matched to the original request.
RequestIDGenerator RequestIDGenerator
// MessageReader is used to read a message from the connection
// if set, connection's MessageLengthReader will be ignored
MessageReader MessageReader
// MessageWriter is used to write a message to the connection
// if set, connection's MessageLengthWriter will be ignored
MessageWriter MessageWriter
}
type MessageReader interface {
ReadMessage(r io.Reader) (*iso8583.Message, error)
}
type MessageWriter interface {
WriteMessage(w io.Writer, message *iso8583.Message) error
}
type Option func(*Options) error
func GetDefaultOptions() Options {
return Options{
ConnectTimeout: 10 * time.Second,
SendTimeout: 30 * time.Second,
IdleTime: 5 * time.Second,
ReadTimeout: 60 * time.Second,
PingHandler: nil,
TLSConfig: nil,
RequestIDGenerator: &defaultRequestIDGenerator{},
}
}
// IdleTime sets an IdleTime option
func IdleTime(d time.Duration) Option {
return func(o *Options) error {
o.IdleTime = d
return nil
}
}
// SendTimeout sets an SendTimeout option
func SendTimeout(d time.Duration) Option {
return func(o *Options) error {
o.SendTimeout = d
return nil
}
}
// ConnectTimeout sets an SendTimeout option
func ConnectTimeout(d time.Duration) Option {
return func(o *Options) error {
o.ConnectTimeout = d
return nil
}
}
// ReadTimeout sets an ReadTimeout option
func ReadTimeout(d time.Duration) Option {
return func(o *Options) error {
o.ReadTimeout = d
return nil
}
}
// ReadTimeoutHandler sets a ReadTimeoutHandler option
func ReadTimeoutHandler(handler func(c *Connection)) Option {
return func(o *Options) error {
o.ReadTimeoutHandler = handler
return nil
}
}
// PingHandler sets a PingHandler option
func PingHandler(handler func(c *Connection)) Option {
return func(o *Options) error {
o.PingHandler = handler
return nil
}
}
// ConnectionClosedHandler sets a ConnectionClosedHandler option
func ConnectionClosedHandler(handler func(c *Connection)) Option {
return func(o *Options) error {
o.ConnectionClosedHandlers = append(o.ConnectionClosedHandlers, handler)
return nil
}
}
func ConnectionEstablishedHandler(handler func(c *Connection)) Option {
return func(o *Options) error {
o.ConnectionEstablishedHandler = handler
return nil
}
}
// InboundMessageHandler sets an InboundMessageHandler option
func InboundMessageHandler(handler func(c *Connection, message *iso8583.Message)) Option {
return func(o *Options) error {
o.InboundMessageHandler = handler
return nil
}
}
// ErrorHandler sets an ErrorHandler option
// in many cases err will be an instance of the `SafeError`
// for more details: https://github.com/moov-io/iso8583/pull/185
func ErrorHandler(h func(err error)) Option {
return func(opts *Options) error {
opts.ErrorHandler = h
return nil
}
}
// OnConnect sets a callback that will be synchronously called when connection is established.
// If it returns error, then connections will be closed and re-connect will be attempted
func OnConnect(h func(c *Connection) error) Option {
return func(opts *Options) error {
opts.OnConnect = h
return nil
}
}
// OnConnectCtx sets a callback that will be synchronously called when connection is established.
// If it returns error, then connections will be closed and re-connect will be attempted
func OnConnectCtx(h func(ctx context.Context, c *Connection) error) Option {
return func(opts *Options) error {
opts.OnConnectCtx = h
return nil
}
}
func OnClose(h func(c *Connection) error) Option {
return func(opts *Options) error {
opts.OnClose = h
return nil
}
}
func OnCloseCtx(h func(ctx context.Context, c *Connection) error) Option {
return func(opts *Options) error {
opts.OnCloseCtx = h
return nil
}
}
func defaultTLSConfig() *tls.Config {
return &tls.Config{
MinVersion: tls.VersionTLS12,
}
}
func ClientCert(cert, key string) Option {
return func(o *Options) error {
if o.TLSConfig == nil {
o.TLSConfig = defaultTLSConfig()
}
certificate, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return fmt.Errorf("loading certificate: %w", err)
}
o.TLSConfig.Certificates = []tls.Certificate{certificate}
return nil
}
}
// RootCAs creates pool of Root CAs
func RootCAs(file ...string) Option {
return func(o *Options) error {
if o.TLSConfig == nil {
o.TLSConfig = defaultTLSConfig()
}
certPool := x509.NewCertPool()
for _, f := range file {
cert, err := os.ReadFile(f)
if err != nil {
return fmt.Errorf("loading root certificate %s: %w", file, err)
}
ok := certPool.AppendCertsFromPEM(cert)
if !ok {
return fmt.Errorf("parsing root certificate %s: %w", file, err)
}
}
o.TLSConfig.RootCAs = certPool
return nil
}
}
func SetTLSConfig(cfg func(*tls.Config)) Option {
return func(o *Options) error {
if o.TLSConfig == nil {
o.TLSConfig = defaultTLSConfig()
}
cfg(o.TLSConfig)
return nil
}
}
// RequestIDGenerator sets a RequestIDGenerator option
func SetRequestIDGenerator(g RequestIDGenerator) Option {
return func(o *Options) error {
o.RequestIDGenerator = g
return nil
}
}
// SetMessageReader sets a MessageReader option
func SetMessageReader(r MessageReader) Option {
return func(o *Options) error {
o.MessageReader = r
return nil
}
}
// SetMessageWriter sets a MessageWriter option
func SetMessageWriter(w MessageWriter) Option {
return func(o *Options) error {
o.MessageWriter = w
return nil
}
}