Directory
- Poolboy
- Implementation ideas
- Process diagram
- State structure
- Key functions
- Child_spec
- Init
- New_woker
- Checkout
- Checkin
- Transcation
- Handle_info
- Questions
Poolboy
The Poolboy is a lightweight, versatile, high-performance, highly available Erlang Pooling Library.
Implementation ideas
- Use Gen_server to ensure multithreading is available
- Using link, supversior to monitor the management worker
- Use Mornitor to monitor administrative tasks
- Checkout Error Automatic Cancel_waiting
- Worker thread error, auto checkin
Process diagram
Introduction of the Processes:
- Poolboy is the Poolboy management process, can be used to each poolboy set a name, different names of the Poolboy completely independent
- Poolboy_sup is a supervsior, monitoring all the Poolboy_worker
- Poolboy_worker is the work process
- Client_process is a process that uses poolboy
The relationship between the processes:
- PID of Poolboy_sup in Poolboy process
- Poolboy_sup supervise all poolboy_worker processes, exit Poolboy_sup will exit all poolboy_worker processes at the same time
- Poolboy process link All poolboy_worker process, in the Poolboy_worker process exit when the corresponding processing, in the same vein, poolboy process hangs out of the time to end all the poolboy_worker process, here with Li Another layer of NK is considered to be differentiated from the process of the next name Mornitor.
- Poolboy process Mornitor All client_process processes, automatically check_in corresponding poolboy_worker processes at the end of the client_process process
State structure
- Waiting: Waiting queue
- Monitors: Monitor ETS
- Supversior:pool_boy Supversior PID
- Size:worker number Limit
- Workers: Work process
- Max_overflow:worker Overflow Restrictions
- Overflow: Current Overflow
- Strategry:lifo or FIFO
Key function Child_spec
- Start Poolboy Gen_server, you can pass the process name in.
Init
- Create angle wait queue waiting queue
- Chuang Kok Mornitors ETS
- Store waiting queue and mornitors ets Tid in state
- Creating Poolboy_sup and workers
Supported options
Poolargs:
- Worker_module:worker module name, starting Pool_boy supversior, incoming worker_module parameter
- Size: Initializes the #state. Size, and Init eventually creates the number of worker
- Max_overflow: Initializes the #state. Max_overflow
- Strategray: Adding a worker policy
New_woker
- New_worker (SUP): Create worker under Sup, = = Link worker to Poolboy = =, monitor worker status
- New_worker (Sup, frompid): Do not link worker, but monitor frompid
Checkout
checkout(Pool, Block, Timeout)
handle_call({checkout, CRef, Block}, {FromPid, _} = From, State)
:
- When a worker is present:
- Monitor the frompid and insert the monitoring relationship {Workerid, CRef, mref} into the Mornitor ETS.
- Remove the workers used
- Already overflow, not exceeding the upper limit
- New_worker (Sup, frompid), monitoring frompid
- Save the monitoring relationship to Mornitor ETS
- Overflow + 1
- Block = False
{reply, full, state};
- []
- Monitoring Frompid
- Save {frompid, CRef, mref} to the waiting queue
When there is an abnormality cacel_waiting: = = Necessity = =, prevent the fun (Worker) from collapsing, automatically checkin:
- Delete the cref corresponding item in the Mornitor ETS or waiting queue
Checkin
gen_server:cast(Pool, {checkin, Worker}).
- Find the corresponding monitoring relationship, release the monitoring relationship
- Handle_checkin
- There is a wait queue that is transferred to a task in the wait queue using
- There is no wait queue and overflow, delete worker, overflow-1
- Otherwise, the worker is added to the state#state.workers
Transcation
transaction(Pool, Fun, Timeout) -> Worker = poolboy:checkout(Pool, true, Timeout), try Fun(Worker) after ok = poolboy:checkin(Pool, Worker) end.
Handle_info
handle_info({'DOWN', MRef, _, _, _}, State)
When the user process collapses:
- Delete monitoring relationship, Handle_checkin, auto checkin
- Or delete the wait queue
- The worker:exit process is similar to handle_check_in at work, but pay attention to the new worker
- Idle worker: rebuild new, = = Do not automatically restart? = =, Supvisor strategy for never restart, self-management
Questions
- Poolboy_sup is not responsible for poolboy_worker restart, just responsible for Poolboy_worker exit, then end all poolboy_worker with link, what is the difference?
- ends Poolboy_worker by supversior, execution poolboy_worker:terminate
- ends Poolboy_worker through linked process, which requires To Poolboy_worker set trapexit
- Checkout you did not use the self PID as a task ref, but re-make_ref, commenting that it was to resolve some cases Checko UT timeout will not automatically checkin, need to wait for client_process exit to be checkin, not figured is why?