Skip to content
This repository has been archived by the owner on Dec 11, 2018. It is now read-only.

Commit

Permalink
Make timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
lpgauth committed Mar 11, 2014
1 parent 2df0f57 commit 7e5c391
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/bullet_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
handler :: module(),
handler_state :: term(),
% poll or eventsource for GET requests
get_mode :: 'undefined' | 'poll' | 'eventsource'
get_mode :: 'undefined' | 'poll' | 'eventsource',
timeout = infinity :: timeout()
}).

-define(TIMEOUT, 60000). %% @todo Configurable.

%% HTTP.

init(Transport, Req, Opts) ->
Expand All @@ -53,6 +52,7 @@ init(Transport, Req, Opts) ->

init(Transport, Req, Opts, <<"GET">>) ->
{handler, Handler} = lists:keyfind(handler, 1, Opts),
Timeout = get_value(timeout, Opts, 60000),
State = #state{handler=Handler},
{GetMode, Req2} = get_mode(Req),
Active = case GetMode of
Expand All @@ -64,7 +64,7 @@ init(Transport, Req, Opts, <<"GET">>) ->
{ok, Req4} = start_get_mode(GetMode, Req3),
Req5 = cowboy_req:compact(Req4),
{loop, Req5, State#state{handler_state=HandlerState,
get_mode=GetMode}, ?TIMEOUT, hibernate};
get_mode=GetMode}, Timeout, hibernate};
{shutdown, Req3, HandlerState} ->
{shutdown, Req3, State#state{handler_state=HandlerState}}
end;
Expand Down Expand Up @@ -126,12 +126,13 @@ terminate(_Reason, Req, #state{handler=Handler, handler_state=HandlerState}) ->

websocket_init(Transport, Req, Opts) ->
{handler, Handler} = lists:keyfind(handler, 1, Opts),
Timeout = get_value(timeout, Opts, 60000),
State = #state{handler=Handler},
case Handler:init(Transport, Req, Opts, true) of
{ok, Req2, HandlerState} ->
Req3 = cowboy_req:compact(Req2),
{ok, Req3, State#state{handler_state=HandlerState},
?TIMEOUT, hibernate};
Timeout, hibernate};
{shutdown, Req2, _HandlerState} ->
{shutdown, Req2}
end.
Expand Down Expand Up @@ -193,3 +194,13 @@ reply_get_mode(eventsource, Data, Req) ->
Line <- binary:split(Bin, [<<"\r\n">>, <<"\r">>, <<"\n">>], [global])],
ok = cowboy_req:chunk([Event, <<"\n">>], Req),
{loop, Req}.

%% Internal.

%% @doc Faster alternative to proplists:get_value/3.
%% @private
get_value(Key, Opts, Default) ->
case lists:keyfind(Key, 1, Opts) of
{_, Value} -> Value;
_ -> Default
end.

0 comments on commit 7e5c391

Please sign in to comment.