machine_gun icon indicating copy to clipboard operation
machine_gun copied to clipboard

Eviction policy for idle connection pools

Open DmitryKakurin opened this issue 3 years ago • 6 comments

At what point connection pool pool_group:host:port is stopped if there is no activity? We have a server application where connection targets keep changing. I.e. we would make a lot of calls to host1:port1 (with pool size configured to 1000 connections) and then we will never call it again. Then we'll call host2:port2 a lot, and will not call it ever anymore too, etc. I suspect in the current implementation these connection pools will just keep running forever until Erlang reaches max number of processes and starts throwing system_limit errors. Is this correct or maybe I just cannot find the logic that stops these pools in the code?

DmitryKakurin avatar Aug 12 '20 00:08 DmitryKakurin

You can get all current pools by calling DynamicSupervisor.which_children, get pool's name such as :"group@host:port" using Process.info and then terminate a specific pool with DynamicSupervisor.terminate_child.

For example:

[{_, pool_pid, _, _} | _] = DynamicSupervisor.which_children(MachineGun.Supervisor)
{_, pool_name} = Process.info(pool_pid, :registered_name)
DynamicSupervisor.terminate_child(MachineGun.Supervisor, pool_pid)

petrohi avatar Aug 27 '20 03:08 petrohi

Thank you, this is useful! Do you have plans to implement automatic eviction policy based on pool idle time?

DmitryKakurin avatar Aug 27 '20 23:08 DmitryKakurin

This is definitely a good idea and a requirement for your use case. Currently I don't have the bandwidth to work on it, but the PR is always welcome.

petrohi avatar Aug 28 '20 00:08 petrohi

Is there a way to detect last usage time of a pool from pool_pid or pool_name? Also can the pool names be made into strings (so we don't have to worry about overflowing atoms table)?

DmitryKakurin avatar Sep 04 '20 22:09 DmitryKakurin

Is there a way to detect last usage time of a pool from pool_pid or pool_name?

None at the moment.

Also can the pool names be made into strings (so we don't have to worry about overflowing atoms table)?

This is good point and needs to be fixed for the general use case. Can you create separate issue for this?

petrohi avatar Sep 10 '20 19:09 petrohi

Created #24

DmitryKakurin avatar Sep 10 '20 21:09 DmitryKakurin