Centralize chan closure to be done in the client, not the server. Set chan size to max message size.
This commit is contained in:
parent
f782e82909
commit
c3e8e78dad
@ -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,8 +179,10 @@ func (c *ChatClient) sendPayload(payload events.EventPayload) {
|
||||
return
|
||||
}
|
||||
|
||||
if c.send != nil {
|
||||
c.send <- data
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ChatClient) sendAction(message string) {
|
||||
clientMessage := events.ActionEvent{
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user