server: per-connection wire state and closing frames in the conn registry (#18)

Conn now carries what DESIGN 6/6.4/6.5 need per connection, all under the one
registry lock: the outgoing n counter (starts at 1, server frames only), the intent
token bucket, the malformed-flood window, the player's named flag and the last Pong.
None of it is shared, so the n counter is not a global side channel (H25).

activate/revokePlayer now hand back the doomed Conn values instead of raw sockets:
the caller still has the frame count, so the final closing frame carries the right n
before the close. Tests cover per-connection counters, the bucket and the flood
window in isolation, and that a snapshot survives the lock.
This commit is contained in:
2026-11-15 08:33:07 +01:00
parent eb48034eba
commit 7e2d5b8dc0
2 changed files with 139 additions and 10 deletions
+69 -2
View File
@@ -1,7 +1,9 @@
## Connection caps (#17, DESIGN 4.3/6.5): 4 per player (5th evicts the oldest), 16 total (17th refused).
## Per-connection wire state (#18, DESIGN 6/6.4/6.5 row 8): the `n` counter, the intent bucket and
## the malformed-flood window are per connection and none of them is global.
import std/[unittest, options]
import mummy
import conns, auth
import conns, auth, protocol
proc dummyWs(): WebSocket =
# Mummy's fields are private; an all-zero value is enough for registry bookkeeping tests.
@@ -50,6 +52,8 @@ suite "per-player cap":
check c.reserve()
let fifth = c.activate(7, prPlayer, dummyWs())
check fifth.evictedId == some(ids[0])
check fifth.evicted.get.playerId == 7
check fifth.evicted.get.frameNo == 1 # a fresh connection: its closing frame is n = 1
check c.activeCountFor(7) == 4
check c.connById(ids[0]).isNone
check c.connById(fifth.id).isSome
@@ -85,7 +89,10 @@ suite "revocation":
discard c.activate(1, prPlayer, dummyWs())
discard c.activate(2, prPlayer, dummyWs())
discard c.activate(1, prPlayer, dummyWs())
check c.revokePlayer(1).len == 2
let revoked = c.revokePlayer(1)
check revoked.len == 2
check revoked[0].playerId == 1
for conn in revoked: check c.connById(conn.id).isNone
check c.activeCountFor(1) == 0
check c.activeCountFor(2) == 1
check c.activeCount == 1
@@ -106,3 +113,63 @@ suite "revocation":
check c.connForWs(ws).get.id == a.id
var other: WebSocket # another all-zero value still equals the first one structurally
check c.connForWs(other).isSome
suite "per-connection wire state (#18)":
test "the frame counter starts at 1 on each connection and is never global":
let c = newConns()
let a = c.activate(1, prPlayer, dummyWs())
let b = c.activate(1, prPlayer, dummyWs())
check c.nextFrameNo(a.id) == 1
check c.nextFrameNo(a.id) == 2
check c.nextFrameNo(b.id) == 1 # per connection, not a server-wide sequence (H25)
check c.nextFrameNo(a.id) == 3
check c.connById(b.id).get.frames == 1
test "a removed connection has no next frame number; the caller sends closing with frameNo":
let c = newConns()
let a = c.activate(1, prPlayer, dummyWs())
discard c.nextFrameNo(a.id)
discard c.nextFrameNo(a.id)
let doomed = c.connById(a.id).get
check doomed.frameNo == 3
c.remove(a.id)
check c.nextFrameNo(a.id) == 0 # gone: drop the frame
check doomed.frameNo == 3 # the snapshot still says which number closes it
test "the intent bucket is per connection: draining one leaves the other full":
let c = newConns()
let a = c.activate(1, prPlayer, dummyWs())
let b = c.activate(2, prPlayer, dummyWs())
for _ in 0 ..< RateBurst: check c.takeIntentToken(a.id, 0)
check not c.takeIntentToken(a.id, 0)
check c.takeIntentToken(b.id, 0)
check not c.takeIntentToken(999, 0) # unknown connection: nothing to spend it on
test "the malformed window is per connection and fires on the tenth frame":
let c = newConns()
let a = c.activate(1, prPlayer, dummyWs())
let b = c.activate(2, prPlayer, dummyWs())
for i in 0 ..< MalformedFloodLimit - 1:
check not c.recordMalformed(a.id, int64(i))
check c.recordMalformed(a.id, int64(MalformedFloodLimit - 1))
check not c.recordMalformed(b.id, 0)
test "a Pong is recorded on the right connection":
let c = newConns()
let a = c.activate(1, prPlayer, dummyWs())
let b = c.activate(2, prPlayer, dummyWs())
c.notePong(a.id, 5, 4242)
check c.connById(a.id).get.pongSeq == 5
check c.connById(a.id).get.lastPongMs == 4242
check c.connById(b.id).get.pongSeq == 0
test "a snapshot hands out every live connection without holding the lock afterwards":
let c = newConns()
let a = c.activate(1, prPlayer, dummyWs())
discard c.activate(2, prGm, dummyWs())
let snap = c.snapshot()
check snap.len == 2
check snap[0].id == a.id
c.remove(a.id)
check snap.len == 2
check c.activeCount == 1