Skip to content
Permalink
Browse files

Remove uneeded heartbeat since cowboy handles timeouts

  • Loading branch information
chrismccord committed Oct 20, 2015
1 parent 234995e commit 7b252f42cc8552496a5ebd392f59a008ef6c98a9
@@ -24,8 +24,6 @@ defmodule Phoenix.Transports.WebSocket do
It may be set to `false` (not recommended) or to a list of explicitly
allowed origins
* `:heartbeat` - the heartbeat interval in milliseconds, default `30_000`
* `:code_reloader` - optionally override the default `:code_reloader` value
from the socket's endpoint
@@ -44,9 +42,8 @@ defmodule Phoenix.Transports.WebSocket do

def default_config() do
[serializer: Phoenix.Transports.WebSocketSerializer,
timeout: :infinity,
transport_log: false,
heartbeat: 30_000]
timeout: 60_000,
transport_log: false]
end

def handlers() do
@@ -56,7 +53,6 @@ defmodule Phoenix.Transports.WebSocket do
## Callbacks

import Plug.Conn, only: [fetch_query_params: 1, send_resp: 3]
import Phoenix.Utils, only: [now_ms: 0]

alias Phoenix.Socket.Broadcast
alias Phoenix.Socket.Transport
@@ -100,24 +96,19 @@ defmodule Phoenix.Transports.WebSocket do
Process.flag(:trap_exit, true)
serializer = Keyword.fetch!(config, :serializer)
timeout = Keyword.fetch!(config, :timeout)
heartbeat = Keyword.fetch!(config, :heartbeat)

if socket.id, do: socket.endpoint.subscribe(self, socket.id, link: true)

Process.send_after(self, :phoenix_heartbeat, heartbeat)

{:ok, %{socket: socket,
channels: HashDict.new,
channels_inverse: HashDict.new,
client_last_active: now_ms(),
heartbeat_interval: heartbeat,
serializer: serializer}, timeout}
end

@doc false
def ws_handle(opcode, payload, state) do
msg = state.serializer.decode!(payload, opcode: opcode)
state = bump_client_last_active(state)

case Transport.dispatch(msg, state.channels, state.socket) do
:noreply ->
@@ -150,23 +141,10 @@ defmodule Phoenix.Transports.WebSocket do
format_reply(msg, state)
end

def ws_info(:phoenix_heartbeat, state) do
if client_unresponsive?(state) do
{:shutdown, state}
else
Process.send_after(self, :phoenix_heartbeat, state.heartbeat_interval)
encode_reply Transport.heartbeat_message(), state
end
end

def ws_info(_, state) do
{:ok, state}
end

defp client_unresponsive?(state) do
now_ms() - state.client_last_active > (state.heartbeat_interval * 2)
end

@doc false
def ws_terminate(_reason, _state) do
:ok
@@ -197,10 +175,6 @@ defmodule Phoenix.Transports.WebSocket do
{:reply, {encoding, encoded_payload}, state}
end

defp bump_client_last_active(state) do
%{state | client_last_active: now_ms()}
end

defp code_reload(conn, opts, endpoint) do
reload? = Keyword.get(opts, :code_reloader, endpoint.config(:code_reloader))
if reload?, do: Phoenix.CodeReloader.reload!(endpoint)
@@ -63,7 +63,7 @@ defmodule Phoenix.Integration.WebSocketTest do
channel "rooms:*", RoomChannel

transport :websocket, Phoenix.Transports.WebSocket,
check_origin: ["//example.com"], heartbeat: 100
check_origin: ["//example.com"], timeout: 100

def connect(%{"reject" => "true"}, _socket) do
:error
@@ -238,21 +238,14 @@ defmodule Phoenix.Integration.WebSocketTest do
assert log =~ "The client's requested channel transport version \"123.1.1\" does not match server's version"
end

test "sends heartbeat and shuts down if client goes quiet" do
test "shuts down if client goes quiet" do
{:ok, socket} = WebsocketClient.start_link(self, "ws://127.0.0.1:#{@port}/ws/websocket")
Process.monitor(socket)
WebsocketClient.send_heartbeat(socket)
assert_receive %Message{event: "phx_reply",
payload: %{"response" => %{}, "status" => "ok"},
ref: "1", topic: "phoenix"}

assert_receive %Message{event: "heartbeat", topic: "phoenix", payload: %{}}, 200

WebsocketClient.send_heartbeat(socket)
assert_receive %Message{event: "phx_reply",
payload: %{"response" => %{}, "status" => "ok"},
ref: "2", topic: "phoenix"}, 200

assert_receive {:DOWN, _, :process, ^socket, :normal}, 400
end
end
@@ -66,7 +66,7 @@ defmodule Phoenix.SocketTest do

test "transport config is exposted and merged with prior registrations" do
ws = {Phoenix.Transports.WebSocket,
[timeout: 1234, serializer: Phoenix.Transports.WebSocketSerializer, transport_log: false, heartbeat: 30_000]}
[timeout: 1234, serializer: Phoenix.Transports.WebSocketSerializer, transport_log: false]}

lp = {Phoenix.Transports.LongPoll,
[window_ms: 10000, pubsub_timeout_ms: 2000, serializer: Phoenix.Transports.LongPollSerializer,

0 comments on commit 7b252f4

Please sign in to comment.
You can’t perform that action at this time.