Browse Source

WIP performance and load related updates

gek/new-load-tests
Gabe Kangas 5 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
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@ -71,6 +72,7 @@ func (s *Server) Run() {
} }
case message := <-s.inbound: case message := <-s.inbound:
fmt.Println("inbound...")
s.eventReceived(message) s.eventReceived(message)
} }
} }
@ -214,21 +216,31 @@ func (s *Server) Broadcast(payload events.EventPayload) error {
return err return err
} }
s.mu.Lock() go func() {
defer s.mu.Unlock() s.mu.Lock()
defer s.mu.Unlock()
for _, client := range s.clients { defer func() {
if client == nil { if a := recover(); a != nil {
continue fmt.Println("RECOVER", a)
} }
}()
select { for _, client := range s.clients {
case client.send <- data: if client == nil {
default: continue
client.close() }
delete(s.clients, client.id)
if client.send != nil {
select {
case client.send <- data:
default:
client.close()
delete(s.clients, client.id)
}
}
} }
} }()
return nil return nil
} }
@ -241,7 +253,9 @@ func (s *Server) Send(payload events.EventPayload, client *Client) {
return return
} }
client.send <- data if client.send != nil {
client.send <- data
}
} }
// DisconnectUser will forcefully disconnect all clients belonging to a user by ID. // 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 {
console.error(e, e.data); console.error(e, e.data);
return; return;
} }
if (!model.type) { if (!model.type) {
console.error('No type provided', model); console.error('No type provided', model);
return; return;

Loading…
Cancel
Save