Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions lib/slipstream/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ defmodule Slipstream.Configuration do
type: :non_neg_integer,
default: 30_000
],
upgrade_timeout_msec: [
doc: """
The time to wait for the server to respond to the WebSocket upgrade
request once the connection has been opened. If the server does not
respond within this time, the connection is closed and
`c:Slipstream.handle_disconnect/2` is invoked with
`{:error, :upgrade_timeout}`. A value of `0` will disable the timeout.
""",
type: :non_neg_integer,
default: 10_000
],
headers: [
doc: """
A set of headers to merge with the request headers when GETing the
Expand Down Expand Up @@ -139,6 +150,7 @@ defmodule Slipstream.Configuration do
@type t :: %__MODULE__{
uri: %URI{},
heartbeat_interval_msec: non_neg_integer(),
upgrade_timeout_msec: non_neg_integer(),
headers: [{String.t(), String.t()}],
json_parser: module(),
serializer: module(),
Expand Down
37 changes: 35 additions & 2 deletions lib/slipstream/connection/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ defmodule Slipstream.Connection.Pipeline do
put_message(p, :connect)
end

defp decode_message(
%{raw_message: :upgrade_timeout, state: %State{status: :opened}} = p
) do
put_message(
p,
event(%Events.ChannelConnectFailed{reason: :upgrade_timeout})
)
end

# the upgrade timer fired just as the connection was established, so it
# could not be cancelled
# coveralls-ignore-start
defp decode_message(%{raw_message: :upgrade_timeout} = p) do
put_message(p, event(%Events.NoOp{}))
end

# coveralls-ignore-stop

defp decode_message(
%{
raw_message: {:DOWN, ref, :process, _pid, reason},
Expand Down Expand Up @@ -184,7 +202,14 @@ defmodule Slipstream.Connection.Pipeline do
) do
with {:ok, conn} <- Impl.http_connect(config),
{:ok, conn, ref} <- Impl.websocket_upgrade(conn, config) do
put_state(p, %{state | conn: conn, request_ref: ref})
state = %{
state
| conn: conn,
request_ref: ref,
upgrade_timer: start_upgrade_timer(config)
}

put_state(p, state)
else
# coveralls-ignore-start
{:error, conn, reason} ->
Expand Down Expand Up @@ -408,6 +433,8 @@ defmodule Slipstream.Connection.Pipeline do
state: %State{} = state
} = p
) do
if state.upgrade_timer, do: Process.cancel_timer(state.upgrade_timer)

timer =
if state.config.heartbeat_interval_msec != 0 do
{:ok, tref} =
Expand All @@ -420,7 +447,7 @@ defmodule Slipstream.Connection.Pipeline do
end

state =
%{state | status: :connected, heartbeat_timer: timer}
%{state | status: :connected, upgrade_timer: nil, heartbeat_timer: timer}
|> State.reset_heartbeat()

route_event state, event
Expand Down Expand Up @@ -473,6 +500,12 @@ defmodule Slipstream.Connection.Pipeline do

# coveralls-ignore-stop

defp start_upgrade_timer(%{upgrade_timeout_msec: 0}), do: nil

defp start_upgrade_timer(%{upgrade_timeout_msec: timeout}) do
Process.send_after(self(), :upgrade_timeout, timeout)
end

@spec default_return(t()) :: t()
defp default_return(%{state: state, return: nil} = p) do
put_return(p, {:noreply, state})
Expand Down
1 change: 1 addition & 0 deletions lib/slipstream/connection/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule Slipstream.Connection.State do
:websocket,
:request_ref,
:join_params,
:upgrade_timer,
:heartbeat_timer,
:heartbeat_ref,
:metadata,
Expand Down
58 changes: 58 additions & 0 deletions test/slipstream/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,64 @@ defmodule Slipstream.IntegrationTest do
end
end

describe "given the server never responds to the WebSocket upgrade request" do
setup do
{:ok, listen} = :gen_tcp.listen(0, [:binary, active: false])
{:ok, port} = :inet.port(listen)
test_pid = self()

spawn_link(fn ->
{:ok, socket} = :gen_tcp.accept(listen)
{:ok, _upgrade_request} = :gen_tcp.recv(socket, 0)
# never respond, but report when the client closes the connection
send(test_pid, {:server_recv, :gen_tcp.recv(socket, 0)})
end)

[
config: [
uri: "ws://localhost:#{port}/socket/websocket",
upgrade_timeout_msec: 100
]
]
end

test "the socket is disconnected with :upgrade_timeout reason", c do
import Slipstream

assert {:error, :upgrade_timeout} =
c.config
|> connect!()
|> await_connect(15_000)

assert_receive {:server_recv, {:error, :closed}}
end
end

describe "given the server responds to the WebSocket upgrade request" do
setup do
[
config: [
uri: "ws://localhost:4001/socket/websocket",
upgrade_timeout_msec: 500
]
]
end

test "the upgrade timeout does not close the connected socket", c do
import Slipstream

socket =
c.config
|> connect!()
|> await_connect!(15_000)

Process.sleep(1_000)

assert Process.alive?(socket.channel_pid)
refute_received _
end
end

defmodule EtfSerializer do
alias Slipstream.Message

Expand Down