Disable WebSocket compression and see if it helps with Safari

This commit is contained in:
Noah 2023-03-31 12:36:49 -07:00
parent 541dd917c5
commit f3354f2f1c

View File

@ -30,7 +30,8 @@ type Subscriber struct {
conn *websocket.Conn
ctx context.Context
cancel context.CancelFunc
messages chan []byte
messages chan []byte // WebSocket outgoing queue
cooldown sync.Mutex // don't spam messages out the WS
closeSlow func()
muteMu sync.RWMutex
@ -113,7 +114,12 @@ func (sub *Subscriber) SendJSON(v interface{}) error {
return err
}
log.Debug("SendJSON(%d=%s): %s", sub.ID, sub.Username, data)
return sub.conn.Write(sub.ctx, websocket.MessageText, data)
// Write it to their outgoing channel so we can rate limit in case of Safari browsers
// which get overwhelmed with too many messages.
sub.messages <- data
// return sub.conn.Write(sub.ctx, websocket.MessageText, data)
return nil
}
// SendMe sends the current user state to the client.
@ -140,7 +146,9 @@ func (s *Server) WebSocket() http.HandlerFunc {
ip := util.IPAddress(r)
log.Info("WebSocket connection from %s - %s", ip, r.Header.Get("User-Agent"))
log.Debug("Headers: %+v", r.Header)
c, err := websocket.Accept(w, r, nil)
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
CompressionMode: websocket.CompressionDisabled,
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Could not accept websocket connection: %s", err)
@ -177,7 +185,11 @@ func (s *Server) WebSocket() http.HandlerFunc {
for {
select {
case msg := <-sub.messages:
// Don't spam messages too quick.
sub.cooldown.Lock()
err = writeTimeout(ctx, time.Second*5, c, msg)
time.Sleep(100 * time.Second)
sub.cooldown.Unlock()
if err != nil {
return
}