Skip to content

Forget a topic once it is left, client- or server-initiated - #95

Open
chrismccord wants to merge 1 commit into
CuatroElixir:mainfrom
chrismccord:forget-left-topics
Open

chrismccord wants to merge 1 commit into
CuatroElixir:mainfrom
chrismccord:forget-left-topics

Conversation

@chrismccord

Copy link
Copy Markdown

A topic that was left, by the client (leave/2) or by the server closing its channel (phx_close), keeps its join forever today. The socket only marks it :closed, and the connection never drops its join ref. A client that joins a fresh topic per short-lived task therefore grows both maps with every task it ever ran. The socket is copied into every command it sends, so each push gets slower as the dead joins pile up. On one long-lived connection that joins a channel per task, we saw thousands of dead joins after a few minutes.

This change forgets a topic once it is left:

  • Socket.apply_event/2 drops the topic's join on TopicLeft.
  • The connection pipeline drops the topic's join ref on TopicLeft, as it already does for TopicJoinFailed and TopicJoinClosed.
  • Events for a topic whose join is already gone (a second close, a late join reply) leave the socket unchanged instead of raising on a missing key.

A topic closed by an error or a refused join keeps its join, so rejoin/3 still works for it.

Behavior change: after a topic is left, rejoin/3 returns {:error, :never_joined} instead of rejoining with the old params. Join it again with join/3. The existing "once left, a topic may be rejoined" test is updated to match.

Tests: a new test/slipstream/socket_joins_test.exs covers a left join being dropped, 500 join/leave cycles leaving joins empty, late events after the join is gone, error closes keeping the join, and rejoin/3 on a left topic. The full suite passes (97 tests).

A left topic (the client left it, or the server closed its channel with
phx_close) kept its join forever: the socket only marked it :closed and the
connection never dropped its join ref. A client that joins a fresh topic per
task grew both maps with every task it ever ran, and the socket, copied into
every command, grew with it. A long-lived connection that joins one channel
per short-lived task held thousands of dead joins within minutes, and every
push copied them.

TopicLeft now drops the topic's join in the socket and its join ref in the
connection. Events for a topic whose join is gone (a second close, a late
join reply) leave the socket as it is instead of crashing on a missing key.
A topic closed by an error or a refused join keeps its join for rejoin/3.
Rejoining a left topic takes join/3; rejoin/3 answers {:error, :never_joined}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant