Skip to content
Open
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
16 changes: 16 additions & 0 deletions lib/slipstream/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ defmodule Slipstream.Configuration do
type: :keyword_list,
default: [protocols: [:http1]]
],
send_timeout_msec: [
doc: """
The maximum time to wait when writing to the socket. If a write does
not complete within this time (for example because the remote server
has stopped reading, so the TCP send buffer is full) the socket is
closed and `c:Slipstream.handle_disconnect/2` is invoked with
`{:send_failure, reason}`. Without this bound a stalled write blocks
the connection process indefinitely, which also prevents heartbeats
from detecting the dead connection. This sets the `:send_timeout` and
`:send_timeout_close` transport options unless they are given in
`:mint_opts`. A value of `0` will disable the timeout.
""",
type: :non_neg_integer,
default: 30_000
],
extensions: [
doc: """
A list of extensions to pass to `Mint.WebSocket.upgrade/4`.
Expand Down Expand Up @@ -140,6 +155,7 @@ defmodule Slipstream.Configuration do
uri: %URI{},
heartbeat_interval_msec: non_neg_integer(),
headers: [{String.t(), String.t()}],
send_timeout_msec: non_neg_integer(),
json_parser: module(),
serializer: module(),
reconnect_after_msec: [non_neg_integer()],
Expand Down
22 changes: 21 additions & 1 deletion lib/slipstream/connection/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,27 @@ defmodule Slipstream.Connection.Impl do
map_http_scheme(config.uri.scheme),
config.uri.host,
config.uri.port,
config.mint_opts
mint_opts(config)
)
end

# bounds writes to the socket unless the user has configured the transport
# options themselves
def mint_opts(%{send_timeout_msec: timeout} = config)
when timeout in [0, nil],
do: config.mint_opts

def mint_opts(config) do
defaults = [
send_timeout: config.send_timeout_msec,
send_timeout_close: true
]

Keyword.update(
config.mint_opts,
:transport_opts,
defaults,
&Keyword.merge(defaults, &1)
)
end

Expand Down
90 changes: 90 additions & 0 deletions test/slipstream/send_timeout_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
defmodule Slipstream.SendTimeoutTest do
use ExUnit.Case, async: true

@moduledoc """
Writes to the socket are bounded so that a stalled peer cannot block the
connection process forever
"""

alias Slipstream.Configuration
alias Slipstream.Connection.Impl
alias Slipstream.FakeWebSocketServer, as: Server

@client Slipstream.GoodExample
@topic "rooms:lobby"

describe "mint options" do
defp config(opts) do
Configuration.validate!([uri: "ws://localhost:4000/socket"] ++ opts)
end

test "a send timeout is added to the transport options by default" do
assert config([]) |> Impl.mint_opts() |> Keyword.get(:transport_opts) ==
[send_timeout: 30_000, send_timeout_close: true]
end

test "transport options given in mint_opts take precedence" do
opts =
config(
send_timeout_msec: 5_000,
mint_opts: [
protocols: [:http1],
transport_opts: [send_timeout: 1_000, sndbuf: 1_024]
]
)
|> Impl.mint_opts()

assert opts[:protocols] == [:http1]

assert Enum.sort(opts[:transport_opts]) == [
send_timeout: 1_000,
send_timeout_close: true,
sndbuf: 1_024
]
end

test "a send timeout of 0 leaves the mint options as given" do
assert config(send_timeout_msec: 0) |> Impl.mint_opts() ==
[protocols: [:http1]]
end
end

test "a peer which stops reading causes a disconnect" do
uri =
Server.start(
fn sock ->
Server.handshake(sock)
{:ok, _opcode, join, _rest} = Server.recv_frame(sock)
[join_ref, ref, topic, "phx_join", _params] = Jason.decode!(join)
Server.reply(sock, join_ref, ref, topic, "ok")
# never read from the socket again
Process.sleep(:infinity)
end,
recbuf: 1_024
)

client =
start_supervised!(
{@client,
pid: self(),
uri: uri,
heartbeat_interval_msec: 100,
send_timeout_msec: 300,
mint_opts: [protocols: [:http1], transport_opts: [sndbuf: 1_024]]}
)

assert_receive {@client, :connected}
GenServer.cast(client, {:join, @topic, %{}})
assert_receive {@client, :joined, @topic, %{}}

# more than the socket buffers can hold
GenServer.cast(
client,
{:push, @topic, "upload", %{"data" => String.duplicate("a", 8_000_000)}}
)

assert_receive {@client, :disconnected,
{:send_failure, %Mint.TransportError{reason: :timeout}}},
2_000
end
end
227 changes: 227 additions & 0 deletions test/support/fake_websocket_server.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
defmodule Slipstream.FakeWebSocketServer do
@moduledoc """
A minimal raw-TCP WebSocket server for tests that need byte-level control
over what the server sends (or does not send)

`start/2` listens on a random port and calls `handler` with the accepted
socket for every connection. The listener and every handler are torn down
when the calling (test) process exits.

uri =
FakeWebSocketServer.start(fn socket ->
FakeWebSocketServer.handshake(socket)
FakeWebSocketServer.send_frame(socket, :ping, "hello")
Process.sleep(:infinity)
end)
"""

@guid "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
@opcodes %{text: 0x1, binary: 0x2, close: 0x8, ping: 0x9, pong: 0xA}

@doc """
Starts a listener and returns the `ws://` URI to connect to
"""
def start(handler, listen_opts \\ []) when is_function(handler, 1) do
owner = self()

spawn_link(fn ->
Process.flag(:trap_exit, true)

{:ok, lsock} =
:gen_tcp.listen(
0,
[:binary, active: false, reuseaddr: true, packet: :raw] ++
listen_opts
)

{:ok, port} = :inet.port(lsock)
send(owner, {__MODULE__, :port, port})
accept_loop(lsock, handler, owner)
end)

receive do
{__MODULE__, :port, port} -> "ws://127.0.0.1:#{port}/socket/websocket"
end
end

defp accept_loop(lsock, handler, owner) do
receive do
{:EXIT, ^owner, _reason} -> exit(:shutdown)
{:EXIT, _handler, _reason} -> accept_loop(lsock, handler, owner)
after
0 -> :ok
end

case :gen_tcp.accept(lsock, 50) do
{:ok, sock} ->
pid = spawn_link(fn -> receive(do: (:go -> handler.(sock))) end)
:ok = :gen_tcp.controlling_process(sock, pid)
send(pid, :go)

{:error, :timeout} ->
:ok
end

accept_loop(lsock, handler, owner)
end

@doc """
Reads the HTTP upgrade request and returns the `sec-websocket-key`
"""
def read_request(sock, acc \\ "") do
{:ok, data} = :gen_tcp.recv(sock, 0, 5_000)
acc = acc <> data

if String.contains?(acc, "\r\n\r\n") do
[_, key] = Regex.run(~r/sec-websocket-key:\s*(\S+)/i, acc)
key
else
read_request(sock, acc)
end
end

@doc """
The full `101 Switching Protocols` response for a request key
"""
def upgrade_response(key) do
accept = Base.encode64(:crypto.hash(:sha, key <> @guid))

"HTTP/1.1 101 Switching Protocols\r\n" <>
"upgrade: websocket\r\n" <>
"connection: Upgrade\r\n" <>
"sec-websocket-accept: #{accept}\r\n\r\n"
end

@doc """
Reads the upgrade request and accepts it
"""
def handshake(sock) do
key = read_request(sock)
:ok = :gen_tcp.send(sock, upgrade_response(key))
end

@doc """
Encodes an unmasked server-to-client frame
"""
def frame(opcode, payload) when is_atom(opcode),
do: frame(Map.fetch!(@opcodes, opcode), payload)

def frame(opcode, payload) when is_integer(opcode) do
len = byte_size(payload)

header =
cond do
len < 126 -> <<1::1, 0::3, opcode::4, 0::1, len::7>>
len < 65_536 -> <<1::1, 0::3, opcode::4, 0::1, 126::7, len::16>>
true -> <<1::1, 0::3, opcode::4, 0::1, 127::7, len::64>>
end

header <> payload
end

def send_frame(sock, opcode, payload),
do: :gen_tcp.send(sock, frame(opcode, payload))

@doc """
Sends a Phoenix V2 JSON message (a list) as a text frame
"""
def send_message(sock, message) when is_list(message),
do: send_frame(sock, :text, Jason.encode!(message))

def reply(sock, join_ref, ref, topic, status, response \\ %{}) do
send_message(sock, [
join_ref,
ref,
topic,
"phx_reply",
%{"status" => status, "response" => response}
])
end

@doc """
Receives one (masked) client frame

Returns `{:ok, opcode, payload, rest}` or `{:error, reason}`
"""
def recv_frame(sock, buffer \\ "") do
case parse(buffer) do
{:ok, _op, _payload, _rest} = ok ->
ok

:more ->
case :gen_tcp.recv(sock, 0, 30_000) do
{:ok, data} -> recv_frame(sock, buffer <> data)
{:error, reason} -> {:error, reason}
end
end
end

defp parse(
<<_::4, op::4, 1::1, 127::7, len::64, mask::binary-4, rest::binary>>
),
do: unmask(op, mask, rest, len)

defp parse(
<<_::4, op::4, 1::1, 126::7, len::16, mask::binary-4, rest::binary>>
),
do: unmask(op, mask, rest, len)

defp parse(<<_::4, op::4, 1::1, len::7, mask::binary-4, rest::binary>>)
when len < 126,
do: unmask(op, mask, rest, len)

defp parse(_), do: :more

defp unmask(op, mask, data, len) when byte_size(data) >= len do
<<payload::binary-size(len), rest::binary>> = data
key = mask |> :binary.copy(div(len, 4) + 1) |> binary_part(0, len)
{:ok, op, :crypto.exor(payload, key), rest}
end

defp unmask(_op, _mask, _data, _len), do: :more

@doc """
Accepts the upgrade and then behaves like a tiny Phoenix server: joins and
leaves are accepted and heartbeats are acknowledged

`override` is called with `(socket, {join_ref, ref, topic, event, payload})`
for every text message from the client; return `:default` to fall back to
the default behaviour.
"""
def serve(sock, override \\ fn _sock, _message -> :default end) do
handshake(sock)
serve_loop(sock, "", override)
end

defp serve_loop(sock, buffer, override) do
case recv_frame(sock, buffer) do
{:ok, 0x1, payload, rest} ->
[join_ref, ref, topic, event, params] = Jason.decode!(payload)
message = {join_ref, ref, topic, event, params}

if override.(sock, message) == :default,
do: default_reply(sock, message)

serve_loop(sock, rest, override)

{:ok, _opcode, _payload, rest} ->
serve_loop(sock, rest, override)

{:error, _reason} ->
:ok
end
end

defp default_reply(sock, {join_ref, ref, topic, "phx_join", _params}),
do: reply(sock, join_ref, ref, topic, "ok")

defp default_reply(sock, {join_ref, ref, topic, "phx_leave", _params}) do
reply(sock, join_ref, ref, topic, "ok")
send_message(sock, [join_ref, join_ref, topic, "phx_close", %{}])
end

defp default_reply(sock, {_join_ref, ref, "phoenix", "heartbeat", _params}),
do: reply(sock, nil, ref, "phoenix", "ok")

defp default_reply(_sock, _message), do: :ok
end
Loading