-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
client.go
300 lines (275 loc) · 9.16 KB
/
client.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
package wsrpc
import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/smartcontractkit/wsrpc"
"github.com/smartcontractkit/wsrpc/connectivity"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/csakey"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/wsrpc/pb"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
// MaxConsecutiveTransmitFailures controls how many consecutive requests are
// allowed to time out before we reset the connection
const MaxConsecutiveTransmitFailures = 5
var (
timeoutCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "mercury_transmit_timeout_count",
Help: "Running count of transmit timeouts",
},
[]string{"serverURL"},
)
dialCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "mercury_dial_count",
Help: "Running count of dials to mercury server",
},
[]string{"serverURL"},
)
dialSuccessCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "mercury_dial_success_count",
Help: "Running count of successful dials to mercury server",
},
[]string{"serverURL"},
)
dialErrorCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "mercury_dial_error_count",
Help: "Running count of errored dials to mercury server",
},
[]string{"serverURL"},
)
connectionResetCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "mercury_connection_reset_count",
Help: fmt.Sprintf("Running count of times connection to mercury server has been reset (connection reset happens automatically after %d consecutive transmit failures)", MaxConsecutiveTransmitFailures),
},
[]string{"serverURL"},
)
)
type Client interface {
services.ServiceCtx
pb.MercuryClient
}
type Conn interface {
WaitForReady(ctx context.Context) bool
GetState() connectivity.State
Close()
}
type client struct {
utils.StartStopOnce
csaKey csakey.KeyV2
serverPubKey []byte
serverURL string
logger logger.Logger
conn Conn
client pb.MercuryClient
consecutiveTimeoutCnt atomic.Int32
wg sync.WaitGroup
chStop utils.StopChan
chResetTransport chan struct{}
timeoutCountMetric prometheus.Counter
dialCountMetric prometheus.Counter
dialSuccessCountMetric prometheus.Counter
dialErrorCountMetric prometheus.Counter
connectionResetCountMetric prometheus.Counter
}
// Consumers of wsrpc package should not usually call NewClient directly, but instead use the Pool
func NewClient(lggr logger.Logger, clientPrivKey csakey.KeyV2, serverPubKey []byte, serverURL string) Client {
return newClient(lggr, clientPrivKey, serverPubKey, serverURL)
}
func newClient(lggr logger.Logger, clientPrivKey csakey.KeyV2, serverPubKey []byte, serverURL string) *client {
return &client{
csaKey: clientPrivKey,
serverPubKey: serverPubKey,
serverURL: serverURL,
logger: lggr.Named("WSRPC").With("mercuryServerURL", serverURL),
chResetTransport: make(chan struct{}, 1),
chStop: make(chan struct{}),
timeoutCountMetric: timeoutCount.WithLabelValues(serverURL),
dialCountMetric: dialCount.WithLabelValues(serverURL),
dialSuccessCountMetric: dialSuccessCount.WithLabelValues(serverURL),
dialErrorCountMetric: dialErrorCount.WithLabelValues(serverURL),
connectionResetCountMetric: connectionResetCount.WithLabelValues(serverURL),
}
}
func (w *client) Start(_ context.Context) error {
return w.StartOnce("WSRPC Client", func() error {
if err := w.dial(context.Background()); err != nil {
return err
}
w.wg.Add(1)
go w.runloop()
return nil
})
}
// NOTE: Dial is non-blocking, and will retry on an exponential backoff
// in the background until close is called, or context is cancelled.
// This is why we use the background context, not the start context here.
//
// Any transmits made while client is still trying to dial will fail
// with error.
func (w *client) dial(ctx context.Context, opts ...wsrpc.DialOption) error {
w.dialCountMetric.Inc()
conn, err := wsrpc.DialWithContext(ctx, w.serverURL,
append(opts,
wsrpc.WithTransportCreds(w.csaKey.Raw().Bytes(), w.serverPubKey),
wsrpc.WithLogger(w.logger),
)...,
)
if err != nil {
w.dialErrorCountMetric.Inc()
setLivenessMetric(false)
return errors.Wrap(err, "failed to dial wsrpc client")
}
w.dialSuccessCountMetric.Inc()
setLivenessMetric(true)
w.conn = conn
w.client = pb.NewMercuryClient(conn)
return nil
}
func (w *client) runloop() {
defer w.wg.Done()
for {
select {
case <-w.chStop:
return
case <-w.chResetTransport:
// Using channel here ensures we only have one reset in process at
// any given time
w.resetTransport()
}
}
}
// resetTransport disconnects and reconnects to the mercury server
func (w *client) resetTransport() {
w.connectionResetCountMetric.Inc()
ok := w.IfStarted(func() {
w.conn.Close() // Close is safe to call multiple times
})
if !ok {
panic("resetTransport should never be called unless client is in 'started' state")
}
ctx, cancel := w.chStop.Ctx(context.Background())
defer cancel()
b := utils.NewRedialBackoff()
for {
// Will block until successful dial, or context is canceled (i.e. on close)
if err := w.dial(ctx, wsrpc.WithBlock()); err != nil {
if ctx.Err() != nil {
w.logger.Debugw("ResetTransport exiting due to client Close", "err", err)
return
}
w.logger.Errorw("ResetTransport failed to redial", "err", err)
time.Sleep(b.Duration())
} else {
break
}
}
w.logger.Info("ResetTransport successfully redialled")
}
func (w *client) Close() error {
return w.StopOnce("WSRPC Client", func() error {
close(w.chStop)
w.conn.Close()
w.wg.Wait()
return nil
})
}
func (w *client) Name() string {
return "EVM.Mercury.WSRPCClient"
}
func (w *client) HealthReport() map[string]error {
return map[string]error{w.Name(): w.Healthy()}
}
// Healthy if connected
func (w *client) Healthy() (err error) {
if err = w.StartStopOnce.Healthy(); err != nil {
return err
}
state := w.conn.GetState()
if state != connectivity.Ready {
return errors.Errorf("client state should be %s; got %s", connectivity.Ready, state)
}
return nil
}
func (w *client) waitForReady(ctx context.Context) (err error) {
ok := w.IfStarted(func() {
if ready := w.conn.WaitForReady(ctx); !ready {
err = errors.Errorf("websocket client not ready; got state: %v", w.conn.GetState())
return
}
})
if !ok {
return errors.New("client is not started")
}
return
}
func (w *client) Transmit(ctx context.Context, req *pb.TransmitRequest) (resp *pb.TransmitResponse, err error) {
w.logger.Trace("Transmit")
start := time.Now()
if err = w.waitForReady(ctx); err != nil {
return nil, errors.Wrap(err, "Transmit call failed")
}
resp, err = w.client.Transmit(ctx, req)
if errors.Is(err, context.DeadlineExceeded) {
w.timeoutCountMetric.Inc()
cnt := w.consecutiveTimeoutCnt.Add(1)
if cnt == MaxConsecutiveTransmitFailures {
w.logger.Errorf("Timed out on %d consecutive transmits, resetting transport", cnt)
// NOTE: If we get 5+ request timeouts in a row, close and re-open
// the websocket connection.
//
// This *shouldn't* be necessary in theory (ideally, wsrpc would
// handle it for us) but it acts as a "belts and braces" approach
// to ensure we get a websocket connection back up and running
// again if it gets itself into a bad state.
select {
case w.chResetTransport <- struct{}{}:
default:
// This can happen if we had 5 consecutive timeouts, already
// sent a reset signal, then the connection started working
// again (resetting the count) then we got 5 additional
// failures before the runloop was able to close the bad
// connection.
//
// It should be safe to just ignore in this case.
//
// Debug log in case my reasoning is wrong.
w.logger.Debugf("Transport is resetting, cnt=%d", cnt)
}
}
} else {
w.consecutiveTimeoutCnt.Store(0)
}
if err != nil {
w.logger.Warnw("Transmit call failed due to networking error", "err", err, "resp", resp)
incRequestStatusMetric(statusFailed)
} else {
w.logger.Tracew("Transmit call succeeded", "resp", resp)
incRequestStatusMetric(statusSuccess)
setRequestLatencyMetric(float64(time.Since(start).Milliseconds()))
}
return
}
func (w *client) LatestReport(ctx context.Context, req *pb.LatestReportRequest) (resp *pb.LatestReportResponse, err error) {
lggr := w.logger.With("req.FeedId", hexutil.Encode(req.FeedId))
lggr.Trace("LatestReport")
if err = w.waitForReady(ctx); err != nil {
return nil, errors.Wrap(err, "LatestReport failed")
}
resp, err = w.client.LatestReport(ctx, req)
if err != nil {
lggr.Errorw("LatestReport failed", "err", err, "resp", resp)
} else if resp.Error != "" {
lggr.Errorw("LatestReport failed; mercury server returned error", "err", resp.Error, "resp", resp)
} else {
lggr.Debugw("LatestReport succeeded", "resp", resp)
}
return
}