Search code examples
algorithmconnection-pooling

what's the best algorithm to find a free connection in a pool?


I need to program an application that manages a pool of connection. I would like to know which algorithms should be studied.


Solution

  • A typical Pool implementation would have a free bucket stack:

    • when a connection is requested, pop it from the stack
    • when a connection is given back to the pool, push it on the stack

    This favors re-use of the last used connection, which is good for caching.

    When giving a connection to the user, you'll use RAII so that it'll get back to the Pool automatically (and deterministically) when all references to it are dropped.

    Now, it's up to you to decide how to handle the events:

    • Request of a connection when there is none available (you can wait, build a new one, etc...)
    • Get back a connection when there are already a lot of it in the stack (do we want to keep so many of them ?)

    Those are implementation details of your pool and should be adapted depending on your requirements.