/
connect.go
221 lines (189 loc) · 4.28 KB
/
connect.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
package connect
import (
"context"
"fmt"
"log"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
)
const host = "wss://api.coin.z.com/ws/public/v1"
type connectionState string
const (
connectionStateConnecting = connectionState("Connecting")
connectionStateConnected = connectionState("Connected")
connectionStateClosed = connectionState("Closed")
)
// Connection ...
type Connection struct {
sync.Mutex
conn *websocket.Conn
state *atomic.Value
ctx context.Context
stopFunc context.CancelFunc
subscribeFunc func() error
unsubscribeFunc func() error
stream chan []byte
msgStream chan msgRequest
}
type msgRequest struct {
msg interface{}
errChan chan error
}
// New is...
func New() *Connection {
ctx, cancelFunc := context.WithCancel(context.Background())
conn := &Connection{
state: &atomic.Value{},
ctx: ctx,
stopFunc: cancelFunc,
stream: make(chan []byte, 100),
msgStream: make(chan msgRequest, 1),
}
conn.state.Store(connectionStateClosed)
go conn.send()
go conn.receive()
return conn
}
// SetSubscribeFunc ...
func (c *Connection) SetSubscribeFunc(f func() interface{}) {
c.subscribeFunc = c.createSendFunc(f)
}
// SetUnsubscribeFunc ...
func (c *Connection) SetUnsubscribeFunc(f func() interface{}) {
c.unsubscribeFunc = c.createSendFunc(f)
}
// Subscribe ...
func (c *Connection) Subscribe() error {
return c.subscribeFunc()
}
// Unsubscribe ...
func (c *Connection) Unsubscribe() error {
return c.unsubscribeFunc()
}
func (c *Connection) createSendFunc(f func() interface{}) func() error {
return func() error {
req := msgRequest{
msg: f(),
errChan: make(chan error),
}
c.msgStream <- req
return <-req.errChan
}
}
func (c *Connection) send() {
ctx, _ := context.WithCancel(c.ctx)
for {
select {
case m := <-c.msgStream:
e := c.Send(m.msg)
m.errChan <- e
case <-ctx.Done():
return
}
}
}
func (c *Connection) receive() {
defer func() {
if c.isConnected() {
_ = c.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
_ = c.conn.Close()
}
}()
for {
if !c.isConnected() {
if err := c.dial(); err != nil {
log.Println(err)
continue
}
e := c.subscribeFunc()
if e != nil {
log.Println(fmt.Sprintf("[Subscribe]error:%v", e))
_ = c.conn.Close()
c.state.Store(connectionStateClosed)
continue // TODO:review
}
}
_, msg, err := c.conn.ReadMessage()
if err != nil {
log.Println(fmt.Sprintf("[ReadMessage]error:%v", err))
_ = c.conn.Close()
c.state.Store(connectionStateClosed)
continue // TODO:review
}
c.stream <- msg
select {
case <-c.ctx.Done():
return
default:
}
}
}
func (c *Connection) isConnected() bool {
c.Lock()
defer c.Unlock()
v, ok := c.state.Load().(connectionState)
if !ok {
c.state.Store(connectionStateClosed)
return false
}
return v == connectionStateConnected || v == connectionStateConnecting
}
// Send is...
func (c *Connection) Send(msg interface{}) error {
err := c.waitForConnected()
if err != nil {
return err
}
err = c.conn.WriteJSON(msg)
if err != nil {
return fmt.Errorf("write error:%v", err)
}
log.Printf("[Send]msg:%+v", msg)
return nil
}
func (c *Connection) waitForConnected() error {
if c.isConnected() {
return nil
}
for i := 0; i < 10; i++ {
if c.isConnected() {
return nil
}
time.Sleep(time.Second)
}
return fmt.Errorf("connection timeout")
}
func (c *Connection) dial() error {
c.Lock()
defer c.Unlock()
log.Println("dial start")
if c.conn != nil {
c.state.Store(connectionStateClosed)
_ = c.conn.Close()
}
c.state.Store(connectionStateConnecting)
conn, res, err := websocket.DefaultDialer.Dial(host, nil)
if err != nil {
c.state.Store(connectionStateClosed)
return fmt.Errorf("dial error:%v, response:%v", err, res)
}
conn.SetReadLimit(10240)
_ = conn.SetReadDeadline(time.Now().Add(120 * time.Second))
conn.SetPongHandler(func(appData string) error {
_ = conn.SetReadDeadline(time.Now().Add(120 * time.Second))
return nil
})
c.conn = conn
c.state.Store(connectionStateConnected)
return nil
}
// Stream ...
func (c *Connection) Stream() <-chan []byte {
return c.stream
}
// Close is ...
func (c *Connection) Close() {
c.stopFunc()
}