Skip to content

Commit

Permalink
Cleanup ws disconnected state (#34)
Browse files Browse the repository at this point in the history
* Cleanup ws disconnected state

* Use 'S' instead of 'State'
  • Loading branch information
ziopio authored Jun 13, 2024
1 parent 2ccd077 commit 27b25ac
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/grisp_connect_ws.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
ping_timer
}).

-define(disconnected_state,
#state{gun_pid = undefined, gun_ref = undefine,
ws_up = false, ping_timer = undefined}).

-include_lib("kernel/include/logger.hrl").

%--- API Functions -------------------------------------------------------------
Expand Down Expand Up @@ -76,8 +72,8 @@ handle_info({gun_up, GunPid, _}, #state{gun_pid = GunPid} = S) ->
?LOG_INFO(#{event => connection_enstablished}),
WsStream = gun:ws_upgrade(GunPid, "/grisp-connect/ws",[],
#{silence_pings => false}),
NewState = S#state{gun_pid = GunPid, ws_stream = WsStream},
{noreply, NewState};
NewS = S#state{gun_pid = GunPid, ws_stream = WsStream},
{noreply, NewS};
handle_info({gun_up, Pid, http}, #state{gun_pid = GunPid} = S) ->
?LOG_WARNING("Ignoring unexpected gun_up http message"
" from pid ~p, current pid is ~p", [Pid, GunPid]),
Expand Down Expand Up @@ -116,7 +112,7 @@ handle_info({'DOWN', _, process, Pid, Reason}, #state{gun_pid = Pid,
?LOG_WARNING(#{event => gun_crash, reason => Reason}),
timer:cancel(Tref),
grisp_connect_client:disconnected(),
{noreply, S?disconnected_state};
{noreply, disconnected_state(S)};
handle_info(ping_timeout, S) ->
?LOG_WARNING(#{event => ping_timeout}),
grisp_connect_client:disconnected(),
Expand All @@ -128,13 +124,17 @@ handle_info(M, S) ->
% internal functions -----------------------------------------------------------

shutdown_gun(#state{gun_pid = Pid, gun_ref = GunRef,
ping_timer = PingTimer} = State) ->
ping_timer = PingTimer} = S) ->
timer:cancel(PingTimer),
demonitor(GunRef),
gun:shutdown(Pid),
State?disconnected_state.
disconnected_state(S).

start_ping_timer() ->
{ok, Timeout} = application:get_env(grisp_connect, ws_ping_timeout),
{ok, Tref} = timer:send_after(Timeout, ping_timeout),
Tref.

disconnected_state(S) ->
S#state{gun_pid = undefined, gun_ref = undefined,
ws_up = false, ping_timer = undefined}.

0 comments on commit 27b25ac

Please sign in to comment.