Skip to content

Commit

Permalink
bugfix: fix 2 bugs with connection deletion code
Browse files Browse the repository at this point in the history
- Connections are unique for the 3-uple (user, device, conneciton) IDs.
  The code was only checking (user, device). This means we would delete
  ALL connections for a device, is ANY connection expired.
- ...except we wouldn't, because of the 2nd bug, which is the deletion
  code itself. This is missing `i--` so we will not do an ID check on
  the element after a deleted index.

Both of these issues have now been fixed.
  • Loading branch information
kegsay committed Nov 24, 2023
1 parent 86f9333 commit 129dea8
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions sync3/connmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sync"
"time"

"golang.org/x/exp/slices"

"github.com/ReneKroon/ttlcache/v2"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -228,10 +230,11 @@ func (m *ConnMap) closeConn(conn *Conn) {
h := conn.handler
conns := m.userIDToConn[conn.UserID]
for i := 0; i < len(conns); i++ {
if conns[i].DeviceID == conn.DeviceID {
if conns[i].DeviceID == conn.DeviceID && conns[i].CID == conn.CID {
// delete without preserving order
conns[i] = conns[len(conns)-1]
conns = conns[:len(conns)-1]
conns[i] = nil // allow GC
conns = slices.Delete(conns, i, i+1)
i--
}
}
m.userIDToConn[conn.UserID] = conns
Expand Down

0 comments on commit 129dea8

Please sign in to comment.