From 5bb6438e10ae6b620b88f8d9989e6f6c0d252f34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 1 Nov 2017 17:06:37 +0000 Subject: 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. --- src/cowboy_clock.erl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/cowboy_clock.erl b/src/cowboy_clock.erl index e0e387d..28f8a1b 100644 --- a/src/cowboy_clock.erl +++ b/src/cowboy_clock.erl @@ -49,9 +49,17 @@ start_link() -> 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(). 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(). rfc1123(DateTime) -> -- cgit v1.2.3