-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
connection_manager.go
212 lines (166 loc) · 5.05 KB
/
connection_manager.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
package feeds
import (
"context"
"crypto/ed25519"
"sync"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/recovery"
pb "github.com/smartcontractkit/chainlink/core/services/feeds/proto"
"github.com/smartcontractkit/wsrpc"
"github.com/smartcontractkit/wsrpc/connectivity"
)
//go:generate mockery --name ConnectionsManager --output ./mocks/ --case=underscore
type ConnectionsManager interface {
Connect(opts ConnectOpts)
Disconnect(id int64) error
Close()
GetClient(id int64) (pb.FeedsManagerClient, error)
IsConnected(id int64) bool
}
// connectionsManager manages the rpc connections to Feeds Manager services
type connectionsManager struct {
mu sync.Mutex
wgClosed sync.WaitGroup
connections map[int64]*connection
lggr logger.Logger
}
type connection struct {
// ctx allows us to cancel any connections which are currently blocking
// while waiting to establish a connection to FMS.
ctx context.Context
cancel context.CancelFunc
connected bool
client pb.FeedsManagerClient
}
func newConnectionsManager(lggr logger.Logger) *connectionsManager {
return &connectionsManager{
mu: sync.Mutex{},
connections: map[int64]*connection{},
lggr: lggr,
}
}
// ConnectOpts defines the required options to connect to an FMS server
type ConnectOpts struct {
FeedsManagerID int64
// URI is the URI of the feeds manager
URI string
// Privkey defines the local CSA private key
Privkey []byte
// Pubkey defines the Feeds Manager Service's public key
Pubkey []byte
// Handlers defines the wsrpc Handlers
Handlers pb.NodeServiceServer
// OnConnect defines a callback for when the dial succeeds
OnConnect func(pb.FeedsManagerClient)
}
// Connects to a feeds manager
//
// Connection to FMS is handled in a goroutine because the Dial will block
// until it can establish a connection. This is important during startup because
// we do not want to block other services from starting.
//
// Eventually when FMS does come back up, wsrpc will establish the connection
// without any interaction on behalf of the node operator.
func (mgr *connectionsManager) Connect(opts ConnectOpts) {
ctx, cancel := context.WithCancel(context.Background())
conn := &connection{
ctx: ctx,
cancel: cancel,
connected: false,
}
mgr.wgClosed.Add(1)
mgr.mu.Lock()
mgr.connections[opts.FeedsManagerID] = conn
mgr.mu.Unlock()
go recovery.WrapRecover(mgr.lggr, func() {
defer mgr.wgClosed.Done()
mgr.lggr.Infow("Connecting to Feeds Manager...", "feedsManagerID", opts.FeedsManagerID)
clientConn, err := wsrpc.DialWithContext(conn.ctx, opts.URI,
wsrpc.WithTransportCreds(opts.Privkey, ed25519.PublicKey(opts.Pubkey)),
wsrpc.WithBlock(),
)
if err != nil {
// We only want to log if there was an error that did not occur
// from a context cancel.
if conn.ctx.Err() == nil {
mgr.lggr.Warnf("Error connecting to Feeds Manager server: %v", err)
} else {
mgr.lggr.Infof("Closing wsrpc websocket connection: %v", err)
}
return
}
defer clientConn.Close()
mgr.lggr.Infow("Connected to Feeds Manager", "feedsManagerID", opts.FeedsManagerID)
// Initialize a new wsrpc client to make RPC calls
mgr.mu.Lock()
conn.connected = true
conn.client = pb.NewFeedsManagerClient(clientConn)
mgr.connections[opts.FeedsManagerID] = conn
mgr.mu.Unlock()
// Initialize RPC call handlers on the client connection
pb.RegisterNodeServiceServer(clientConn, opts.Handlers)
if opts.OnConnect != nil {
opts.OnConnect(conn.client)
}
// Detect changes in connection status
go func() {
for {
s := clientConn.GetState()
clientConn.WaitForStateChange(conn.ctx, s)
s = clientConn.GetState()
// Exit the goroutine if we shutdown the connection
if s == connectivity.Shutdown {
break
}
mgr.mu.Lock()
conn.connected = s == connectivity.Ready
mgr.mu.Unlock()
}
}()
// Wait for close
<-conn.ctx.Done()
})
}
// Disconnect closes a single connection
func (mgr *connectionsManager) Disconnect(id int64) error {
mgr.mu.Lock()
defer mgr.mu.Unlock()
conn, ok := mgr.connections[id]
if !ok {
return errors.New("feeds manager is not connected")
}
conn.cancel()
delete(mgr.connections, id)
mgr.lggr.Infow("Disconnected Feeds Manager", "feedsManagerID", id)
return nil
}
// Close closes all connections
func (mgr *connectionsManager) Close() {
mgr.mu.Lock()
for _, conn := range mgr.connections {
conn.cancel()
}
mgr.mu.Unlock()
mgr.wgClosed.Wait()
}
// GetClient returns a single client by id
func (mgr *connectionsManager) GetClient(id int64) (pb.FeedsManagerClient, error) {
mgr.mu.Lock()
conn, ok := mgr.connections[id]
mgr.mu.Unlock()
if !ok || !conn.connected {
return nil, errors.New("feeds manager is not connected")
}
return conn.client, nil
}
// IsConnected returns true if the connection to a feeds manager is active
func (mgr *connectionsManager) IsConnected(id int64) bool {
mgr.mu.Lock()
conn, ok := mgr.connections[id]
mgr.mu.Unlock()
if !ok {
return false
}
return conn.connected
}