/
websocket.go
274 lines (211 loc) · 5.67 KB
/
websocket.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
// Copyright 2022 Fluidity Money. All rights reserved. Use of this
// source code is governed by a GPL-style license that can be found in the
// LICENSE.md file.
package websocket
// websocket supports an easy-to-use websocket server and series of broadcast
// primitives.
import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/fluidity-money/fluidity-app/lib/log"
"github.com/fluidity-money/fluidity-app/lib/web"
"github.com/gorilla/websocket"
)
// Context to use when logging
const Context = `WEBSERVER/WEBSOCKET`
// DeadlinePong to send after a ping request in the control message
const DeadlinePong = 3 * time.Minute
// websocketUpgrader used in every endpoint in this codebase.
var websocketUpgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// Endpoint handles a HTTP request and upgrades it, giving the user a
// channel to receive messages down and a reply channel to send messages
// to the websocket.
func Endpoint(endpoint string, handler func(string, url.Values, <-chan []byte, chan<- []byte, chan<- error, <-chan bool)) {
http.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
ipAddress := r.Header.Get(web.HeaderIpAddress)
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Upgrading IP %v to a websocket!"
k.Payload = ipAddress
})
websocketConn, err := websocketUpgrader.Upgrade(w, r, nil)
if err != nil {
err_ := fmt.Errorf(
"Failed to handle a websocket upgrade! %#v",
err,
)
log.App(func(k *log.Log) {
k.Context = Context
k.Format(
"Websocket upgrade for IP %#v failed!",
ipAddress,
)
k.Payload = err_
})
return
}
defer websocketConn.Close()
var (
messages = make(chan []byte, 1)
replies = make(chan []byte, 1)
chanShutdownWriter = make(chan bool)
chanHandlerRequestShutdown = make(chan error)
chanHandlerShutdown = make(chan bool)
chanPongs = make(chan bool)
)
go func() {
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Beginning to read messages!"
k.Payload = ipAddress
})
for {
msgType, content, err := websocketConn.ReadMessage()
switch msgType {
case websocket.PongMessage:
// just return, we ignore these
return
case websocket.TextMessage,
websocket.BinaryMessage:
// log that we received this message, then continue with
// the rest of the code (we handle this normally)
log.Debug(func(k *log.Log) {
k.Context = Context
k.Format(
"Received this message from the queue, ip %v: %#v",
ipAddress,
string(content),
)
})
case websocket.PingMessage:
// need to handle an incoming ping!
// send out a request to do so and jump out!
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Incoming ping was received! Going to send out a pong."
k.Payload = ipAddress
})
chanPongs <- true
return
}
if err != nil {
log.App(func(k *log.Log) {
k.Context = Context
k.Format(
"Failed to read message from ip %#v websocket!",
ipAddress,
)
k.Payload = err
})
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "After an error with IP %#v, sending a message to chanHandlerShutdown..."
k.Payload = ipAddress
})
chanHandlerShutdown <- true
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "After an error, sending a message to chanShutdownWriter..."
k.Payload = ipAddress
})
chanShutdownWriter <- true
return
}
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Sending a mesage to the replies channel!"
k.Payload = ipAddress
})
replies <- content
}
}()
go func() {
for {
select {
case _ = <-chanShutdownWriter:
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Received a message to shut down the writer!"
k.Payload = ipAddress
})
return
case <-chanPongs:
deadline := time.Now().Add(DeadlinePong)
log.Debug(func(k *log.Log) {
k.Context = Context
k.Format(
"Sending out a pong in response to a ping, deadline %v!",
deadline,
)
k.Payload = ipAddress
})
err := websocketConn.WriteControl(
websocket.PongMessage,
nil,
deadline,
)
if err != nil {
log.App(func(k *log.Log) {
k.Context = Context
k.Format(
"Failed to write a pong message to IP %#v websocket!",
ipAddress,
)
k.Payload = err
})
chanHandlerShutdown <- true
return
}
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Wrote a pong message to the websocket!"
k.Payload = ipAddress
})
case message := <-messages:
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Received a message to write!"
k.Payload = ipAddress
})
err := websocketConn.WriteMessage(
websocket.TextMessage,
message,
)
if err != nil {
log.App(func(k *log.Log) {
k.Context = Context
k.Format(
"Failed to write a message to IP %#v websocket!",
ipAddress,
)
k.Payload = err
})
chanHandlerShutdown <- true
return
}
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Wrote a message to the websocket!"
k.Payload = ipAddress
})
}
}
}()
handler(
ipAddress,
r.URL.Query(),
replies,
messages,
chanHandlerRequestShutdown,
chanHandlerShutdown,
)
})
}