couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject [02/19] mochiweb commit: updated refs/heads/upstream to bd6ae7c
Date Wed, 09 Sep 2015 08:42:45 GMT
performance: cache date

as the formatting of the date for each request is quite expensive
we cache it in an ets table now.

heavily inspired by cowboy, but does not use binaries and uses
still httpd_util:rfc1183 internally.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/commit/9aad1236
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/tree/9aad1236
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/diff/9aad1236

Branch: refs/heads/upstream
Commit: 9aad123606a5930d44f4ff8a4bdef83b72917571
Parents: b66b68d
Author: Robert Kowalski <rok@kowalski.gd>
Authored: Mon Jul 20 02:46:38 2015 +0200
Committer: Robert Kowalski <rok@kowalski.gd>
Committed: Tue Jul 28 20:59:19 2015 +0200

----------------------------------------------------------------------
 src/mochiweb_clock.erl   | 101 ++++++++++++++++++++++++++++++++++++++++++
 src/mochiweb_http.erl    |   4 ++
 src/mochiweb_request.erl |   2 +-
 3 files changed, 106 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/9aad1236/src/mochiweb_clock.erl
----------------------------------------------------------------------
diff --git a/src/mochiweb_clock.erl b/src/mochiweb_clock.erl
new file mode 100644
index 0000000..3c9f464
--- /dev/null
+++ b/src/mochiweb_clock.erl
@@ -0,0 +1,101 @@
+%% Copyright (c) 2011-2014, Loïc Hoguin <essen@ninenines.eu>
+%% Copyright (c) 2015, Robert Kowalski <rok@kowalski.gd>
+%%
+%% Permission to use, copy, modify, and/or distribute this software for any
+%% purpose with or without fee is hereby granted, provided that the above
+%% copyright notice and this permission notice appear in all copies.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+%% While a gen_server process runs in the background to update
+%% the cache of formatted dates every second, all API calls are
+%% local and directly read from the ETS cache table, providing
+%% fast time and date computations.
+
+-module(mochiweb_clock).
+
+-behaviour(gen_server).
+
+%% API.
+-export([start_link/0]).
+-export([start/0]).
+-export([stop/0]).
+-export([rfc1123/0]).
+
+%% gen_server.
+-export([init/1]).
+-export([handle_call/3]).
+-export([handle_cast/2]).
+-export([handle_info/2]).
+-export([terminate/2]).
+-export([code_change/3]).
+
+-record(state, {}).
+
+%% API.
+
+-spec start_link() -> {ok, pid()}.
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+-spec start() -> {ok, pid()}.
+start() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+-spec stop() -> stopped.
+stop() ->
+    gen_server:call(?MODULE, stop).
+
+-spec rfc1123() -> string().
+rfc1123() ->
+    case ets:lookup(lala, rfc1123) of
+        [{rfc1123, Date}] ->
+            Date;
+        [] ->
+            <<"">>
+    end.
+
+%% gen_server.
+
+-spec init([]) -> {ok, #state{}}.
+init([]) ->
+    lala = ets:new(lala, [named_table, protected]),
+    handle_info(update_date, #state{}),
+    timer:send_interval(1000, update_date),
+    {ok, #state{}}.
+
+-type from() :: {pid(), term()}.
+-spec handle_call
+    (stop, from(), State) -> {stop, normal, stopped, State}
+    when State::#state{}.
+handle_call(stop, _From, State) ->
+    {stop, normal, stopped, State};
+handle_call(_Request, _From, State) ->
+    {reply, ignored, State}.
+
+-spec handle_cast(_, State) -> {noreply, State} when State::#state{}.
+handle_cast(_Msg, State) ->
+    {noreply, State}.
+
+-spec handle_info(any(), State) -> {noreply, State} when State::#state{}.
+handle_info(update_date, State) ->
+    Date = httpd_util:rfc1123_date(),
+    ets:insert(lala, {rfc1123, Date}),
+    {noreply, State};
+handle_info(_Info, State) ->
+    {noreply, State}.
+
+-spec terminate(_, _) -> ok.
+terminate(_Reason, _State) ->
+    ok.
+
+-spec code_change(_, State, _) -> {ok, State} when State::#state{}.
+code_change(_OldVsn, State, _Extra) ->
+    {ok, State}.
+

http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/9aad1236/src/mochiweb_http.erl
----------------------------------------------------------------------
diff --git a/src/mochiweb_http.erl b/src/mochiweb_http.erl
index d83fdda..a1e909d 100644
--- a/src/mochiweb_http.erl
+++ b/src/mochiweb_http.erl
@@ -48,9 +48,11 @@ parse_options(Options) ->
     mochilists:set_defaults(?DEFAULTS, Options1).
 
 stop() ->
+    mochiweb_clock:stop(),
     mochiweb_socket_server:stop(?MODULE).
 
 stop(Name) ->
+    mochiweb_clock:stop(),
     mochiweb_socket_server:stop(Name).
 
 %% @spec start(Options) -> ServerRet
@@ -65,9 +67,11 @@ stop(Name) ->
 %%      The proplist is as follows: [{name, Name}, {port, Port}, {active_sockets, ActiveSockets},
{timing, Timing}].
 %% @end
 start(Options) ->
+    {ok, _Pid} = mochiweb_clock:start(),
     mochiweb_socket_server:start(parse_options(Options)).
 
 start_link(Options) ->
+    {ok, _Pid} = mochiweb_clock:start_link(),
     mochiweb_socket_server:start_link(parse_options(Options)).
 
 loop(Socket, Opts, Body) ->

http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/9aad1236/src/mochiweb_request.erl
----------------------------------------------------------------------
diff --git a/src/mochiweb_request.erl b/src/mochiweb_request.erl
index 99352d2..093d9ba 100644
--- a/src/mochiweb_request.erl
+++ b/src/mochiweb_request.erl
@@ -691,7 +691,7 @@ maybe_serve_file(File, ExtraHeaders, {?MODULE, [_Socket, _Opts, _Method,
_RawPat
 
 server_headers() ->
     [{"Server", "MochiWeb/1.0 (" ++ ?QUIP ++ ")"},
-     {"Date", httpd_util:rfc1123_date()}].
+     {"Date", mochiweb_clock:rfc1123()}].
 
 make_code(X) when is_integer(X) ->
     [integer_to_list(X), [" " | httpd_util:reason_phrase(X)]];


Mime
View raw message