-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.go
168 lines (143 loc) · 3.72 KB
/
ws.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
package apic
import (
"context"
"time"
"nhooyr.io/websocket"
)
type WSClient struct {
// endpoint is the server endpoint
endpoint string
// logger infos connection lifecycles, and debugs each message sent and received
logger Logger
// conn is the (current) underlying connection
conn *websocket.Conn
// handler is the global message handler
handler func([]byte) error
// onOpen is the callback invoked after each connection is opened
onOpen func(*WSClient) error
// onClose is the callback invoked after each connection is closed
onClose func(*WSClient) error
encoder Encoder
pingInterval time.Duration
shouldReconnect reconnectPolicy
}
func NewWSClient(endpoint string, opts ...WSOption) *WSClient {
w := &WSClient{
logger: noLogger{},
endpoint: endpoint,
encoder: defaultEncoder,
handler: func(_ []byte) error { return nil },
onOpen: func(_ *WSClient) error { return nil },
onClose: func(_ *WSClient) error { return nil },
shouldReconnect: func(_ error) bool { return false },
}
for _, opt := range opts {
opt(w)
}
return w
}
// Start runs the client until either:
// - the context is canceled
// - the reconnect policy returns false
func (c *WSClient) Start(ctx context.Context) error {
for {
err := c.run(ctx)
c.logger.Info("disconnected", "error", err)
if !c.shouldReconnect(err) {
return err
}
c.logger.Info("reconnecting...")
}
}
// Write encodes and writes an object to the current connection.
func (c *WSClient) Write(ctx context.Context, obj any) error {
bts, err := c.encoder(obj)
if err != nil {
return err
}
c.logger.Debug("send", "message", string(bts))
if ctx == nil {
ctx = context.Background()
}
return c.conn.Write(ctx, websocket.MessageText, bts)
}
// run connects the websocket, and runs the single connection until
// either the connection is terminated, or the global handler returns
// a non nil error.
func (c *WSClient) run(ctx context.Context) error {
if err := c.connect(ctx); err != nil {
return err
}
c.logger.Info("connected")
defer c.conn.Close(websocket.StatusInternalError, "app closing")
readErr := make(chan error)
data := make(chan []byte)
go reader(c.conn, data, readErr)
if err := c.onOpen(c); err != nil {
return err
}
defer func() {
if err := c.onClose(c); err != nil {
c.logger.Info("onClose returned error", "error", err.Error())
}
}()
c.logger.Info("starting")
pings := make(chan struct{})
defer close(pings)
if c.pingInterval != 0 {
go func() {
t := time.NewTicker(c.pingInterval)
defer t.Stop()
for {
<-t.C
pings <- struct{}{}
}
}()
}
for {
select {
case bts := <-data:
c.logger.Debug("recv", "message", string(bts))
if err := c.handler(bts); err != nil {
return err
}
case err := <-readErr:
return err
case <-pings:
if err := c.conn.Ping(ctx); err != nil {
return err
}
case <-ctx.Done():
return nil
}
}
}
// connect creates a new connection and assigns it
// to the receiver
// TODO: the threadsafety thing, across the whole client
func (c *WSClient) connect(ctx context.Context) error {
conn, _, err := websocket.Dial(ctx, c.endpoint, nil)
if err != nil {
return err
}
conn.SetReadLimit(-1) // that's just like, my opinion or whatever
c.conn = conn
return nil
}
// reader is a helper func to pump messages from a connection
func reader(conn *websocket.Conn, data chan []byte, errs chan error) {
defer close(data)
defer close(errs)
for {
_, bts, err := conn.Read(context.Background())
if err != nil {
errs <- err
return
}
data <- bts
}
}
// reconnectPolicy configures reconnect behavior.
// if the function returns true, the client will
// attempt a reconnect.
type reconnectPolicy func(error) bool