Permalink
Browse files

Merge pull request #39 from juise/master

Unregister a timer for deleted meters
  • Loading branch information...
2 parents cb7d7d3 + e1c5fc5 commit a87ef8aca55a06483d2c539d27b459c628dbe8e7 @joewilliams joewilliams committed Oct 6, 2012
Showing with 22 additions and 4 deletions.
  1. +2 −0 src/folsom_ets.erl
  2. +13 −1 src/folsom_meter_timer_server.erl
  3. +7 −3 test/folsom_erlang_checks.erl
View
@@ -251,10 +251,12 @@ delete_metric(Name, gauge) ->
true = ets:delete(?FOLSOM_TABLE, Name),
ok;
delete_metric(Name, meter) ->
+ ok = folsom_meter_timer_server:unregister(Name),
true = ets:delete(?METER_TABLE, Name),
true = ets:delete(?FOLSOM_TABLE, Name),
ok;
delete_metric(Name, meter_reader) ->
+ ok = folsom_meter_timer_server:unregister(Name),
true = ets:delete(?METER_READER_TABLE, Name),
true = ets:delete(?FOLSOM_TABLE, Name),
ok;
@@ -28,7 +28,7 @@
-behaviour(gen_server).
%% API
--export([start_link/0, register/2, dump/0]).
+-export([start_link/0, register/2, unregister/1, dump/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -98,6 +98,15 @@ handle_call({register, Name, Module}, _From, State) ->
#state{registered_timers = NewList}
end,
{reply, ok, NewState};
+handle_call({unregister, Name}, _From, State) ->
+ NewState = case proplists:is_defined(Name, State#state.registered_timers) of
+ true ->
+ Ref = proplists:get_value(Name, State#state.registered_timers),
+ {ok, cancel} = timer:cancel(Ref),
+ #state{registered_timers = proplists:delete(Name, State#state.registered_timers)};
+ false -> State
+ end,
+ {reply, ok, NewState};
handle_call(dump, _From, State) ->
{reply, State, State}.
@@ -160,5 +169,8 @@ code_change(_OldVsn, State, _Extra) ->
register(Name, Module) ->
gen_server:call(?SERVER, {register, Name, Module}).
+unregister(Name) ->
+ gen_server:call(?SERVER, {unregister, Name}).
+
dump() ->
gen_server:call(?SERVER, dump).
@@ -67,7 +67,7 @@ create_metrics() ->
ok = folsom_metrics:new_spiral(spiral),
?debugFmt("ensuring meter tick is registered with gen_server~n", []),
- ok = ensure_meter_tick_exists(),
+ ok = ensure_meter_tick_exists(2),
?debugFmt("ensuring multiple timer registrations dont cause issues", []),
ok = folsom_meter_timer_server:register(meter, folsom_metrics_meter),
@@ -220,6 +220,8 @@ delete_metrics() ->
ok = folsom_metrics:delete_metric(timed),
ok = folsom_metrics:delete_metric(testcounter),
+ ok = ensure_meter_tick_exists(2),
+
1 = length(ets:tab2list(?METER_TABLE)),
ok = folsom_metrics:delete_metric(meter),
0 = length(ets:tab2list(?METER_TABLE)),
@@ -228,6 +230,8 @@ delete_metrics() ->
ok = folsom_metrics:delete_metric(meter_reader),
0 = length(ets:tab2list(?METER_READER_TABLE)),
+ ok = ensure_meter_tick_exists(0),
+
ok = folsom_metrics:delete_metric(duration),
ok = folsom_metrics:delete_metric(spiral),
@@ -259,9 +263,9 @@ counter_metric(Count, Counter) ->
0 = Result.
-ensure_meter_tick_exists() ->
+ensure_meter_tick_exists(MeterCnt) ->
{state, State} = folsom_meter_timer_server:dump(),
- 2 = length(State),
+ MeterCnt = length(State),
ok.
%% internal function

0 comments on commit a87ef8a

Please sign in to comment.