forked from rainhq/signalr
/
config.go
145 lines (123 loc) · 3.47 KB
/
config.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
package signalr
import (
"net/http"
"net/url"
"time"
"github.com/cenkalti/backoff/v4"
)
type DialOpt func(*config)
func HTTPClient(client *http.Client) DialOpt {
return func(c *config) {
c.Client = client
}
}
func Dialer(dialer WebsocketDialerFunc) DialOpt {
return func(c *config) {
c.Dialer = dialer
}
}
func Protocol(protocol string) DialOpt {
return func(c *config) {
c.Protocol = protocol
}
}
func Params(params url.Values) DialOpt {
return func(c *config) {
c.Params = params
}
}
func Headers(headers http.Header) DialOpt {
return func(c *config) {
c.Headers = headers
}
}
// The maximum number of times to re-attempt a negotiation.
func MaxNegotiateRetries(retries int) DialOpt {
return func(c *config) {
c.MaxNegotiateRetries = retries
}
}
// The maximum number of times to re-attempt a connection.
func MaxConnectRetries(retries int) DialOpt {
return func(c *config) {
c.MaxConnectRetries = retries
}
}
func MaxReconnectRetries(retries int) DialOpt {
return func(c *config) {
c.MaxReconnectRetries = retries
}
}
// The maximum number of times to re-attempt a start command.
func MaxStartRetries(retries int) DialOpt {
return func(c *config) {
c.MaxStartRetries = retries
}
}
// The time to wait before retrying, in the event that an error occurs
// when contacting the SignalR service.
func RetryInterval(interval time.Duration) DialOpt {
return func(c *config) {
c.RetryInterval = interval
}
}
// The maximum amount of time to spend retrying a reconnect attempt.
func MaxReconnectDuration(duration time.Duration) DialOpt {
return func(c *config) {
c.MaxReconnectDuration = duration
}
}
// MaxMessageProcessDuration the maximum amount of time to spend on processing message
func MaxMessageProcessDuration(duration time.Duration) DialOpt {
return func(c *config) {
c.MaxMessageProcessDuration = duration
}
}
type config struct {
Client *http.Client
Dialer WebsocketDialerFunc
Protocol string
Params url.Values
Headers http.Header
MaxNegotiateRetries int
MaxConnectRetries int
MaxReconnectRetries int
MaxReconnectDuration time.Duration
MaxStartRetries int
RetryInterval time.Duration
MaxMessageProcessDuration time.Duration
}
func (c config) NegotiateBackoff() backoff.BackOff {
return constantBackoff(c.RetryInterval, c.MaxNegotiateRetries)
}
func (c config) ConnectBackoff() backoff.BackOff {
return constantBackoff(c.RetryInterval, c.MaxConnectRetries)
}
func (c config) ReconnectBackoff() backoff.BackOff {
return constantBackoff(c.RetryInterval, c.MaxReconnectRetries)
}
func (c config) StartBackoff() backoff.BackOff {
return constantBackoff(c.RetryInterval, c.MaxStartRetries)
}
var newDefaultConfig = func() config {
return config{
Client: http.DefaultClient,
Dialer: NewDefaultDialer,
Protocol: "1.5",
Params: make(url.Values),
Headers: make(http.Header),
MaxNegotiateRetries: 5,
MaxConnectRetries: 5,
MaxReconnectRetries: 5,
MaxReconnectDuration: 5 * time.Minute,
MaxStartRetries: 5,
RetryInterval: 1 * time.Second,
MaxMessageProcessDuration: 10 * time.Second,
}
}
func constantBackoff(interval time.Duration, maxRetries int) backoff.BackOff {
return backoff.WithMaxRetries(
backoff.NewConstantBackOff(interval),
uint64(maxRetries),
)
}