bugfix: fix 2 bugs with connection deletion code

- 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.
This commit is contained in:
Kegan Dougal 2023-11-24 14:47:47 +00:00
parent 86f9333fdd
commit 129dea816a

View File

@ -5,6 +5,8 @@ import (
"sync"
"time"
"golang.org/x/exp/slices"
"github.com/ReneKroon/ttlcache/v2"
"github.com/prometheus/client_golang/prometheus"
)
@ -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