Skip to content

Commit

Permalink
wip: server initiated channel handovers
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Oct 29, 2024
1 parent 58cae7e commit cd99fcf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions lib/phoenix/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,41 @@ defmodule Phoenix.Socket do
{:ok, state}
end

def __info__({:handover, payload, handover_pid, topic, join_ref}, {state, socket}) do
{channel, opts} = socket.handler.__channel__(topic)
opts = Keyword.put(opts, :handover_pid, handover_pid)
join_message = %Message{topic: topic, payload: payload, ref: join_ref, join_ref: join_ref}

case Phoenix.Channel.Server.join(socket, channel, join_message, opts) do
{:ok, reply, pid} ->
reply = %Message{
join_ref: join_ref,
ref: nil,
topic: topic,
event: "phx_handover",
payload: reply
}

shutdown_duplicate_channel(handover_pid, :handover)

state = put_channel(state, pid, topic, join_ref)
{:reply, :ok, encode_reply(socket, reply), {state, socket}}

{:error, reply} ->
reply = %Reply{
join_ref: join_ref,
ref: nil,
topic: topic,
status: :error,
payload: reply
}

shutdown_duplicate_channel(handover_pid, :handover)

{:reply, :error, encode_reply(socket, reply), {state, socket}}
end
end

def __info__(_, state) do
{:ok, state}
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Phoenix.MixProject do
end
end

@version "1.7.14"
@version "1.8.0-dev"
@scm_url "https://github.com/phoenixframework/phoenix"

# If the elixir requirement is updated, we need to make the installer
Expand Down

0 comments on commit cd99fcf

Please sign in to comment.