-
Notifications
You must be signed in to change notification settings - Fork 41
/
pool.go
393 lines (352 loc) · 10.5 KB
/
pool.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package client
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"sync"
"github.com/OpenBazaar/multiwallet/client/blockbook"
clientErr "github.com/OpenBazaar/multiwallet/client/errors"
"github.com/OpenBazaar/multiwallet/model"
"github.com/btcsuite/btcutil"
"github.com/op/go-logging"
"golang.org/x/net/proxy"
)
var Log = logging.MustGetLogger("pool")
// ClientPool is an implementation of the APIClient interface which will handle
// server failure, rotate servers, and retry API requests.
type ClientPool struct {
blockChan chan model.Block
cancelListenChan context.CancelFunc
listenAddrs []btcutil.Address
listenAddrsLock sync.Mutex
poolManager *rotationManager
proxyDialer proxy.Dialer
txChan chan model.Transaction
unblockStart chan struct{}
HTTPClient http.Client
}
func (p *ClientPool) newMaximumTryEnumerator() *maxTryEnum {
return &maxTryEnum{max: 3, attempts: 0}
}
type maxTryEnum struct{ max, attempts int }
func (m *maxTryEnum) next() bool {
var now = m.attempts
m.attempts++
return now < m.max
}
func (m *maxTryEnum) isFinal() bool {
return m.attempts == m.max
}
// NewClientPool instantiates a new ClientPool object with the given server APIs
func NewClientPool(endpoints []string, proxyDialer proxy.Dialer) (*ClientPool, error) {
if len(endpoints) == 0 {
return nil, errors.New("no client endpoints provided")
}
var (
pool = &ClientPool{
blockChan: make(chan model.Block),
poolManager: &rotationManager{},
listenAddrs: make([]btcutil.Address, 0),
txChan: make(chan model.Transaction),
unblockStart: make(chan struct{}, 1),
}
manager, err = newRotationManager(endpoints, proxyDialer)
)
if err != nil {
return nil, err
}
pool.poolManager = manager
return pool, nil
}
// Start will attempt to connect to the first available server. If it fails to
// connect it will rotate through the servers to try to find one that works.
func (p *ClientPool) Start() error {
go p.run()
return nil
}
func (p *ClientPool) Clients() []*blockbook.BlockBookClient {
var clients []*blockbook.BlockBookClient
for _, c := range p.poolManager.clientCache {
clients = append(clients, c)
}
return clients
}
func (p *ClientPool) run() {
for {
select {
case <-p.unblockStart:
return
default:
p.runLoop()
}
}
}
func (p *ClientPool) runLoop() error {
p.poolManager.SelectNext()
var closeChan = make(chan error, 0)
defer close(closeChan)
if err := p.poolManager.StartCurrent(closeChan); err != nil {
Log.Errorf("error starting %s: %s", p.poolManager.currentTarget, err.Error())
p.poolManager.FailCurrent()
p.poolManager.CloseCurrent()
return err
}
var ctx context.Context
ctx, p.cancelListenChan = context.WithCancel(context.Background())
go p.listenChans(ctx)
defer p.stopWebsocketListening()
p.replayListenAddresses()
if err := <-closeChan; err != nil {
p.poolManager.FailCurrent()
p.poolManager.CloseCurrent()
}
return nil
}
// Close proxies the same request to the active client
func (p *ClientPool) Close() {
p.stopWebsocketListening()
p.unblockStart <- struct{}{}
p.poolManager.CloseCurrent()
}
// PoolManager returns the pool manager object
func (p *ClientPool) PoolManager() *rotationManager {
return p.poolManager
}
// FailAndCloseCurrentClient cleans up the active client's connections, and
// signals to the rotation manager that it is unhealthy. The internal runLoop
// will detect the client's closing and attempt to start the next available.
func (p *ClientPool) FailAndCloseCurrentClient() {
p.stopWebsocketListening()
p.poolManager.FailCurrent()
p.poolManager.CloseCurrent()
}
func (p *ClientPool) stopWebsocketListening() {
if p.cancelListenChan != nil {
p.cancelListenChan()
p.cancelListenChan = nil
}
}
// listenChans proxies the block and tx chans from the client to the ClientPool's channels
func (p *ClientPool) listenChans(ctx context.Context) {
var (
client = p.poolManager.AcquireCurrent()
blockChan = client.BlockChannel()
txChan = client.TxChannel()
)
defer p.poolManager.ReleaseCurrent()
go func() {
for {
select {
case block := <-blockChan:
p.blockChan <- block
case tx := <-txChan:
p.txChan <- tx
case <-ctx.Done():
return
}
}
}()
}
// executeRequest handles making the HTTP request and responding with rotating the
// pool and/or retrying requests. As all requests travel through this method, without
// middleware intercepting error responses, all returned errors are checked to see if
// they are Retryable and/or Fatal as defined by client/errors. These error properties
// can be composed like client/errors.MakeFatal(client/errors.MakeRetryable(err)).
// This approach should allow individual requests to define how the resulting error
// should be handled upstream of the request.
func (p *ClientPool) executeRequest(queryFunc func(c *blockbook.BlockBookClient) error) error {
var err error
for e := p.newMaximumTryEnumerator(); e.next(); {
var client = p.poolManager.AcquireCurrentWhenReady()
if err = queryFunc(client); err != nil {
p.poolManager.ReleaseCurrent()
if clientErr.IsFatal(err) || e.isFinal() {
Log.Warningf("rotating server due to fatal or exhausted attempts")
p.FailAndCloseCurrentClient()
}
if !clientErr.IsRetryable(err) {
Log.Errorf("unretryable error: %s", err.Error())
return err
}
if !e.isFinal() {
Log.Warningf("retrying due to error: %s", err.Error())
}
continue
} else {
p.poolManager.ReleaseCurrent()
return nil
}
}
Log.Errorf("exhausted retry attempts, last error: %s", err.Error())
return fmt.Errorf("request failed: %s", err.Error())
}
// BlockNofity proxies the active client's block channel
func (p *ClientPool) BlockNotify() <-chan model.Block {
return p.blockChan
}
// Broadcast proxies the same request to the active client
func (p *ClientPool) Broadcast(tx []byte) (string, error) {
var (
txid string
queryFunc = func(c *blockbook.BlockBookClient) error {
Log.Debugf("(%s) broadcasting transaction", c.EndpointURL().String())
r, err := c.Broadcast(tx)
if err != nil {
return err
}
txid = r
return nil
}
)
err := p.executeRequest(queryFunc)
return txid, err
}
// EstimateFee proxies the same request to the active client
func (p *ClientPool) EstimateFee(nBlocks int) (int, error) {
var (
fee int
queryFunc = func(c *blockbook.BlockBookClient) error {
Log.Debugf("(%s) requesting fee estimate", c.EndpointURL().String())
r, err := c.EstimateFee(nBlocks)
if err != nil {
return clientErr.MakeRetryable(err)
}
fee = r
return nil
}
)
err := p.executeRequest(queryFunc)
return fee, err
}
// GetBestBlock proxies the same request to the active client
func (p *ClientPool) GetBestBlock() (*model.Block, error) {
var (
block *model.Block
queryFunc = func(c *blockbook.BlockBookClient) error {
Log.Debugf("(%s) request best block info", c.EndpointURL().String())
r, err := c.GetBestBlock()
if err != nil {
return clientErr.MakeRetryable(err)
}
block = r
return err
}
)
err := p.executeRequest(queryFunc)
return block, err
}
// GetInfo proxies the same request to the active client
func (p *ClientPool) GetInfo() (*model.Info, error) {
var (
info *model.Info
queryFunc = func(c *blockbook.BlockBookClient) error {
Log.Debugf("(%s) request backend info", c.EndpointURL().String())
r, err := c.GetInfo()
if err != nil {
return clientErr.MakeRetryable(err)
}
info = r
return nil
}
)
err := p.executeRequest(queryFunc)
return info, err
}
// GetRawTransaction proxies the same request to the active client
func (p *ClientPool) GetRawTransaction(txid string) ([]byte, error) {
var (
tx []byte
queryFunc = func(c *blockbook.BlockBookClient) error {
Log.Debugf("(%s) request transaction info, txid: %s", c.EndpointURL().String(), txid)
r, err := c.GetRawTransaction(txid)
if err != nil {
return err
}
tx = r
return nil
}
)
err := p.executeRequest(queryFunc)
return tx, err
}
// GetTransactions proxies the same request to the active client
func (p *ClientPool) GetTransactions(addrs []btcutil.Address) ([]model.Transaction, error) {
var (
txs []model.Transaction
queryFunc = func(c *blockbook.BlockBookClient) error {
var addrStrings []string
for _, a := range addrs {
addrStrings = append(addrStrings, a.String())
}
Log.Debugf("(%s) request transactions for (%d) addrs", c.EndpointURL().String(), len(addrs))
Log.Debugf("\taddrs requested: %s", strings.Join(addrStrings, ","))
r, err := c.GetTransactions(addrs)
if err != nil {
return err
}
txs = r
return nil
}
)
err := p.executeRequest(queryFunc)
return txs, err
}
// GetTransaction proxies the same request to the active client
func (p *ClientPool) GetTransaction(txid string) (*model.Transaction, error) {
var (
tx *model.Transaction
queryFunc = func(c *blockbook.BlockBookClient) error {
Log.Debugf("(%s) request transaction data, txid: %s", c.EndpointURL().String(), txid)
r, err := c.GetTransaction(txid)
if err != nil {
return err
}
tx = r
return nil
}
)
err := p.executeRequest(queryFunc)
return tx, err
}
// GetUtxos proxies the same request to the active client
func (p *ClientPool) GetUtxos(addrs []btcutil.Address) ([]model.Utxo, error) {
var (
utxos []model.Utxo
queryFunc = func(c *blockbook.BlockBookClient) error {
var addrStrings []string
for _, a := range addrs {
addrStrings = append(addrStrings, a.String())
}
Log.Debugf("(%s) request utxos for (%d) addrs", c.EndpointURL().String(), len(addrs))
Log.Debugf("\taddrs requested: %s", strings.Join(addrStrings, ","))
r, err := c.GetUtxos(addrs)
if err != nil {
return err
}
utxos = r
return nil
}
)
err := p.executeRequest(queryFunc)
return utxos, err
}
// ListenAddresses proxies the same request to the active client
func (p *ClientPool) ListenAddresses(addrs ...btcutil.Address) {
p.listenAddrsLock.Lock()
defer p.listenAddrsLock.Unlock()
var client = p.poolManager.AcquireCurrentWhenReady()
defer p.poolManager.ReleaseCurrent()
p.listenAddrs = append(p.listenAddrs, addrs...)
client.ListenAddresses(addrs...)
}
func (p *ClientPool) replayListenAddresses() {
p.listenAddrsLock.Lock()
defer p.listenAddrsLock.Unlock()
var client = p.poolManager.AcquireCurrent()
defer p.poolManager.ReleaseCurrent()
client.ListenAddresses(p.listenAddrs...)
}
// TransactionNotify proxies the active client's tx channel
func (p *ClientPool) TransactionNotify() <-chan model.Transaction { return p.txChan }