Skip to content

Instantly share code, notes, and snippets.

@rnewson
Created October 15, 2018 19:58
Show Gist options
  • Save rnewson/9a3aa9dffea0880d3c2a22c0b94c12b8 to your computer and use it in GitHub Desktop.
Save rnewson/9a3aa9dffea0880d3c2a22c0b94c12b8 to your computer and use it in GitHub Desktop.
change cloudant auth caching
diff --git a/src/chttpd/src/chttpd_sup.erl b/src/chttpd/src/chttpd_sup.erl
index fe84b67eb..afdcbcb8d 100644
--- a/src/chttpd/src/chttpd_sup.erl
+++ b/src/chttpd/src/chttpd_sup.erl
@@ -80,20 +80,20 @@ maybe_replace(Key, Value, Settings) ->
end.
lru_opts() ->
- case config:get("chttpd_auth_cache", "max_objects") of
- MxObjs when is_integer(MxObjs), MxObjs > 0 ->
+ case config:get_integer("chttpd_auth_cache", "max_objects", 0) of
+ MxObjs when MxObjs > 0 ->
[{max_objects, MxObjs}];
_ ->
[]
end ++
- case config:get("chttpd_auth_cache", "max_size", "104857600") of
- MxSize when is_integer(MxSize), MxSize > 0 ->
+ case config:get_integer("chttpd_auth_cache", "max_size", 104857600) of
+ MxSize when MxSize > 0 ->
[{max_size, MxSize}];
_ ->
[]
end ++
- case config:get("chttpd_auth_cache", "max_lifetime", "600000") of
- MxLT when is_integer(MxLT), MxLT > 0 ->
+ case config:get_integer("chttpd_auth_cache", "max_lifetime", 600000) of
+ MxLT when MxLT > 0 ->
[{max_lifetime, MxLT}];
_ ->
[]
diff --git a/src/cloudant_auth_cache.erl b/src/cloudant_auth_cache.erl
index a2fa978..c575c87 100644
--- a/src/cloudant_auth_cache.erl
+++ b/src/cloudant_auth_cache.erl
@@ -11,40 +11,19 @@
% the License.
-module(cloudant_auth_cache).
--behaviour(gen_server).
-
-export([
- start_link/0,
get_user/2,
get_user_creds/2,
update_user_creds/3
]).
--export([
- init/1,
- terminate/2,
- handle_call/3,
- handle_cast/2,
- handle_info/2,
- code_change/3
-]).
-
--export([
- listen_for_changes/1,
- changes_callback/2
-]).
-
-export([get_user_config/1]).
-export([get_customer/1]).
-export([ensure_auth_ddoc_exists/3]).
-export([auth_ddoc_validate_function/1]).
-%% For testing
--export([state/0]).
--export([set_restart_listener_delay/1]).
-
-include_lib("couch/include/couch_db.hrl").
-include("couch_js_functions.hrl").
-include("cloudant_js_functions.hrl").
@@ -52,21 +31,6 @@
-define(CACHE, chttpd_auth_cache_lru).
-define(USER_CONFIG_FIELD, <<"config">>).
--record(state, {
- changes_pid,
- last_seq="now",
- restart_listener_delay = 5000
-}).
-
-
-start_link() ->
- gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-
-%% For testing and debugging
-state() ->
- [_ | Fields] = tuple_to_list(sys:get_state(?MODULE)),
- lists:zip(record_info(fields, state), Fields).
-
get_user(Req, UserName) ->
case get_user_creds(Req, UserName) of
nil ->
@@ -132,50 +96,6 @@ get_user_config(Req) ->
Config
end.
-set_restart_listener_delay(Delay) ->
- gen_server:call(?MODULE, {restart_listener_delay, Delay}).
-
-%% gen_server callbacks
-
-init([]) ->
- {ok, #state{changes_pid = spawn_changes("now")}}.
-
-
-terminate(_Reason, #state{changes_pid = Pid}) ->
- exit(Pid, kill).
-
-
-handle_call({restart_listener_delay, Delay}, _From, #state{} = State) ->
- {reply, ok, State#state{restart_listener_delay = Delay}};
-handle_call(_Call, _From, State) ->
- {noreply, State}.
-
-
-handle_cast(_Msg, State) ->
- {noreply, State}.
-
-
-handle_info({'DOWN', _, _, Pid, Reason}, #state{changes_pid=Pid} = State) ->
- Seq = case Reason of
- {seq, EndSeq} ->
- EndSeq;
- _ ->
- couch_log:notice("~p changes listener died ~p", [?MODULE, Reason]),
- 0
- end,
- #state{restart_listener_delay = Delay} = State,
- erlang:send_after(Delay, self(), {start_listener, Seq}),
- {noreply, State#state{last_seq=Seq}};
-handle_info({start_listener, Seq}, State) ->
- {noreply, State#state{changes_pid = spawn_changes(Seq)}};
-handle_info(_Msg, State) ->
- {noreply, State}.
-
-
-code_change(_OldVsn, #state{}=State, _Extra) ->
- {ok, State}.
-
-
% The #user_ctx record is a bear to upgrade, hence this awfulness. The
% cloudant_auth module will strip out the allowed_clusters field
apply_allowed_clusters(Props) when is_list(Props) ->
@@ -271,38 +191,6 @@ load_user_from_db(DbName, UserName) ->
nil
end.
-
-spawn_changes(Since) ->
- {Pid, _} = spawn_monitor(?MODULE, listen_for_changes, [Since]),
- Pid.
-
-
-listen_for_changes(Since) ->
- CBFun = fun ?MODULE:changes_callback/2,
- Args = #changes_args{
- feed = "continuous",
- since = Since,
- heartbeat = true,
- filter = {default, main_only}
- },
- fabric:changes(global_dbname(), CBFun, Since, Args).
-
-
-changes_callback(start, Since) ->
- {ok, Since};
-changes_callback({stop, EndSeq, _Pending}, _) ->
- exit({seq, EndSeq});
-changes_callback({change, {Change}}, _) ->
- UserName = couch_util:get_value(id, Change),
- ets_lru:remove(?CACHE, UserName),
- {ok, couch_util:get_value(seq, Change)};
-changes_callback(timeout, EndSeq) ->
- {ok, EndSeq};
-changes_callback({error, _}, EndSeq) ->
- exit({seq, EndSeq});
-changes_callback(waiting_for_updates, Acc) ->
- {ok, Acc}.
-
make_admin_doc(HashedPwd, Salt) ->
[
{<<"roles">>, admin_roles()},
diff --git a/src/cloudant_epi.erl b/src/cloudant_epi.erl
index dcb5e97..b28ddd2 100644
--- a/src/cloudant_epi.erl
+++ b/src/cloudant_epi.erl
@@ -38,7 +38,7 @@ data_providers() ->
[].
processes() ->
- [{chttpd_epi, [?CHILD(chttpd_auth_cache, cloudant_auth_cache, worker)]}].
+ [].
notify(_Key, _Old, _New) ->
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment