diff options
author | Tim Fox <tim@rabbitmq.com> | 2011-02-11 12:07:40 +0000 |
---|---|---|
committer | Tim Fox <tim@rabbitmq.com> | 2011-02-11 12:07:40 +0000 |
commit | 2344574821599928d2af80a6026c92278f266a7e (patch) | |
tree | b8b0efa3124e652602e796a01b2444531cd72506 /src/file_handle_cache.erl | |
parent | 06d42a9d8b565a900f0d9af8b166d76baf806f19 (diff) | |
download | rabbitmq-server-2344574821599928d2af80a6026c92278f266a7e.tar.gz |
Refactored handle_call({obtain, ...}, ...) into single headed function
Diffstat (limited to 'src/file_handle_cache.erl')
-rw-r--r-- | src/file_handle_cache.erl | 71 |
1 files changed, 49 insertions, 22 deletions
diff --git a/src/file_handle_cache.erl b/src/file_handle_cache.erl index a1b8efc1..9bb7abd7 100644 --- a/src/file_handle_cache.erl +++ b/src/file_handle_cache.erl @@ -849,33 +849,60 @@ handle_call({open, Pid, Requested, EldestUnusedSince}, From, false -> {noreply, run_pending_item(Item, State1)} end; -handle_call({obtain, Pid}, From, State = #fhc_state { obtain_limit = Limit, - obtain_count = Count, - obtain_pending = Pending, - clients = Clients }) - when Limit =/= infinity andalso Count >= Limit -> - ok = track_client(Pid, Clients), - true = ets:update_element(Clients, Pid, {#cstate.blocked, true}), - Item = #pending { kind = obtain, pid = Pid, requested = 1, from = From }, - {noreply, State #fhc_state { obtain_pending = pending_in(Item, Pending) }}; +%% handle_call({obtain, Pid}, From, State = #fhc_state { obtain_limit = Limit, +%% obtain_count = Count, +%% obtain_pending = Pending, +%% clients = Clients }) +%% when Limit =/= infinity andalso Count >= Limit -> +%% ok = track_client(Pid, Clients), +%% true = ets:update_element(Clients, Pid, {#cstate.blocked, true}), +%% Item = #pending { kind = obtain, pid = Pid, requested = 1, from = From }, +%% {noreply, State #fhc_state { obtain_pending = pending_in(Item, Pending) }}; + +%% handle_call({obtain, Pid}, From, State = #fhc_state { obtain_count = Count, +%% obtain_pending = Pending, +%% clients = Clients }) -> +%% Item = #pending { kind = obtain, pid = Pid, requested = 1, from = From }, +%% ok = track_client(Pid, Clients), +%% case needs_reduce(State #fhc_state { obtain_count = Count + 1 }) of +%% true -> +%% true = ets:update_element(Clients, Pid, {#cstate.blocked, true}), +%% {noreply, reduce(State #fhc_state { +%% obtain_pending = pending_in(Item, Pending) })}; +%% false -> +%% {noreply, adjust_alarm(State, run_pending_item(Item, State))} +%% end; + handle_call({obtain, Pid}, From, State = #fhc_state { obtain_count = Count, obtain_pending = Pending, clients = Clients }) -> - Item = #pending { kind = obtain, pid = Pid, requested = 1, from = From }, ok = track_client(Pid, Clients), - case needs_reduce(State #fhc_state { obtain_count = Count + 1 }) of - true -> - true = ets:update_element(Clients, Pid, {#cstate.blocked, true}), - {noreply, reduce(State #fhc_state { - obtain_pending = pending_in(Item, Pending) })}; - false -> - {noreply, adjust_alarm(State, run_pending_item(Item, State))} - end; + Item = #pending { kind = obtain, pid = Pid, requested = 1, from = From }, + Enqueue = fun () -> + true = ets:update_element(Clients, Pid, + {#cstate.blocked, true}), + State #fhc_state { + obtain_pending = pending_in(Item, Pending) } + end, + {noreply, + case obtain_limit_reached(State) of + true -> Enqueue(); + false -> case needs_reduce(State #fhc_state { + obtain_count = Count + 1 }) of + true -> reduce(Enqueue()); + false -> adjust_alarm( + State, run_pending_item(Item, State)) + end + end}; + handle_call({set_limit, Limit}, _From, State) -> - {reply, ok, maybe_reduce( - adjust_alarm(State, process_pending(State #fhc_state { - limit = Limit, - obtain_limit = obtain_limit(Limit) })))}; + {reply, ok, adjust_alarm( + State, maybe_reduce( + process_pending( + State #fhc_state { + limit = Limit, + obtain_limit = obtain_limit(Limit) })))}; + handle_call(get_limit, _From, State = #fhc_state { limit = Limit }) -> {reply, Limit, State}. |