-
Notifications
You must be signed in to change notification settings - Fork 27
/
provider.go
154 lines (125 loc) · 3.67 KB
/
provider.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
package bitshares
import (
"encoding/json"
"github.com/denkhaus/bitshares/api"
"github.com/denkhaus/logging"
"github.com/juju/errors"
deadlock "github.com/sasha-s/go-deadlock"
"github.com/tevino/abool"
)
type ClientProvider interface {
OnError(fn api.ErrorFunc)
Connect() error
Subscribe(apiID int, method string, fn api.SubscribeCallback, args ...interface{}) (*json.RawMessage, error)
CallAPI(apiID int, method string, args ...interface{}) (*json.RawMessage, error)
Close() error
}
type SimpleClientProvider struct {
api.WebsocketClient
api WebsocketAPI
}
func NewSimpleClientProvider(endpointURL string, ws WebsocketAPI) ClientProvider {
wsc := api.NewWebsocketClient(endpointURL)
sim := SimpleClientProvider{
api: ws,
WebsocketClient: wsc,
}
return &sim
}
func (p *SimpleClientProvider) CallAPI(apiID int, method string, args ...interface{}) (*json.RawMessage, error) {
if !p.WebsocketClient.IsConnected() {
if err := p.api.Connect(); err != nil {
return nil, errors.Annotate(err, "Connect [api]")
}
}
rawMessage, err := p.WebsocketClient.CallAPI(apiID, method, args...)
if err != nil {
return nil, errors.Annotate(err, "call api error")
}
if rawMessage == nil {
return nil, errors.New("raw message returned is nil")
}
return rawMessage, err
}
type BestNodeClientProvider struct {
api.WebsocketClient
mu deadlock.RWMutex
nodeChanged *abool.AtomicBool
api WebsocketAPI
tester api.LatencyTester
}
func NewBestNodeClientProvider(endpointURL string, ws WebsocketAPI) (ClientProvider, error) {
tester, err := api.NewLatencyTester(endpointURL)
if err != nil {
return nil, errors.Annotate(err, "NewLatencyTester")
}
pr := &BestNodeClientProvider{
api: ws,
tester: tester,
nodeChanged: abool.NewBool(false),
WebsocketClient: tester.TopNodeClient(),
}
tester.OnTopNodeChanged(pr.onTopNodeChanged)
tester.Start()
return pr, nil
}
func (p *BestNodeClientProvider) onTopNodeChanged(newEndpoint string) error {
logging.Infof("change top node client -> %s\n", newEndpoint)
p.nodeChanged.Set()
return nil
}
func (p *BestNodeClientProvider) renewClient() error {
p.mu.Lock()
defer p.mu.Unlock()
if p.WebsocketClient.IsConnected() {
logging.Debug("close [client]")
if err := p.WebsocketClient.Close(); err != nil {
return errors.Annotate(err, "Close [client]")
}
}
p.WebsocketClient = p.tester.TopNodeClient()
return nil
}
func (p *BestNodeClientProvider) handleReconnect() error {
if err := p.renewClient(); err != nil {
return errors.Annotate(err, "renewClient")
}
logging.Debug("reconnect api")
if err := p.api.Connect(); err != nil {
return errors.Annotate(err, "Connect [api]")
}
return nil
}
func (p *BestNodeClientProvider) needsReconnect() bool {
return p.nodeChanged.IsSet() ||
!p.WebsocketClient.IsConnected()
}
func (p *BestNodeClientProvider) CallAPI(apiID int, method string, args ...interface{}) (*json.RawMessage, error) {
//ensure reliable connection
if p.needsReconnect() {
// either way unsignal nodeChanged
p.nodeChanged.UnSet()
if err := p.handleReconnect(); err != nil {
return nil, errors.Annotate(err, "handleReconnect")
}
}
p.mu.RLock()
defer p.mu.RUnlock()
return p.WebsocketClient.CallAPI(apiID, method, args...)
}
func (p *BestNodeClientProvider) Close() error {
p.mu.Lock()
defer p.mu.Unlock()
logging.Debug("close provider")
if p.WebsocketClient.IsConnected() {
logging.Debug("close [client]")
if err := p.WebsocketClient.Close(); err != nil {
return errors.Annotate(err, "Close [client]")
}
}
logging.Debug("close [tester]")
if err := p.tester.Close(); err != nil {
return errors.Annotate(err, "Close [tester]")
}
return nil
}