removeClientAndListeners() -- stop confusing listeners when removing from the same ws.

This commit is contained in:
fiatjaf 2024-08-04 17:25:19 -03:00
parent ef57f54a28
commit 07ea3a35ec

View File

@ -102,7 +102,7 @@ func (rl *Relay) removeClientAndListeners(ws *WebSocket) {
defer rl.clientsMutex.Unlock()
if specs, ok := rl.clients[ws]; ok {
// swap delete listeners and delete client (all specs will be deleted)
for _, spec := range specs {
for s, spec := range specs {
// no need to cancel contexts since they inherit from the main connection context
// just delete the listeners (swap-delete)
srl := spec.subrelay
@ -112,7 +112,12 @@ func (rl *Relay) removeClientAndListeners(ws *WebSocket) {
moved := srl.listeners[movedFromIndex] // this wasn't removed, but will be moved
srl.listeners[spec.index] = moved
// update the index of the listener we just moved
// temporarily update the spec of the listener being removed to have index == -1
// (since it was removed) so it doesn't match in the search below
rl.clients[ws][s].index = -1
// now we must update the the listener we just moved
// so its .index reflects its new position on srl.listeners
movedSpecs := rl.clients[moved.ws]
idx := slices.IndexFunc(movedSpecs, func(ls listenerSpec) bool {
return ls.index == movedFromIndex