diff options
Diffstat (limited to 'src/bullet_handler.erl')
-rw-r--r-- | src/bullet_handler.erl | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/bullet_handler.erl b/src/bullet_handler.erl index 55a81d9..bcfc442 100644 --- a/src/bullet_handler.erl +++ b/src/bullet_handler.erl @@ -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) -> @@ -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 @@ -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; @@ -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. @@ -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. |