Skip to content

Commit

Permalink
Fix overly aggressive websocket cleanup. Resolves #4598
Browse files Browse the repository at this point in the history
  • Loading branch information
jertel committed Jun 23, 2021
1 parent 41f0f24 commit 5816520
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions web/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ func (host *Host) AddConnection(wsConn *websocket.Conn, ip string) *Connection {
func (host *Host) RemoveConnection(wsConn *websocket.Conn) {
host.lock.Lock();
defer host.lock.Unlock()
host.connections = make([]*Connection, 0)
remaining := make([]*Connection, 0)
for _, connection := range host.connections {
if connection.websocket != wsConn {
host.connections = append(host.connections, connection)
remaining = append(remaining, connection)
}
}
host.connections = remaining
log.WithField("Connections", len(host.connections)).Debug("Removed WebSocket connection")
}

Expand Down
25 changes: 25 additions & 0 deletions web/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ func TestAddRemoveConnection(tester *testing.T) {
})
}

func TestMultipleConnections(tester *testing.T) {
host := NewHost("http://some.where/path", "/tmp/foo", 123, "unit test")
conn1 := &websocket.Conn{}
conn2 := &websocket.Conn{}
tester.Run("testing add multiple connections", func(t *testing.T) {
host.AddConnection(conn1, "1.2.3.4");
host.AddConnection(conn2, "1.2.3.4");
if len(host.connections) != 2 {
tester.Errorf("expected %d but got %d", 2, len(host.connections))
}
})
tester.Run("testing remove first connection", func(t *testing.T) {
host.RemoveConnection(conn1);
if len(host.connections) != 1 {
t.Errorf("final expected %d but got %d", 1, len(host.connections))
}
})
tester.Run("testing remove second connection", func(t *testing.T) {
host.RemoveConnection(conn2);
if len(host.connections) != 0 {
t.Errorf("final expected %d but got %d", 0, len(host.connections))
}
})
}

func TestManageConnections(tester *testing.T) {
host := NewHost("http://some.where/path", "/tmp/foo", 123, "unit test")
conn := host.AddConnection(nil, "")
Expand Down

0 comments on commit 5816520

Please sign in to comment.