大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
PoolBoy
Checkout
ready({checkout, Block, Timeout}, {FromPid, _}=From, State) ->
#state{supervisor = Sup,
workers = Workers,
monitors = Monitors,
max_overflow = MaxOverflow} = State,
case queue:out(Workers) of
{{value, Pid}, Left} ->
Ref = erlang:monitor(process, FromPid),
true = ets:insert(Monitors, {Pid, Ref}),
NextState = case queue:is_empty(Left) of
true when MaxOverflow < 1 -> full;
true -> overflow;
false -> ready
end,
{reply, Pid, NextState, State#state{workers=Left}};
{empty, Empty} when MaxOverflow > 0 ->
{Pid, Ref} = new_worker(Sup, FromPid),
true = ets:insert(Monitors, {Pid, Ref}),
{reply, Pid, overflow, State#state{workers=Empty, overflow=1}};
{empty, Empty} when Block =:= false ->
{reply, full, full, State#state{workers=Empty}};
{empty, Empty} ->
Waiting = add_waiting(From, Timeout, State#state.waiting),
{next_state, full, State#state{workers=Empty, waiting=Waiting}}
end;
Checkout
出一个worker
从worker Queue
中,假设有,则monitor(ets)
这个worker
,然后依据队列的容量和MaxOverflow
的值来确定下一状态为full。overflow,ready (ready + overflow <= full)
;假设没有。而MaxOverFlow
的值大于0
,则新建一个worker
,并将其增加monitor
。最后重置状态项worker = empty
, overflow = 1
;假设没有,而且MaxOverflow
小于1, Block == false
,则{reply, full, full, State#state{workers=Empty}}
;如过没有。MaxOverflow < 1
, Block == true
,则 {next_state, full, State#state{workers=Empty, waiting=Waiting}}
。
Checkin
ready({checkin, Pid}, State) ->
Monitors = State#state.monitors,
case ets:lookup(Monitors, Pid) of
[{Pid, Ref}] ->
true = erlang:demonitor(Ref),
true = ets:delete(Monitors, Pid),
Workers = queue:in(Pid, State#state.workers),
{next_state, ready, State#state{workers=Workers}};
[] ->
{next_state, ready, State}
end;
从Monitor
中剔除相应的worker
。然后回收到worker queue
中去。
状态转变
状态转变的计算:
worker_queue_size(当前size) + maxoverflow
。
ready
仅仅与当前worker_queque_size
有关,overflow
和worker_queue_size(0)
和maxoverflow>0
有关。full
和work_queue_size(0), overfllow = maxoverflow
有关。
worker的来源
全部的worker
要么在初始化时创建平;要么调用checkout
时。经过poolboy
创建。但此时创建的worker
没有进到worker queue
。要想进到worker queue
,仅仅能调用checkin
。
work pid 回收到worker_queue中
checkin_while_full
–》{empty, Empty} when MaxOverflow < 1 ->
;
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117047.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...