-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws_client.go
168 lines (147 loc) · 4.57 KB
/
ws_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
package gitopia
import (
"context"
"time"
"github.com/cometbft/cometbft/libs/log"
jsonrpcclient "github.com/cometbft/cometbft/rpc/jsonrpc/client"
jsonrpctypes "github.com/cometbft/cometbft/rpc/jsonrpc/types"
"github.com/gitopia/gitopia-go/logger"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
const (
TM_WS_PING_PERIOD = 10 * time.Second
TM_WS_MAX_RECONNECT = 3
)
var (
mTmError = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: viper.GetString("APP_NAME"),
Name: "tm_errors",
Help: "Number of tm errors",
}, []string{"error"})
)
type evenHandlerFunc func(context.Context, []byte) error
type WSEvents struct {
wsc *jsonrpcclient.WSClient
query string
}
func NewWSEvents(ctx context.Context, query string) (*WSEvents, error) {
wse := &WSEvents{
query: query,
}
var err error
wse.wsc, err = jsonrpcclient.NewWS(viper.GetString("TM_ADDR"),
TM_WS_ENDPOINT,
jsonrpcclient.PingPeriod(TM_WS_PING_PERIOD),
jsonrpcclient.MaxReconnectAttempts(TM_WS_MAX_RECONNECT),
jsonrpcclient.OnReconnect(func() {
// resubscribe immediately
wse.subscribeAfter(0 * time.Second)
}))
if err != nil {
return nil, errors.Wrap(err, "error creating ws client")
}
w := logger.FromContext(ctx).WriterLevel(logrus.DebugLevel)
l := log.NewTMLogger(log.NewSyncWriter(w))
wse.wsc.SetLogger(l)
if err := wse.wsc.Start(); err != nil {
return nil, errors.Wrap(err, "error connecting to WS")
}
return wse, nil
}
func terminateOnCancel(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return nil
}
// processes events from tm
// cancel context to stop processing
// returns error on failure
// returns error when event handler returns error
// handler must handle all errors. it must return only fatal errors
func (wse *WSEvents) Subscribe(ctx context.Context, h evenHandlerFunc) (<-chan struct{}, chan error) {
e := make(chan error)
done := make(chan struct{})
go func() {
defer func() { close(done) }()
logger.FromContext(ctx).Debug("subscribing to tm")
defer logger.FromContext(ctx).Debug("subscription done")
err := wse.wsc.Subscribe(ctx, wse.query)
if err != nil {
e <- errors.Wrap(err, "error sending subscribe request")
return
}
//!! CAUTION!! all events are processed sequentially in order to support backfill!
// this might lead to event queue overflow on the chain and connection disconnection
for {
err = terminateOnCancel(ctx)
if err != nil {
e <- err
return
}
var event jsonrpctypes.RPCResponse
select {
case event = <-wse.wsc.ResponsesCh:
case <-wse.wsc.Quit():
e <- errors.New("ws conn closed")
return
}
if event.Error != nil {
logger.FromContext(ctx).Error("WS error", "err", event.Error.Error())
// Error can be ErrAlreadySubscribed or max client (subscriptions per
// client) reached or Tendermint exited.
// We can ignore ErrAlreadySubscribed, but need to retry in other
// cases.
// if !isErrAlreadySubscribed(event.Error) {
// // Resubscribe after 1 second to give Tendermint time to restart (if
// // crashed).
// wse.subscribeAfter(1 * time.Second)
// }
// OnReconnect handles this
mTmError.With(prometheus.Labels{"error": "ws_event_error"}).Inc()
continue
}
jsonBuf, err := event.Result.MarshalJSON()
if err != nil {
logger.FromContext(ctx).WithError(err).WithField("result", event.Result).
Error("error parsing result. ignoring event")
mTmError.With(prometheus.Labels{"error": "parse_error"}).Inc()
continue
}
// hack: TM sends empty event to begin with. skipping
if string(jsonBuf) == "{}" {
logger.FromContext(ctx).Info("received empty event. continuing...")
continue
}
err = h(ctx, jsonBuf)
if err != nil {
logger.FromContext(ctx).Error(errors.WithMessage(err, "error from event handler"))
mTmError.With(prometheus.Labels{"error": "handler_error"}).Inc()
e <- err
return
}
}
}()
return ctx.Done(), e
}
func (wse *WSEvents) Unsubscribe(ctx context.Context, query string) error {
if err := wse.wsc.Unsubscribe(ctx, query); err != nil {
return err
}
return nil
}
// After being reconnected, it is necessary to redo subscription to server
// otherwise no data will be automatically received.
func (wse *WSEvents) subscribeAfter(d time.Duration) {
time.Sleep(d)
err := wse.wsc.Subscribe(context.Background(), wse.query)
if err != nil {
wse.wsc.Logger.Error("Failed to resubscribe", "err", err)
}
}