Просмотр исходного кода

Merge pull request #42 from dcheckoway/master

Don't bother queueing when Timeout=0
Seth Falcon 10 лет назад
Родитель
Сommit
3022a65ce6
2 измененных файлов с 31 добавлено и 1 удалено
  1. 2 0
      src/pooler.erl
  2. 29 1
      test/pooler_tests.erl

+ 2 - 0
src/pooler.erl

@@ -582,6 +582,8 @@ take_member_from_pool_queued(Pool0 = #pool{queue_max = QMax,
             send_metric(Pool1, events, error_no_members, history),
             send_metric(Pool1, queue_max_reached, {inc, 1}, counter),
             {error_no_members, Pool1};
+        {{error_no_members, Pool1}, _} when Timeout =:= 0 ->
+            {error_no_members, Pool1};
         {{error_no_members, Pool1 = #pool{queued_requestors = QueuedRequestors}}, QueueCount} ->
             TRef = erlang:send_after(Timeout, self(), {requestor_timeout, From}),
             send_metric(Pool1, queue_count, QueueCount, histogram),

+ 29 - 1
test/pooler_tests.erl

@@ -341,7 +341,7 @@ basic_tests() ->
                ?assertEqual(ok, pooler:rm_pool(dyn_pool_1))
        end},
 
-      {"metrics have been called",
+      {"metrics have been called (no timeout/queue)",
        fun() ->
                %% exercise the API to ensure we have certain keys reported as metrics
                fake_metrics:reset_metrics(),
@@ -362,6 +362,34 @@ basic_tests() ->
                                         <<"pooler.test_pool_1.in_use_count">>,
                                         <<"pooler.test_pool_1.killed_free_count">>,
                                         <<"pooler.test_pool_1.killed_in_use_count">>,
+                                        <<"pooler.test_pool_1.take_rate">>]),
+               Metrics = fake_metrics:get_metrics(),
+               GotKeys = lists:usort([ Name || {Name, _, _} <- Metrics ]),
+               ?assertEqual(ExpectKeys, GotKeys)
+       end},
+
+      {"metrics have been called (with timeout/queue)",
+       fun() ->
+               %% exercise the API to ensure we have certain keys reported as metrics
+               fake_metrics:reset_metrics(),
+               %% pass a non-zero timeout here to exercise queueing
+               Pids = [ pooler:take_member(test_pool_1, 1) || _I <- lists:seq(1, 10) ],
+               [ pooler:return_member(test_pool_1, P) || P <- Pids ],
+               catch pooler:take_member(bad_pool_name),
+               %% kill and unused member
+               exit(hd(Pids), kill),
+               %% kill a used member
+               KillMe = pooler:take_member(test_pool_1),
+               exit(KillMe, kill),
+               %% FIXME: We need to wait for pooler to process the
+               %% exit message. This is ugly, will fix later.
+               timer:sleep(200),                % :(
+               ExpectKeys = lists:sort([<<"pooler.test_pool_1.error_no_members_count">>,
+                                        <<"pooler.test_pool_1.events">>,
+                                        <<"pooler.test_pool_1.free_count">>,
+                                        <<"pooler.test_pool_1.in_use_count">>,
+                                        <<"pooler.test_pool_1.killed_free_count">>,
+                                        <<"pooler.test_pool_1.killed_in_use_count">>,
                                         <<"pooler.test_pool_1.take_rate">>,
                                         <<"pooler.test_pool_1.queue_count">>]),
                Metrics = fake_metrics:get_metrics(),