-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.go
396 lines (353 loc) · 9.96 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
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
394
395
396
package websocket
import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"runtime"
"slices"
"sync"
"time"
"github.com/gorilla/websocket"
)
// DefaultSetupClient is an example implementation of a function that sets up a
// websocket connection.
func DefaultSetupConn(c *websocket.Conn) {
pw := 60 * time.Second
c.SetReadLimit(512)
c.SetReadDeadline(time.Now().Add(pw))
c.SetPongHandler(func(string) error {
c.SetReadDeadline(time.Now().Add(pw))
return nil
})
}
func DefaultUpgrader(origins []string) websocket.Upgrader {
upgrader := websocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: 1024}
upgrader.CheckOrigin = func(r *http.Request) bool {
return slices.Contains(origins, r.Header.Get("Origin"))
}
return upgrader
}
// Client is an interface for reading from and writing to a websocket
// connection. It is designed to be used as a middleman between a service and a
// client websocket connection.
type Client interface {
io.Writer
io.Closer
// WriteForever is responsible for writing messages to the client (including
// the regularly spaced ping messages)
WriteForever(context.Context, func(Client), time.Duration)
// ReadForever is responsible for reading messages from the client, and passing
// them to the message handlers
ReadForever(context.Context, func(Client), ...MessageHandler)
// SetLogger allows consumers to inject their own logging dependencies
SetLogger(any) error
// Log allows implementors to use their own logging dependencies
Log(int, string, ...any)
// Wait blocks until the client is done processing messages
Wait()
}
type MessageHandler func(Client, []byte)
// ServeWS upgrades HTTP connections to WebSocket, creates the Client, calls the
// onCreate callback, and starts goroutines that handle reading (writing)
// from (to) the client.
func ServeWS(
// upgrader upgrades the connection
upgrader websocket.Upgrader,
// connSetup is called on the upgraded WebSocket connection to configure
// the connection
connSetup func(*websocket.Conn),
// clientFactory is a function that takes a connection and returns a new Client
clientFactory func(*websocket.Conn) Client,
// onCreate is a function to call once the Client is created (e.g.,
// store it in a some collection on the service for later reference)
onCreate func(context.Context, context.CancelFunc, Client),
// onDestroy is a function to call after the WebSocket connection is closed
// (e.g., remove it from the collection on the service)
onDestroy func(Client),
// ping is the interval at which ping messages are aren't sent
ping time.Duration,
// msgHandlers are callbacks that handle messages received from the client
msgHandlers []MessageHandler,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
// if Upgrade fails it closes the connection, so just return
return
}
connSetup(conn)
client := clientFactory(conn)
ctx := context.Background()
ctx, cf := context.WithCancel(ctx)
onCreate(ctx, cf, client)
// all writes will happen in this goroutine, ensuring only one write on
// the connection at a time
go client.WriteForever(ctx, onDestroy, ping)
// all reads will happen in this goroutine, ensuring only one reader on
// the connection at a time
go client.ReadForever(ctx, onDestroy, msgHandlers...)
}
}
type client struct {
lock *sync.RWMutex
wg *sync.WaitGroup
conn *websocket.Conn
egress chan []byte
logger *slog.Logger
}
// NewClient returns a new Client from a *websocket.Conn. This can be passed to
// ServeWS as the client factory arg.
func NewClient(c *websocket.Conn) Client {
// add 2 to the wait group for the read/write goroutines
wg := &sync.WaitGroup{}
wg.Add(2)
return &client{
lock: &sync.RWMutex{},
wg: wg,
conn: c,
egress: make(chan []byte, 32),
logger: slog.New(slog.NewJSONHandler(os.Stdout, nil)),
}
}
// Write implements the Writer interface.
func (c *client) Write(p []byte) (int, error) {
c.egress <- p
return len(p), nil
}
// Close implements the Closer interface. Note the behavior of calling Close()
// multiple times is undefined; this implementation swallows all errors.
func (c *client) Close() error {
c.conn.WriteControl(websocket.CloseMessage, []byte{}, time.Time{})
c.conn.Close()
return nil
}
// WriteForever serially processes messages from the egress channel and writes them
// to the client, ensuring that all writes to the underlying connection are
// performed here.
func (c *client) WriteForever(ctx context.Context, onDestroy func(Client), ping time.Duration) {
pingTicker := time.NewTicker(ping)
defer func() {
c.wg.Done()
pingTicker.Stop()
onDestroy(c)
}()
for {
select {
case <-ctx.Done():
c.conn.WriteMessage(websocket.CloseMessage, nil)
return
case msgBytes, ok := <-c.egress:
// ok will be false in case the egress channel is closed
if !ok {
c.conn.WriteMessage(websocket.CloseMessage, nil)
return
}
// write a message to the connection
if err := c.conn.WriteMessage(websocket.TextMessage, msgBytes); err != nil {
c.Log(int(slog.LevelError), fmt.Sprintf("error writing message: %v", err))
return
}
case <-pingTicker.C:
if err := c.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
c.Log(int(slog.LevelError), fmt.Sprintf("error writing ping: %v", err))
return
}
}
}
}
// ReadForever serially processes messages from the client and passes them to the
// supplied message handlers in their own goroutine. Each message will be processed
// serially, but the handlers are executed concurrently.
func (c *client) ReadForever(ctx context.Context, onDestroy func(Client), handlers ...MessageHandler) {
defer func() {
c.wg.Done()
onDestroy(c)
}()
ingress := make(chan []byte)
errCancel := make(chan error)
loop := true
// read forever and push into ingress
go func() {
for {
_, payload, err := c.conn.ReadMessage()
if err != nil {
errCancel <- err
return
}
ingress <- payload
}
}()
// read while waiting for shutdown signals
for loop {
select {
case <-ctx.Done():
c.Log(0, "read loop cancelled, shutting down")
loop = false
case err := <-errCancel:
c.Log(0, "client connection closed in read loop")
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure, websocket.CloseNoStatusReceived) {
c.Log(0, "read loop encountered error, shutting down", "error", err.Error())
}
loop = false
case payload := <-ingress:
// handle the message so each message is processed serially (though note
// that handlers are called concurrently)
var wg sync.WaitGroup
wg.Add(len(handlers))
for _, h := range handlers {
go func(h func(Client, []byte)) {
h(c, payload)
wg.Done()
}(h)
}
wg.Wait()
}
}
}
func (c *client) SetLogger(v any) error {
l, ok := v.(*slog.Logger)
if !ok {
return fmt.Errorf("bad logger value supplied")
}
c.logger = l
return nil
}
func (c *client) Log(level int, s string, args ...any) {
_, f, l, ok := runtime.Caller(1)
if ok {
args = append(args, "caller_source", fmt.Sprintf("%s %d", f, l))
}
switch level {
case int(slog.LevelDebug):
c.logger.Debug(s, args...)
case int(slog.LevelInfo):
c.logger.Info(s, args...)
case int(slog.LevelWarn):
c.logger.Warn(s, args...)
case int(slog.LevelError):
c.logger.Error(s, args...)
}
}
// Done blocks until the read/write goroutines have completed
func (c *client) Wait() {
c.wg.Wait()
}
// Manager maintains a set of Clients.
type Manager interface {
Clients() []Client
RegisterClient(context.Context, context.CancelFunc, Client)
UnregisterClient(Client)
Run(context.Context)
}
type manager struct {
mu sync.RWMutex
clients map[Client]context.CancelFunc
register chan regreq
unregister chan regreq
}
type regreq struct {
context context.Context
cancel context.CancelFunc
client Client
done chan struct{}
}
func NewManager() Manager {
return &manager{
mu: sync.RWMutex{},
clients: make(map[Client]context.CancelFunc),
register: make(chan regreq),
unregister: make(chan regreq),
}
}
// Clients returns the currently managed Clients.
func (m *manager) Clients() []Client {
res := []Client{}
m.mu.RLock()
defer m.mu.RUnlock()
for c := range m.clients {
res = append(res, c)
}
return res
}
// RegisterClient adds the Client to the Manager's store.
func (m *manager) RegisterClient(ctx context.Context, cf context.CancelFunc, c Client) {
done := make(chan struct{})
rr := regreq{
context: ctx,
cancel: cf,
client: c,
done: done,
}
m.register <- rr
<-done
}
// UnregisterClient removes the Client from the Manager's store.
func (m *manager) UnregisterClient(c Client) {
done := make(chan struct{})
rr := regreq{
client: c,
done: done,
}
m.unregister <- rr
<-done
}
// Run runs in its own goroutine processing (un)registration requests.
func (m *manager) Run(ctx context.Context) {
// helper fn for cleaning up client
cleanupClient := func(c Client) {
cancel, ok := m.clients[c]
if ok {
cancel()
}
delete(m.clients, c)
c.Close()
}
for {
select {
case <-ctx.Done():
m.mu.Lock()
for client := range m.clients {
cleanupClient(client)
}
m.mu.Unlock()
case rr := <-m.register:
m.mu.Lock()
m.clients[rr.client] = rr.cancel
m.mu.Unlock()
rr.done <- struct{}{}
case rr := <-m.unregister:
m.mu.Lock()
if _, ok := m.clients[rr.client]; ok {
cleanupClient(rr.client)
}
m.mu.Unlock()
rr.done <- struct{}{}
}
}
}
// Broadcaster is an example implementation of Manager that has a
// Broadcast method that writes the supplied message to all clients.
type Broadcaster struct {
*manager
}
func NewBroadcaster() Manager {
m := manager{
mu: sync.RWMutex{},
clients: make(map[Client]context.CancelFunc),
register: make(chan regreq),
unregister: make(chan regreq),
}
return &Broadcaster{
manager: &m,
}
}
func (bb *Broadcaster) Broadcast(b []byte) {
bb.mu.RLock()
defer bb.mu.RUnlock()
for w := range bb.clients {
w.Write(b)
}
}