forked from coder/websocket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat.go
294 lines (254 loc) · 8.47 KB
/
chat.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
package main
import (
"context"
"errors"
"io/ioutil"
"log"
"net/http"
"sync"
"time"
"strings"
"golang.org/x/time/rate"
"nhooyr.io/websocket"
)
// chatServer enables broadcasting to a set of subscribers.
type chatServer struct {
// subscriberMessageBuffer controls the max number
// of messages that can be queued for a subscriber
// before it is kicked.
//
// Defaults to 16.
subscriberMessageBuffer int
// publishLimiter controls the rate limit applied to the publish endpoint.
//
// Defaults to one publish every 100ms with a burst of 8.
publishLimiter *rate.Limiter
// logf controls where logs are sent.
// Defaults to log.Printf.
logf func(f string, v ...interface{})
// serveMux routes the various endpoints to the appropriate handler.
serveMux http.ServeMux
subscribersMu sync.Mutex
subscribers map[*subscriber]struct{}
roomsMu sync.Mutex
rooms map[string]map[*subscriber]struct{}
}
// newChatServer constructs a chatServer with the defaults.
func newChatServer() *chatServer {
cs := &chatServer{
subscriberMessageBuffer: 16,
logf: log.Printf,
subscribers: make(map[*subscriber]struct{}),
rooms: make(map[string]map[*subscriber]struct{}),
publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
}
cs.serveMux.Handle("/", http.FileServer(http.Dir(".")))
cs.serveMux.HandleFunc("/subscribe", cs.subscribeHandler)
cs.serveMux.HandleFunc("/subscribe/", cs.subscribeHandler)
cs.serveMux.HandleFunc("/publish", cs.publishHandler)
cs.serveMux.HandleFunc("/publish/", cs.publishHandler)
return cs
}
// subscriber represents a subscriber.
// Messages are sent on the msgs channel and if the client
// cannot keep up with the messages, closeSlow is called.
type subscriber struct {
msgs chan []byte
closeSlow func()
}
func (cs *chatServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cs.serveMux.ServeHTTP(w, r)
}
// subscribeHandler accepts the WebSocket connection and then subscribes
// it to all future messages.
func (cs *chatServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r,
&websocket.AcceptOptions{ OriginPatterns: []string{"*"},
})
if err != nil {
cs.logf("%v", err)
return
}
defer c.Close(websocket.StatusInternalError, "")
room := strings.Split(r.URL.Path, "/")
if len(room) == 3 {
cs.logf("%v", room[2])
err = cs.subscribeRoom(r.Context(), c, room[2])
} else {
err = cs.subscribe(r.Context(), c)
}
if errors.Is(err, context.Canceled) {
return
}
if websocket.CloseStatus(err) == websocket.StatusNormalClosure ||
websocket.CloseStatus(err) == websocket.StatusGoingAway {
return
}
if err != nil {
cs.logf("%v", err)
return
}
}
// publishHandler reads the request body with a limit of 8192 bytes and then publishes
// the received message.
func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
body := http.MaxBytesReader(w, r.Body, 8192)
msg, err := ioutil.ReadAll(body)
if err != nil {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return
}
cs.publish(msg)
room := strings.Split(r.URL.Path, "/")
if len(room) == 3 {
cs.publishRoom(msg, room[2])
} else {
cs.publishRooms(msg)
}
w.WriteHeader(http.StatusAccepted)
}
// subscribe subscribes the given WebSocket to all broadcast messages.
// It creates a subscriber with a buffered msgs chan to give some room to slower
// connections and then registers the subscriber. It then listens for all messages
// and writes them to the WebSocket. If the context is cancelled or
// an error occurs, it returns and deletes the subscription.
//
// It uses CloseRead to keep reading from the connection to process control
// messages and cancel the context if the connection drops.
func (cs *chatServer) subscribe(ctx context.Context, c *websocket.Conn) error {
ctx = c.CloseRead(ctx)
s := &subscriber{
msgs: make(chan []byte, cs.subscriberMessageBuffer),
closeSlow: func() {
c.Close(websocket.StatusPolicyViolation, "connection too slow to keep up with messages")
},
}
cs.addSubscriber(s)
defer cs.deleteSubscriber(s)
for {
select {
case msg := <-s.msgs:
err := writeTimeout(ctx, time.Second*5, c, msg)
if err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// subscribe subscribes the given WebSocket to all broadcast messages.
// It creates a subscriber with a buffered msgs chan to give some room to slower
// connections and then registers the subscriber. It then listens for all messages
// and writes them to the WebSocket. If the context is cancelled or
// an error occurs, it returns and deletes the subscription.
//
// It uses CloseRead to keep reading from the connection to process control
// messages and cancel the context if the connection drops.
func (cs *chatServer) subscribeRoom(ctx context.Context, c *websocket.Conn, room string) error {
ctx = c.CloseRead(ctx)
s := &subscriber{
msgs: make(chan []byte, cs.subscriberMessageBuffer),
closeSlow: func() {
c.Close(websocket.StatusPolicyViolation, "connection too slow to keep up with messages")
},
}
cs.addRoomSubscriber(s, room)
defer cs.deleteRoomSubscriber(s, room)
for {
select {
case msg := <-s.msgs:
err := writeTimeout(ctx, time.Second*5, c, msg)
if err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// publish publishes the msg to all subscribers.
// It never blocks and so messages to slow subscribers
// are dropped.
func (cs *chatServer) publish(msg []byte) {
cs.subscribersMu.Lock()
defer cs.subscribersMu.Unlock()
cs.publishLimiter.Wait(context.Background())
for s := range cs.subscribers {
select {
case s.msgs <- msg:
default:
go s.closeSlow()
}
}
}
// publish publishes the msg to all subscribers in a single room.
// It never blocks and so messages to slow subscribers
// are dropped.
func (cs *chatServer) publishRoom(msg []byte, room string) {
cs.roomsMu.Lock()
defer cs.roomsMu.Unlock()
cs.publishLimiter.Wait(context.Background())
for s := range cs.rooms[room] {
select {
case s.msgs <- msg:
default:
go s.closeSlow()
}
}
}
// publish publishes the msg to all subscribers in all rooms
// It never blocks and so messages to slow subscribers
// are dropped.
func (cs *chatServer) publishRooms(msg []byte) {
cs.roomsMu.Lock()
defer cs.roomsMu.Unlock()
cs.publishLimiter.Wait(context.Background())
for k := range cs.rooms {
for s := range cs.rooms[k] {
select {
case s.msgs <- msg:
default:
go s.closeSlow()
}
}
}
}
// addSubscriber registers a subscriber.
func (cs *chatServer) addSubscriber(s *subscriber) {
cs.subscribersMu.Lock()
cs.subscribers[s] = struct{}{}
cs.subscribersMu.Unlock()
}
// addSubscriber registers a subscriber to a room.
func (cs *chatServer) addRoomSubscriber(s *subscriber, room string) {
cs.roomsMu.Lock()
if len(cs.rooms[room]) == 0 {
cs.rooms[room] = make(map[*subscriber]struct{})
}
cs.rooms[room][s] = struct{}{}
cs.roomsMu.Unlock()
}
// deleteSubscriber deletes the given subscriber.
func (cs *chatServer) deleteSubscriber(s *subscriber) {
cs.subscribersMu.Lock()
delete(cs.subscribers, s)
cs.subscribersMu.Unlock()
}
// deleteSubscriber deletes the given subscriber.
func (cs *chatServer) deleteRoomSubscriber(s *subscriber, room string) {
if len(cs.rooms[room]) != 0 {
cs.roomsMu.Lock()
delete(cs.rooms[room], s)
cs.roomsMu.Unlock()
}
}
func writeTimeout(ctx context.Context, timeout time.Duration, c *websocket.Conn, msg []byte) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return c.Write(ctx, websocket.MessageText, msg)
}