forked from bitfinexcom/bitfinex-api-go
/
transport.go
213 lines (194 loc) · 4.71 KB
/
transport.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
package websocket
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/op/go-logging"
"net"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
)
// size of channel that the websocket writer
// routine pulls from
const WS_WRITE_CAPACITY = 5000
// size of channel that the websocket reader
// routine pushes websocket updates into
const WS_READ_CAPACITY = 10
// seconds to wait in between re-sending
// the keep alive ping
const KEEP_ALIVE_TIMEOUT = 10
func newWs(baseURL string, logTransport bool, log *logging.Logger) *ws {
return &ws{
BaseURL: baseURL,
downstream: make(chan []byte, WS_READ_CAPACITY),
quit: make(chan error),
kill: make(chan interface{}),
logTransport: logTransport,
log: log,
lock: &sync.RWMutex{},
createTime: time.Now(),
writeChan: make(chan []byte, WS_WRITE_CAPACITY),
}
}
type ws struct {
ws *websocket.Conn
lock *sync.RWMutex
BaseURL string
TLSSkipVerify bool
downstream chan []byte
logTransport bool
log *logging.Logger
createTime time.Time
writeChan chan []byte
kill chan interface{} // signal to routines to kill
quit chan error // signal to parent with error, if applicable
}
func (w *ws) Connect() error {
if w.ws != nil {
return nil // no op
}
var d = websocket.Dialer{
Subprotocols: []string{"p1", "p2"},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: time.Second * 10,
}
d.TLSClientConfig = &tls.Config{InsecureSkipVerify: w.TLSSkipVerify}
w.log.Infof("connecting ws to %s", w.BaseURL)
ws, resp, err := d.Dial(w.BaseURL, nil)
if err != nil {
if err == websocket.ErrBadHandshake {
w.log.Errorf("bad handshake: status code %d", resp.StatusCode)
}
return err
}
w.ws = ws
go w.listenWriteChannel()
go w.listenWs()
// Gorilla/go dont natively support keep alive pinging
// so we need to keep sending a message down the channel to stop
// tcp killing the connection
go w.keepAlivePinger()
return nil
}
func (w *ws) keepAlivePinger() {
for {
pingTimer := time.After(time.Second * KEEP_ALIVE_TIMEOUT)
select {
case <-w.kill:
return
case <-pingTimer:
w.writeChan <- []byte("ping")
}
}
}
// Send marshals the given interface and then sends it to the API. This method
// can block so specify a context with timeout if you don't want to wait for too
// long.
func (w *ws) Send(ctx context.Context, msg interface{}) error {
bs, err := json.Marshal(msg)
if err != nil {
return err
}
select {
case <- ctx.Done():
return ctx.Err()
case <- w.kill: // ws closed
return fmt.Errorf("websocket connection closed")
default:
}
w.log.Debug("ws->srv: %s", string(bs))
// push request into writer channel
w.writeChan <- bs
return nil
}
func (w *ws) Done() <-chan error {
return w.quit
}
// listen for write requests and perform them
func (w *ws) listenWriteChannel() {
for {
select {
case <-w.kill: // ws closed
return
case message := <- w.writeChan:
wsWriter, err := w.ws.NextWriter(websocket.TextMessage)
if err != nil {
w.log.Error("Unable to provision ws connection writer: ", err)
w.stop(err)
return
}
_, err = wsWriter.Write(message)
if err != nil {
w.log.Error("Unable to write to ws: ", err)
w.stop(err)
return
}
if err := wsWriter.Close(); err != nil {
w.log.Error("Unable to close ws connection writer: ", err)
w.stop(err)
return
}
}
}
}
// listen on ws & fwd to listen()
func (w *ws) listenWs() {
for {
if w.ws == nil {
return
}
select {
case <-w.kill: // ws connection ended
return
default:
_, msg, err := w.ws.ReadMessage()
if err != nil {
if cl, ok := err.(*websocket.CloseError); ok {
w.log.Errorf("close error code: %d", cl.Code)
}
// a read during normal shutdown results in an OpError: op on closed connection
if _, ok := err.(*net.OpError); ok {
// general read error on a closed network connection, OK
return
}
w.stop(err)
return
}
w.log.Debugf("srv->ws: %s", string(msg))
w.lock.RLock()
if w.downstream == nil {
w.lock.RUnlock()
return
}
w.downstream <- msg
w.lock.RUnlock()
}
}
}
func (w *ws) Listen() <-chan []byte {
return w.downstream
}
func (w *ws) stop(err error) {
w.lock.Lock()
defer w.lock.Unlock()
if w.ws != nil {
close(w.kill)
w.quit <- err // pass error back
close(w.quit) // signal to parent listeners
close(w.downstream)
w.downstream = nil
if err := w.ws.Close(); err != nil {
w.log.Error(fmt.Errorf("error closing websocket: %s", err))
}
w.ws = nil
}
}
// Close the websocket connection
func (w *ws) Close() {
w.stop(fmt.Errorf("transport connection Close called"))
}