Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration of dynamic socket timeout #2

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/phoenix/transports/websocket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ defmodule Phoenix.Transports.WebSocket do
keys = Keyword.get(opts, :connect_info, [])
connect_info = Transport.connect_info(conn, endpoint, keys)

opts = case Keyword.fetch!(opts, :timeout) do
{m, f, a} -> Keyword.put(opts, :timeout, apply(m, f, a))
_ -> opts
end

config = %{
endpoint: endpoint,
transport: :websocket,
Expand Down
13 changes: 13 additions & 0 deletions test/phoenix/integration/websocket_socket_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,18 @@ defmodule Phoenix.Integration.WebSocketTest do
websocket: [path: "nested/path", check_origin: ["//example.com"], timeout: 200],
custom: :value

socket "/custom/timeout", UserSocket,
websocket: [path: "/", timeout: {Endpoint, :websocket_timeout, []}],
custom: :value

socket "/custom/:socket_var", UserSocket,
websocket: [path: ":path_var/path", check_origin: ["//example.com"], timeout: 200],
custom: :value

socket "/ws/ping", PingSocket,
websocket: true

def websocket_timeout, do: 300
end

setup_all do
Expand Down Expand Up @@ -161,6 +167,13 @@ defmodule Phoenix.Integration.WebSocketTest do
assert {:ok, _} = WebsocketClient.connect(self(), "#{path}?key=value", :noop)
end

test "websocket timeout can be provided via MFA" do
path = "ws://127.0.0.1:#{@port}/custom/timeout/"
{:ok, client} = WebsocketClient.connect(self(), path, :noop)
WebsocketClient.send(client, {:text, "ping"})
assert_receive {:text, "pong"}
end

test "allows a path with variables" do
path = "ws://127.0.0.1:#{@port}/custom/123/456/path"
assert {:ok, client} = WebsocketClient.connect(self(), "#{path}?key=value", :noop)
Expand Down