mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Don't crash when cowboy_clock is not running
This can happen normally when Cowboy is restarted, for example. Instead of failing requests when that happens, we degrade gracefully and do a little more work to provide the current date header.
This commit is contained in:
parent
836342abb8
commit
5bb6438e10
2 changed files with 36 additions and 3 deletions
|
@ -49,9 +49,17 @@ start_link() ->
|
||||||
stop() ->
|
stop() ->
|
||||||
gen_server:call(?MODULE, stop).
|
gen_server:call(?MODULE, stop).
|
||||||
|
|
||||||
|
%% When the ets table doesn't exist, either because of a bug
|
||||||
|
%% or because Cowboy is being restarted, we perform in a
|
||||||
|
%% slightly degraded state and build a new timestamp for
|
||||||
|
%% every request.
|
||||||
-spec rfc1123() -> binary().
|
-spec rfc1123() -> binary().
|
||||||
rfc1123() ->
|
rfc1123() ->
|
||||||
ets:lookup_element(?MODULE, rfc1123, 2).
|
try
|
||||||
|
ets:lookup_element(?MODULE, rfc1123, 2)
|
||||||
|
catch error:badarg ->
|
||||||
|
rfc1123(erlang:universaltime())
|
||||||
|
end.
|
||||||
|
|
||||||
-spec rfc1123(calendar:datetime()) -> binary().
|
-spec rfc1123(calendar:datetime()) -> binary().
|
||||||
rfc1123(DateTime) ->
|
rfc1123(DateTime) ->
|
||||||
|
|
|
@ -23,9 +23,14 @@ all() ->
|
||||||
[{group, no_env}|cowboy_test:common_all()].
|
[{group, no_env}|cowboy_test:common_all()].
|
||||||
|
|
||||||
groups() ->
|
groups() ->
|
||||||
Common = ct_helper:all(?MODULE) -- [set_env_missing],
|
Common = ct_helper:all(?MODULE) -- [restart_gracefully, set_env_missing],
|
||||||
[{no_env, [], [set_env_missing]}|cowboy_test:common_groups(Common)].
|
[
|
||||||
|
{app, [], [restart_gracefully]},
|
||||||
|
{no_env, [], [set_env_missing]}
|
||||||
|
|cowboy_test:common_groups(Common)].
|
||||||
|
|
||||||
|
init_per_group(app, Config) ->
|
||||||
|
cowboy_test:init_common_groups(http, Config, ?MODULE);
|
||||||
init_per_group(Name=no_env, Config) ->
|
init_per_group(Name=no_env, Config) ->
|
||||||
cowboy_test:init_http(Name, #{}, Config);
|
cowboy_test:init_http(Name, #{}, Config);
|
||||||
init_per_group(Name, Config) ->
|
init_per_group(Name, Config) ->
|
||||||
|
@ -39,6 +44,26 @@ init_dispatch(_) ->
|
||||||
{"/", hello_h, []}
|
{"/", hello_h, []}
|
||||||
]}]).
|
]}]).
|
||||||
|
|
||||||
|
%% Tests.
|
||||||
|
|
||||||
|
restart_gracefully(Config) ->
|
||||||
|
doc("Ensure we can process request when the cowboy application is being restarted."),
|
||||||
|
ConnPid = gun_open(Config),
|
||||||
|
%% We can do a request before stopping cowboy.
|
||||||
|
Ref1 = gun:get(ConnPid, "/"),
|
||||||
|
{response, _, 200, _} = gun:await(ConnPid, Ref1),
|
||||||
|
%% Stop the cowboy application.
|
||||||
|
ok = application:stop(cowboy),
|
||||||
|
%% We can still do a request even though cowboy is stopped.
|
||||||
|
Ref2 = gun:get(ConnPid, "/"),
|
||||||
|
{response, _, 200, _} = gun:await(ConnPid, Ref2),
|
||||||
|
%% Start the cowboy application again.
|
||||||
|
ok = application:start(cowboy),
|
||||||
|
%% Even after restarting there are no issues.
|
||||||
|
Ref3 = gun:get(ConnPid, "/"),
|
||||||
|
{response, _, 200, _} = gun:await(ConnPid, Ref3),
|
||||||
|
ok.
|
||||||
|
|
||||||
router_invalid_path(Config) ->
|
router_invalid_path(Config) ->
|
||||||
doc("Ensure a path with invalid percent-encoded characters results in a 400."),
|
doc("Ensure a path with invalid percent-encoded characters results in a 400."),
|
||||||
ConnPid = gun_open(Config),
|
ConnPid = gun_open(Config),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue