Refactor gun pooling yet again
Third time is the charm, eh?
The problem with the current pooling mechanism is that we have to:
- Keep an additional worker process for every connection
- Keep track of clients manually
- Trust the client to checkin and checkouts by itself. In theory this shouldn't be a problem, however there has been situations where the client doesn't return the connection either by mistake or some inexplicable reasons. The current solution to this is to monitor the clients, but it's not ideal.
All of this can be avoided by simply utilizing gun event handlers (see: https://github.com/ninenines/gun/blob/master/src/gun_default_event_h.erl). here is an example of how this would work:
- A client request a connection
- A new connection is being created with a custom handler (still need to determine how to start the connection under an own DynamicSupervisor while retaining option checking facilities, will probably have to send a patch to gun)
- Handler is called with
init
event and registers the process in the registry - The pid of the connection is returned to the client
- The client makes a request
- Handler is called with
request_start
event and adds the request reference to it's internal state - Request finishes
- Handler receives
request_end
event and removes the request reference from it's internal state. It then realizes it has no references left in it's internal state, meaning that all requests are finished. It then calls:timer.kill_after(idle_timeout)
and puts the timer reference as it's registry value - When the next client wants the same connection, the timer is canceled
In addition, this time I would like to move the gun pool into a separate library. It really feels out of place in the Pleroma codebase
cc @href
Edited by rinpatch