|
| 1 | +%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- |
| 2 | +%% ex: ts=4 sw=4 et |
| 3 | +%% The contents of this file are subject to the Mozilla Public License |
| 4 | +%% Version 1.1 (the "License"); you may not use this file except in |
| 5 | +%% compliance with the License. You may obtain a copy of the License |
| 6 | +%% at http://www.mozilla.org/MPL/ |
| 7 | +%% |
| 8 | +%% Software distributed under the License is distributed on an "AS IS" |
| 9 | +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 10 | +%% the License for the specific language governing rights and |
| 11 | +%% limitations under the License. |
| 12 | +%% |
| 13 | +%% The Original Code is RabbitMQ. |
| 14 | +%% |
| 15 | +%% The Initial Developer of the Original Code is GoPivotal, Inc. |
| 16 | +%% Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved. |
| 17 | +%% |
| 18 | + |
| 19 | +-module(code_server_cache). |
| 20 | + |
| 21 | +-behaviour(gen_server). |
| 22 | + |
| 23 | +%% API |
| 24 | +-export([start_link/0, |
| 25 | + maybe_call_mfa/4]). |
| 26 | + |
| 27 | +%% gen_server callbacks |
| 28 | +-export([init/1, |
| 29 | + handle_call/3, |
| 30 | + handle_cast/2, |
| 31 | + handle_info/2, |
| 32 | + terminate/2, |
| 33 | + code_change/3]). |
| 34 | + |
| 35 | +-record(state, { |
| 36 | + modules = #{} :: #{atom() => boolean()} |
| 37 | +}). |
| 38 | + |
| 39 | +%% API |
| 40 | +start_link() -> |
| 41 | + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). |
| 42 | + |
| 43 | +maybe_call_mfa(Module, Function, Args, Default) -> |
| 44 | + gen_server:call(?MODULE, {maybe_call_mfa, {Module, Function, Args, Default}}). |
| 45 | + |
| 46 | +%% gen_server callbacks |
| 47 | + |
| 48 | +init([]) -> |
| 49 | + {ok, #state{}}. |
| 50 | + |
| 51 | +handle_call({maybe_call_mfa, {Mod, _F, _A, _D} = MFA}, _From, #state{modules = ModuleMap} = State0) -> |
| 52 | + Value = maps:get(Mod, ModuleMap, true), |
| 53 | + {ok, Reply, State1} = handle_maybe_call_mfa(Value, MFA, State0), |
| 54 | + {reply, Reply, State1}; |
| 55 | +handle_call(_Request, _From, State) -> |
| 56 | + {reply, ignored, State}. |
| 57 | + |
| 58 | +handle_cast(_Msg, State) -> |
| 59 | + {noreply, State}. |
| 60 | + |
| 61 | +handle_info(_Info, State) -> |
| 62 | + {noreply, State}. |
| 63 | + |
| 64 | +terminate(_Reason, _State) -> |
| 65 | + ok. |
| 66 | + |
| 67 | +code_change(_OldVsn, State, _Extra) -> |
| 68 | + {ok, State}. |
| 69 | + |
| 70 | +%% Internal functions |
| 71 | + |
| 72 | +handle_maybe_call_mfa(false, {_M, _F, _A, Default}, State) -> |
| 73 | + {ok, Default, State}; |
| 74 | +handle_maybe_call_mfa(true, {Module, Function, Args, Default}, State) -> |
| 75 | + try |
| 76 | + Reply = erlang:apply(Module, Function, Args), |
| 77 | + {ok, Reply, State} |
| 78 | + catch |
| 79 | + error:undef -> |
| 80 | + handle_maybe_call_mfa_error(Module, Default, State); |
| 81 | + Err:Reason -> |
| 82 | + rabbit_log:error("Calling ~p:~p failed: ~p:~p~n", |
| 83 | + [Module, Function, Err, Reason]), |
| 84 | + handle_maybe_call_mfa_error(Module, Default, State) |
| 85 | + end. |
| 86 | + |
| 87 | +handle_maybe_call_mfa_error(Module, Default, #state{modules = ModuleMap0} = State0) -> |
| 88 | + ModuleMap1 = maps:put(Module, false, ModuleMap0), |
| 89 | + State1 = State0#state{modules = ModuleMap1}, |
| 90 | + {ok, Default, State1}. |
0 commit comments