Centralize chan closure to be done in the client, not the server. Set chan size to max message size.

This commit is contained in:
Gabe Kangas 2021-07-22 15:27:04 -07:00
parent f782e82909
commit c3e8e78dad
2 changed files with 9 additions and 4 deletions

View file

@ -156,6 +156,10 @@ func (c *ChatClient) close() {
c.conn.Close() c.conn.Close()
c.server.unregister <- c c.server.unregister <- c
if c.send != nil {
close(c.send)
c.send = nil
}
} }
func (c *ChatClient) passesRateLimit() bool { func (c *ChatClient) passesRateLimit() bool {
@ -175,7 +179,9 @@ func (c *ChatClient) sendPayload(payload events.EventPayload) {
return return
} }
c.send <- data if c.send != nil {
c.send <- data
}
} }
func (c *ChatClient) sendAction(message string) { func (c *ChatClient) sendAction(message string) {

View file

@ -54,7 +54,6 @@ func (s *ChatServer) Run() {
if _, ok := s.clients[client.id]; ok { if _, ok := s.clients[client.id]; ok {
s.mu.Lock() s.mu.Lock()
delete(s.clients, client.id) delete(s.clients, client.id)
close(client.send)
s.mu.Unlock() s.mu.Unlock()
} }
@ -72,7 +71,7 @@ func (s *ChatServer) Addclient(conn *websocket.Conn, user *user.User, accessToke
User: user, User: user,
ipAddress: conn.RemoteAddr().String(), ipAddress: conn.RemoteAddr().String(),
accessToken: accessToken, accessToken: accessToken,
send: make(chan []byte, 256), send: make(chan []byte, maxMessageSize),
UserAgent: userAgent, UserAgent: userAgent,
ConnectedAt: time.Now(), ConnectedAt: time.Now(),
} }
@ -195,7 +194,7 @@ func (s *ChatServer) Broadcast(payload events.EventPayload) error {
select { select {
case client.send <- data: case client.send <- data:
default: default:
close(client.send) client.close()
delete(s.clients, client.id) delete(s.clients, client.id)
} }
} }