Browse Source

WIP performance and load related updates

gek/new-load-tests
Gabe Kangas 4 years ago
parent
commit
b0f9465138
  1. 40
      core/chat/server.go
  2. 2
      webroot/js/utils/websocket.js

40
core/chat/server.go

@ -2,6 +2,7 @@ package chat @@ -2,6 +2,7 @@ package chat
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
@ -71,6 +72,7 @@ func (s *Server) Run() { @@ -71,6 +72,7 @@ func (s *Server) Run() {
}
case message := <-s.inbound:
fmt.Println("inbound...")
s.eventReceived(message)
}
}
@ -214,21 +216,31 @@ func (s *Server) Broadcast(payload events.EventPayload) error { @@ -214,21 +216,31 @@ func (s *Server) Broadcast(payload events.EventPayload) error {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
go func() {
s.mu.Lock()
defer s.mu.Unlock()
for _, client := range s.clients {
if client == nil {
continue
}
defer func() {
if a := recover(); a != nil {
fmt.Println("RECOVER", a)
}
}()
select {
case client.send <- data:
default:
client.close()
delete(s.clients, client.id)
for _, client := range s.clients {
if client == nil {
continue
}
if client.send != nil {
select {
case client.send <- data:
default:
client.close()
delete(s.clients, client.id)
}
}
}
}
}()
return nil
}
@ -241,7 +253,9 @@ func (s *Server) Send(payload events.EventPayload, client *Client) { @@ -241,7 +253,9 @@ func (s *Server) Send(payload events.EventPayload, client *Client) {
return
}
client.send <- data
if client.send != nil {
client.send <- data
}
}
// DisconnectUser will forcefully disconnect all clients belonging to a user by ID.

2
webroot/js/utils/websocket.js

@ -163,7 +163,7 @@ export default class Websocket { @@ -163,7 +163,7 @@ export default class Websocket {
console.error(e, e.data);
return;
}
if (!model.type) {
console.error('No type provided', model);
return;

Loading…
Cancel
Save