Remove the relay's broker wait channel after each tick (closes #151)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The IRC relay registers a broker wait channel on each pass of its loop, and when the 100 ms ticker fired instead of a notification it looped without removing that channel. An idle IRC listener connection added ten channels a second to the broker, and all but the last stayed after the connection closed. The ticker case now removes its channel, as the HTTP long-poll does after its timer. Broker.ListenerCount returns how many wait channels a user holds, so the test can check that an idle connection holds at most one, and none after it closes. Model: opus-5-5
This commit is contained in:
@@ -47,6 +47,16 @@ func (b *Broker) Notify(userID int64) {
|
||||
}
|
||||
}
|
||||
|
||||
// ListenerCount returns how many channels from Wait are still
|
||||
// registered for the user: those that neither Notify nor Remove
|
||||
// has taken out.
|
||||
func (b *Broker) ListenerCount(userID int64) int {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
return len(b.listeners[userID])
|
||||
}
|
||||
|
||||
// Remove removes a specific wait channel (for cleanup on timeout).
|
||||
func (b *Broker) Remove(
|
||||
userID int64,
|
||||
|
||||
@@ -59,6 +59,39 @@ func TestRemove(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListenerCount(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
brk := broker.New()
|
||||
|
||||
wantCount := func(step string, userID int64, want int) {
|
||||
t.Helper()
|
||||
|
||||
got := brk.ListenerCount(userID)
|
||||
if got != want {
|
||||
t.Errorf("%s: user %d: got %d, want %d",
|
||||
step, userID, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
wantCount("before Wait", 1, 0)
|
||||
|
||||
waitCh := brk.Wait(1)
|
||||
brk.Wait(1)
|
||||
brk.Wait(2)
|
||||
|
||||
wantCount("after two Waits", 1, 2)
|
||||
|
||||
brk.Remove(1, waitCh)
|
||||
|
||||
wantCount("after Remove", 1, 1)
|
||||
|
||||
brk.Notify(1)
|
||||
|
||||
wantCount("after Notify", 1, 0)
|
||||
wantCount("after Notify", 2, 1)
|
||||
}
|
||||
|
||||
func TestMultipleWaiters(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user