forked from go-steem/rpc
/
transport.go
306 lines (258 loc) · 7.6 KB
/
transport.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
package websocket
import (
"io"
// Stdlib
"context"
"net"
"time"
// Vendor
"github.com/gorilla/websocket"
"github.com/pkg/errors"
"github.com/sourcegraph/jsonrpc2"
tomb "gopkg.in/tomb.v2"
)
const (
DefaultHandshakeTimeout = 30 * time.Second
DefaultWriteTimeout = 10 * time.Second
DefaultReadTimeout = 20 * time.Second
DefaultAutoReconnectMaxDelay = 1 * time.Minute
InitialAutoReconnectDelay = 1 * time.Second
AutoReconnectBackoffCoefficient = 1.5
)
var netDialer net.Dialer
// Transport implements a CallCloser accessing the Steem RPC endpoint over WebSocket.
type Transport struct {
// URLs as passed into the constructor.
urls []string
nextURLIndex int
currentURL string
// Options.
handshakeTimeout time.Duration
readTimeout time.Duration
writeTimeout time.Duration
autoReconnectEnabled bool
autoReconnectMaxDelay time.Duration
monitorChan chan<- interface{}
// The underlying JSON-RPC connection.
connCh chan chan *jsonrpc2.Conn
errCh chan error
t *tomb.Tomb
}
// Option represents an option that can be passed into the transport constructor.
type Option func(*Transport)
// SetDialTimeout can be used to set the timeout when establishing a new connection.
//
// This function is deprecated, please use SetHandshakeTimeout.
func SetDialTimeout(timeout time.Duration) Option {
return SetHandshakeTimeout(timeout)
}
// SetHandshakeTimeout can be used to set the timeout for WebSocket handshake.
func SetHandshakeTimeout(timeout time.Duration) Option {
return func(t *Transport) {
t.handshakeTimeout = timeout
}
}
// SetReadTimeout sets the connection read timeout.
// The timeout is implemented using net.Conn.SetReadDeadline.
func SetReadTimeout(timeout time.Duration) Option {
return func(t *Transport) {
t.readTimeout = timeout
}
}
// SetWriteTimeout sets the connection read timeout.
// The timeout is implemented using net.Conn.SetWriteDeadline.
func SetWriteTimeout(timeout time.Duration) Option {
return func(t *Transport) {
t.writeTimeout = timeout
}
}
// SetReadWriteTimeout sets the connection read and write timeout.
// The timeout is implemented using net.Conn.SetDeadline.
func SetReadWriteTimeout(timeout time.Duration) Option {
return func(t *Transport) {
t.readTimeout = timeout
t.writeTimeout = timeout
}
}
// SetAutoReconnectEnabled can be used to enable automatic reconnection to the RPC endpoint.
// Exponential backoff is used when the connection cannot be established repetitively.
//
// See SetAutoReconnectMaxDelay to set the maximum delay between the reconnection attempts.
func SetAutoReconnectEnabled(enabled bool) Option {
return func(t *Transport) {
t.autoReconnectEnabled = enabled
}
}
// SetAutoReconnectMaxDelay can be used to set the maximum delay between the reconnection attempts.
//
// This option only takes effect when the auto-reconnect mode is enabled.
//
// The default value is 5 minutes.
func SetAutoReconnectMaxDelay(delay time.Duration) Option {
return func(t *Transport) {
t.autoReconnectMaxDelay = delay
}
}
// SetMonitor can be used to set the monitoring channel that can be used to watch
// connection-related state changes.
//
// All channel send operations are happening synchronously, so not receiving messages
// from the channel will lead to the whole thing getting stuck completely.
//
// This option only takes effect when the auto-reconnect mode is enabled.
//
// The channel is closed when the transport is closed.
func SetMonitor(monitorChan chan<- interface{}) Option {
return func(t *Transport) {
t.monitorChan = monitorChan
}
}
// NewTransport creates a new transport that connects to the given WebSocket URLs.
//
// It is possible to specify multiple WebSocket endpoint URLs.
// In case the transport is configured to reconnect automatically,
// the URL to connect to is rotated on every connect attempt using round-robin.
func NewTransport(urls []string, options ...Option) (*Transport, error) {
// Prepare a transport instance.
t := &Transport{
urls: urls,
handshakeTimeout: DefaultHandshakeTimeout,
readTimeout: DefaultReadTimeout,
writeTimeout: DefaultWriteTimeout,
autoReconnectMaxDelay: DefaultAutoReconnectMaxDelay,
connCh: make(chan chan *jsonrpc2.Conn),
errCh: make(chan error),
t: &tomb.Tomb{},
}
// Apply the options.
for _, opt := range options {
opt(t)
}
t.t.Go(t.dialer)
// Return the new transport.
return t, nil
}
// Call implements interfaces.CallCloser.
func (t *Transport) Call(method string, params, result interface{}) error {
// Limit the request context with the tomb context.
ctx := t.t.Context(nil)
Loop:
for {
// Request a connection.
connCh := make(chan *jsonrpc2.Conn, 1)
select {
case t.connCh <- connCh:
case <-ctx.Done():
return errors.Wrap(ctx.Err(), "context closed")
}
// Receive the connection.
conn := <-connCh
// Perform the call.
err := conn.Call(ctx, method, params, result)
if err == nil {
return nil
}
// In case this is a context error, return immediately.
if err := ctx.Err(); err != nil {
return errors.Wrap(err, "context closed")
}
// In case auto-reconnect is disabled, fail immediately.
if !t.autoReconnectEnabled {
return errors.Wrap(err, "call failed")
}
// In case this is a connection error, request a new connection.
err = errors.Cause(err)
if _, ok := err.(*websocket.CloseError); ok || err == io.ErrUnexpectedEOF {
select {
case t.errCh <- errors.Wrap(err, "WebSocket closed"):
continue Loop
case <-ctx.Done():
return errors.Wrap(ctx.Err(), "context closed")
}
}
// Some other error occurred, return it immediately.
return errors.Wrap(err, "call failed")
}
}
func (t *Transport) dialer() error {
ctx := t.t.Context(nil)
var conn *jsonrpc2.Conn
defer func() {
if conn != nil {
conn.Close()
err := errors.Wrap(ctx.Err(), "context closed")
t.emit(&DisconnectedEvent{t.currentURL, err})
}
}()
connect := func() {
delay := InitialAutoReconnectDelay
for {
var err error
conn, err = t.dial(ctx)
if err == nil {
break
}
select {
case <-time.After(delay):
delay = time.Duration(float64(delay) * AutoReconnectBackoffCoefficient)
if delay > t.autoReconnectMaxDelay {
delay = t.autoReconnectMaxDelay
}
case <-ctx.Done():
return
}
}
}
// Establish the initial connection.
connect()
for {
select {
case connCh := <-t.connCh:
connCh <- conn
case err := <-t.errCh:
conn.Close()
t.emit(&DisconnectedEvent{t.currentURL, err})
connect()
case <-ctx.Done():
return nil
}
}
}
func (t *Transport) dial(ctx context.Context) (*jsonrpc2.Conn, error) {
// Set up a dialer.
dialer := websocket.Dialer{
NetDial: func(network, addr string) (net.Conn, error) {
return netDialer.DialContext(ctx, network, addr)
},
HandshakeTimeout: t.handshakeTimeout,
}
// Get the next URL to try.
u := t.urls[t.nextURLIndex]
t.nextURLIndex = (t.nextURLIndex + 1) % len(t.urls)
t.currentURL = u
// Connect the WebSocket.
t.emit(&ConnectingEvent{u})
ws, _, err := dialer.Dial(u, nil)
if err != nil {
err = errors.Wrapf(err, "failed to dial %v", u)
t.emit(&DisconnectedEvent{u, err})
return nil, err
}
t.emit(&ConnectedEvent{u})
// Wrap the WebSocket with JSON-RPC2.
stream := NewObjectStream(ws, t.writeTimeout, t.readTimeout)
return jsonrpc2.NewConn(ctx, stream, nil), nil
}
func (t *Transport) emit(v interface{}) {
if t.monitorChan != nil {
select {
case t.monitorChan <- v:
default:
}
}
}
// Close implements interfaces.CallCloser.
func (t *Transport) Close() error {
t.t.Kill(nil)
return t.t.Wait()
}