-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
130 lines (111 loc) · 2.49 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
package wirenet
import (
"crypto/tls"
"io"
"math"
"time"
)
const (
DefaultKeepAliveInterval = 15 * time.Second
DefaultEnableKeepAlive = true
DefaultReadTimeout = 10 * time.Second
DefaultWriteTimeout = 10 * time.Second
DefaultAcceptBacklog = 256
DefaultSessionCloseTimeout = 5 * time.Second
DefaultRetryMax = 100
DefaultRetryWaitMax = 60 * time.Second
DefaultRetryWaitMin = 5 * time.Second
)
type (
Option func(*wire)
Identification []byte
Token []byte
)
func WithConnectHook(hook func(io.Closer)) Option {
return func(w *wire) {
w.onConnect = hook
}
}
func WithErrorHandler(h ErrorHandler) Option {
return func(w *wire) {
w.errorHandler = h
}
}
func WithSessionOpenHook(hook SessionHook) Option {
return func(w *wire) {
w.openSessHook = hook
}
}
func WithSessionCloseHook(hook SessionHook) Option {
return func(w *wire) {
w.closeSessHook = hook
}
}
func WithIdentification(id Identification, token Token) Option {
return func(w *wire) {
w.identification = id
w.token = token
}
}
func WithTokenValidator(v TokenValidator) Option {
return func(w *wire) {
w.verifyToken = v
}
}
func WithTLS(conf *tls.Config) Option {
return func(w *wire) {
w.tlsConfig = conf
}
}
func WithRetryWait(min, max time.Duration) Option {
return func(w *wire) {
w.retryWaitMax = max
w.retryWaitMin = min
}
}
func WithRetryMax(n int) Option {
return func(w *wire) {
w.retryMax = n
}
}
func WithLogWriter(w io.Writer) Option {
return func(wire *wire) {
wire.transportConf.LogOutput = w
}
}
func WithKeepAliveInterval(interval time.Duration) Option {
return func(wire *wire) {
wire.transportConf.KeepAliveInterval = interval
wire.transportConf.EnableKeepAlive = true
}
}
func WithKeepAlive(flag bool) Option {
return func(wire *wire) {
wire.transportConf.EnableKeepAlive = flag
}
}
func WithReadWriteTimeouts(read, write time.Duration) Option {
return func(wire *wire) {
wire.readTimeout = read
wire.writeTimeout = write
wire.transportConf.ConnectionWriteTimeout = write
}
}
func WithSessionCloseTimeout(dur time.Duration) Option {
return func(w *wire) {
w.sessCloseTimeout = dur
}
}
func WithRetryPolicy(rp RetryPolicy) Option {
return func(w *wire) {
w.retryPolicy = rp
}
}
func DefaultRetryPolicy(min, max time.Duration, attemptNum int) time.Duration {
m := math.Pow(2, float64(attemptNum)) * float64(min)
wait := time.Duration(m)
if float64(wait) != m || wait > max {
wait = max
}
return wait
}