From 39528b555d2764842b1ba602a075295d61842489 Mon Sep 17 00:00:00 2001 From: benjaminplee Date: Tue, 18 Nov 2014 22:24:55 +0000 Subject: [PATCH] Made ets table names clearer by usage Reuse of ibrowse_lb for named table holding load balancers and for name of unnamed table holding connections was confusing. --- include/ibrowse.hrl | 3 +++ src/ibrowse.erl | 22 +++++++++++----------- src/ibrowse_lb.erl | 12 ++++++------ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/include/ibrowse.hrl b/include/ibrowse.hrl index 18dde82..76ab236 100644 --- a/include/ibrowse.hrl +++ b/include/ibrowse.hrl @@ -18,4 +18,7 @@ -record(ibrowse_conf, {key, value}). +-define(CONNECTIONS_LOCAL_TABLE, ibrowse_lb). +-define(LOAD_BALANCER_NAMED_TABLE, ibrowse_lb). + -endif. diff --git a/src/ibrowse.erl b/src/ibrowse.erl index 85bb75c..b541398 100644 --- a/src/ibrowse.erl +++ b/src/ibrowse.erl @@ -317,7 +317,7 @@ send_req(Url, Headers, Method, Body, Options, Timeout) -> #url{host = Host, port = Port, protocol = Protocol} = Parsed_url -> - Lb_pid = case ets:lookup(ibrowse_lb, {Host, Port}) of + Lb_pid = case ets:lookup(?LOAD_BALANCER_NAMED_TABLE, {Host, Port}) of [] -> get_lb_pid(Parsed_url); [#lb_pid{pid = Lb_pid_1}] -> @@ -659,7 +659,7 @@ get_metrics() -> true; (_) -> false - end, ets:tab2list(ibrowse_lb)), + end, ets:tab2list(?LOAD_BALANCER_NAMED_TABLE)), All_ets = ets:all(), lists:map(fun({lb_pid, {Host, Port}, Lb_pid}) -> case lists:dropwhile( @@ -678,7 +678,7 @@ get_metrics() -> end, Dests). get_metrics(Host, Port) -> - case ets:lookup(ibrowse_lb, {Host, Port}) of + case ets:lookup(?LOAD_BALANCER_NAMED_TABLE, {Host, Port}) of [] -> no_active_processes; [#lb_pid{pid = Lb_pid}] -> @@ -746,9 +746,9 @@ init(_) -> State = #state{}, put(my_trace_flag, State#state.trace), put(ibrowse_trace_token, "ibrowse"), - ibrowse_lb = ets:new(ibrowse_lb, [named_table, public, {keypos, 2}]), - ibrowse_conf = ets:new(ibrowse_conf, [named_table, protected, {keypos, 2}]), - ibrowse_stream = ets:new(ibrowse_stream, [named_table, public]), + ?LOAD_BALANCER_NAMED_TABLE = ets:new(?LOAD_BALANCER_NAMED_TABLE, [named_table, public, {keypos, 2}]), + ibrowse_conf = ets:new(ibrowse_conf, [named_table, protected, {keypos, 2}]), + ibrowse_stream = ets:new(ibrowse_stream, [named_table, public]), import_config(), {ok, #state{}}. @@ -833,7 +833,7 @@ set_config_value(Key, Val) -> %% {stop, Reason, State} (terminate/2 is called) %%-------------------------------------------------------------------- handle_call({get_lb_pid, #url{host = Host, port = Port} = Url}, _From, State) -> - Pid = do_get_connection(Url, ets:lookup(ibrowse_lb, {Host, Port})), + Pid = do_get_connection(Url, ets:lookup(?LOAD_BALANCER_NAMED_TABLE, {Host, Port})), {reply, Pid, State}; handle_call(stop, _From, State) -> @@ -841,7 +841,7 @@ handle_call(stop, _From, State) -> ets:foldl(fun(#lb_pid{pid = Pid}, Acc) -> ibrowse_lb:stop(Pid), Acc - end, [], ibrowse_lb), + end, [], ?LOAD_BALANCER_NAMED_TABLE), {stop, normal, ok, State}; handle_call({set_config_value, Key, Val}, _From, State) -> @@ -899,7 +899,7 @@ handle_info(all_trace_off, State) -> (_, Acc) -> Acc end, - ets:foldl(Fun, undefined, ibrowse_lb), + ets:foldl(Fun, undefined, ?LOAD_BALANCER_NAMED_TABLE), ets:select_delete(ibrowse_conf, [{{ibrowse_conf,{trace,'$1','$2'},true},[],['true']}]), {noreply, State}; @@ -915,7 +915,7 @@ handle_info({trace, Bool, Host, Port}, State) -> (_, Acc) -> Acc end, - ets:foldl(Fun, undefined, ibrowse_lb), + ets:foldl(Fun, undefined, ?LOAD_BALANCER_NAMED_TABLE), ets:insert(ibrowse_conf, #ibrowse_conf{key = {trace, Host, Port}, value = Bool}), {noreply, State}; @@ -944,7 +944,7 @@ code_change(_OldVsn, State, _Extra) -> %%-------------------------------------------------------------------- do_get_connection(#url{host = Host, port = Port}, []) -> {ok, Pid} = ibrowse_lb:start_link([Host, Port]), - ets:insert(ibrowse_lb, #lb_pid{host_port = {Host, Port}, pid = Pid}), + ets:insert(?LOAD_BALANCER_NAMED_TABLE, #lb_pid{host_port = {Host, Port}, pid = Pid}), Pid; do_get_connection(_Url, [#lb_pid{pid = Pid}]) -> Pid. diff --git a/src/ibrowse_lb.erl b/src/ibrowse_lb.erl index aa381a4..9f49e0d 100644 --- a/src/ibrowse_lb.erl +++ b/src/ibrowse_lb.erl @@ -70,7 +70,7 @@ init([Host, Port]) -> Max_pipe_sz = ibrowse:get_config_value({max_pipeline_size, Host, Port}, 10), put(my_trace_flag, ibrowse_lib:get_trace_status(Host, Port)), put(ibrowse_trace_token, ["LB: ", Host, $:, integer_to_list(Port)]), - Tid = ets:new(ibrowse_lb, [public, ordered_set]), + Tid = ets:new(?CONNECTIONS_LOCAL_TABLE, [public, ordered_set]), {ok, #state{parent_pid = whereis(ibrowse), host = Host, port = Port, @@ -199,9 +199,9 @@ handle_info({trace, Bool}, #state{ets_tid = Tid} = State) -> handle_info(timeout, State) -> %% We can't shutdown the process immediately because a request %% might be in flight. So we first remove the entry from the - %% ibrowse_lb ets table, and then shutdown a couple of seconds - %% later - ets:delete(ibrowse_lb, {State#state.host, State#state.port}), + %% load balancer named ets table, and then shutdown a couple + %% of seconds later + ets:delete(?LOAD_BALANCER_NAMED_TABLE, {State#state.host, State#state.port}), erlang:send_after(2000, self(), shutdown), {noreply, State#state{proc_state = shutting_down}}; @@ -219,7 +219,7 @@ handle_info(_Info, State) -> terminate(_Reason, #state{host = Host, port = Port}) -> % Use delete_object instead of delete in case another process for this host/port % has been spawned, in which case will be deleting the wrong record because pid won't match. - ets:delete_object(ibrowse_lb, #lb_pid{host_port = {Host, Port}, pid = self()}), + ets:delete_object(?LOAD_BALANCER_NAMED_TABLE, #lb_pid{host_port = {Host, Port}, pid = self()}), ok. %%-------------------------------------------------------------------- @@ -257,7 +257,7 @@ find_best_connection(Pid, Tid, Max_pipe) -> end. maybe_create_ets(#state{ets_tid = undefined} = State) -> - Tid = ets:new(ibrowse_lb, [public, ordered_set]), + Tid = ets:new(?CONNECTIONS_LOCAL_TABLE, [public, ordered_set]), State#state{ets_tid = Tid}; maybe_create_ets(State) -> State.