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

rename heartbeat_delay to server_heartbeat_interval #47

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ simple. It has just a couple of methods:
can be usefull when your loadbalancer doesn't support them.
* `{cookie_needed, boolean()}` - is your load balancer relying on
cookies to get sticky sessions working?
* `{heartbeat_delay, integer()}` - how often to send heartbeat
* `{server_heartbeat_interval, integer()}` - how often to send heartbeat
packets (in ms).
* `{disconnect_delay, integer()}` - how long to hold session state
after the client was last connected (in ms).
Expand Down
6 changes: 4 additions & 2 deletions src/sockjs_action.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ iframe(Req, Headers, #service{sockjs_url = SockjsUrl}) ->

-spec info_test(req(), headers(), service()) -> req().
info_test(Req, Headers, #service{websocket = Websocket,
cookie_needed = CookieNeeded}) ->
cookie_needed = CookieNeeded,
server_heartbeat_interval = ServerHeartbeatInterval}) ->
I = [{websocket, Websocket},
{cookie_needed, CookieNeeded},
{origins, [<<"*:*">>]},
{entropy, sockjs_util:rand32()}],
{entropy, sockjs_util:rand32()},
{server_heartbeat_interval, ServerHeartbeatInterval}],
D = sockjs_json:encode({I}),
H = [{"Content-Type", "application/json; charset=UTF-8"}],
sockjs_http:reply(200, H ++ Headers, D, Req).
Expand Down
4 changes: 2 additions & 2 deletions src/sockjs_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ init_state(Prefix, Callback, State, Options) ->
proplists:get_value(cookie_needed, Options, false),
disconnect_delay =
proplists:get_value(disconnect_delay, Options, 5000),
heartbeat_delay =
proplists:get_value(heartbeat_delay, Options, 25000),
server_heartbeat_interval =
proplists:get_value(server_heartbeat_interval, Options, 25000),
response_limit =
proplists:get_value(response_limit, Options, 128*1024),
hib_timeout =
Expand Down
22 changes: 11 additions & 11 deletions src/sockjs_internal.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
-type(callback() :: fun((user_session(), emittable(), any()) -> ok)).
-type(logger() :: fun((any(), req(), websocket|http) -> req())).

-record(service, {prefix :: nonempty_string(),
callback :: callback(),
state :: any(),
sockjs_url :: nonempty_string(),
cookie_needed :: boolean(),
websocket :: boolean(),
disconnect_delay :: non_neg_integer(),
heartbeat_delay :: non_neg_integer(),
response_limit :: non_neg_integer(),
hib_timeout :: non_neg_integer() | hibernate,
logger :: logger()
-record(service, {prefix :: nonempty_string(),
callback :: callback(),
state :: any(),
sockjs_url :: nonempty_string(),
cookie_needed :: boolean(),
websocket :: boolean(),
disconnect_delay :: non_neg_integer(),
server_heartbeat_interval :: non_neg_integer(),
response_limit :: non_neg_integer(),
hib_timeout :: non_neg_integer() | hibernate,
logger :: logger()
}).

-type(service() :: #service{}).
Expand Down
54 changes: 27 additions & 27 deletions src/sockjs_session.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
-include("sockjs_internal.hrl").
-type(handle() :: {?MODULE, {pid(), info()}}).

-record(session, {id :: session(),
outbound_queue = queue:new() :: queue(),
response_pid :: pid(),
disconnect_tref :: reference(),
disconnect_delay = 5000 :: non_neg_integer(),
heartbeat_tref :: reference() | triggered,
heartbeat_delay = 25000 :: non_neg_integer(),
ready_state = connecting :: connecting | open | closed,
close_msg :: {non_neg_integer(), string()},
-record(session, {id :: session(),
outbound_queue = queue:new() :: queue(),
response_pid :: pid(),
disconnect_tref :: reference(),
disconnect_delay = 5000 :: non_neg_integer(),
heartbeat_tref :: reference() | triggered,
server_heartbeat_interval = 25000 :: non_neg_integer(),
ready_state = connecting :: connecting | open | closed,
close_msg :: {non_neg_integer(), string()},
callback,
state,
handle :: handle()
handle :: handle()
}).
-define(ETS, sockjs_table).

Expand Down Expand Up @@ -107,13 +107,13 @@ mark_waiting(Pid, State = #session{response_pid = Pid,
disconnect_tref = undefined}) ->
State;
%% 2) Noone else waiting - link and start heartbeat timeout.
mark_waiting(Pid, State = #session{response_pid = undefined,
disconnect_tref = DisconnectTRef,
heartbeat_delay = HeartbeatDelay})
mark_waiting(Pid, State = #session{response_pid = undefined,
disconnect_tref = DisconnectTRef,
server_heartbeat_interval = ServerHeartbeatInterval})
when DisconnectTRef =/= undefined ->
link(Pid),
_ = sockjs_util:cancel_send_after(DisconnectTRef, session_timeout),
TRef = erlang:send_after(HeartbeatDelay, self(), heartbeat_triggered),
TRef = erlang:send_after(ServerHeartbeatInterval, self(), heartbeat_triggered),
State#session{response_pid = Pid,
disconnect_tref = undefined,
heartbeat_tref = TRef}.
Expand Down Expand Up @@ -174,25 +174,25 @@ emit(What, State = #session{callback = Callback,
%% --------------------------------------------------------------------------

-spec init({session_or_undefined(), service(), info()}) -> {ok, #session{}}.
init({SessionId, #service{callback = Callback,
state = UserState,
disconnect_delay = DisconnectDelay,
heartbeat_delay = HeartbeatDelay}, Info}) ->
init({SessionId, #service{callback = Callback,
state = UserState,
disconnect_delay = DisconnectDelay,
server_heartbeat_interval = ServerHeartbeatInterval}, Info}) ->
case SessionId of
undefined -> ok;
_Else -> ets:insert(?ETS, {SessionId, self()})
end,
process_flag(trap_exit, true),
TRef = erlang:send_after(DisconnectDelay, self(), session_timeout),
{ok, #session{id = SessionId,
callback = Callback,
state = UserState,
response_pid = undefined,
disconnect_tref = TRef,
disconnect_delay = DisconnectDelay,
heartbeat_tref = undefined,
heartbeat_delay = HeartbeatDelay,
handle = {?MODULE, {self(), Info}}}}.
{ok, #session{id = SessionId,
callback = Callback,
state = UserState,
response_pid = undefined,
disconnect_tref = TRef,
disconnect_delay = DisconnectDelay,
heartbeat_tref = undefined,
server_heartbeat_interval = ServerHeartbeatInterval,
handle = {?MODULE, {self(), Info}}}}.


handle_call({reply, Pid, _Multiple}, _From, State = #session{
Expand Down