From c3e8e78dadc82841a65a68b41a82a999c2338612 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Thu, 22 Jul 2021 15:27:04 -0700 Subject: [PATCH] Centralize chan closure to be done in the client, not the server. Set chan size to max message size. --- core/chat/chatclient.go | 8 +++++++- core/chat/server.go | 5 ++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/core/chat/chatclient.go b/core/chat/chatclient.go index 69015b007..12af7e75e 100644 --- a/core/chat/chatclient.go +++ b/core/chat/chatclient.go @@ -156,6 +156,10 @@ func (c *ChatClient) close() { c.conn.Close() c.server.unregister <- c + if c.send != nil { + close(c.send) + c.send = nil + } } func (c *ChatClient) passesRateLimit() bool { @@ -175,7 +179,9 @@ func (c *ChatClient) sendPayload(payload events.EventPayload) { return } - c.send <- data + if c.send != nil { + c.send <- data + } } func (c *ChatClient) sendAction(message string) { diff --git a/core/chat/server.go b/core/chat/server.go index 55388bd1a..240f32d3f 100644 --- a/core/chat/server.go +++ b/core/chat/server.go @@ -54,7 +54,6 @@ func (s *ChatServer) Run() { if _, ok := s.clients[client.id]; ok { s.mu.Lock() delete(s.clients, client.id) - close(client.send) s.mu.Unlock() } @@ -72,7 +71,7 @@ func (s *ChatServer) Addclient(conn *websocket.Conn, user *user.User, accessToke User: user, ipAddress: conn.RemoteAddr().String(), accessToken: accessToken, - send: make(chan []byte, 256), + send: make(chan []byte, maxMessageSize), UserAgent: userAgent, ConnectedAt: time.Now(), } @@ -195,7 +194,7 @@ func (s *ChatServer) Broadcast(payload events.EventPayload) error { select { case client.send <- data: default: - close(client.send) + client.close() delete(s.clients, client.id) } }