views:

234

answers:

1

Is there a known algorithm for implementing a connection pool? If not what are the known algorithms and what are their trade-offs?
What design patterns are common when designing and programming a connection pool?
Are there any code examples implement a connection pool using boost.asio?
Is it a good idea to use a connection pool for persisting connections (not http)?
How is threading related to connection pooling? When do you need a new thread?

+4  A: 

If you are looking for a pure thread-pooling policy (may be a connection or any resource) there are two simple approaches viz:-

  1. Half Sync/Half Async Model (usually using using message queues to pass information).

  2. Leaders/Followers Model (usually using request queues to pass information).

The first approach goes like this:-

  • You create a pool of threads to handle a resource. Often this size (number of threads) needs to be configurable. Call these threads 'Workers'.
  • You then create a master thread that will dispatch the work to the Worker threads. The application program dispatches the task as a message to the master thread.
  • The master thread puts the same on the message Q of a chosen Worker thread and the Worker thread removes itself from the pool. Choosing and removing the Worker thread needs synchronization.
  • After the Worker completes the task, it returns to the thread-pool.

The master thread itself can consume the tasks it gets in FCFS or a prioritized manner. This will depend on your implementation.

The second model (Leader/Followers) goes something like this:-

  • Create a thread pool. Initially all are Workers. Then elect a Leader, automatically rest-all become followers. Note that electing a Leader has to be synchronized.
  • Put all the data to be processed on a single request Q.
  • The thread-pool Leader dequeues the task. It then immediately elects a new Leader and starts executing the task.
  • The new Leader picks up the next task.

There may be other approaches as well, but the ones outlined above are simple that work with most use-cases.

Half Sync/Half Async Major Weakness:-

  • Higher context switching, synchronization, and data copying overhead.

Leader/Follwers Major Weakness:-

  • Implementation complexity of Leader election in thread pool.

Now you can decide for yourself the more correct approach. HTH,

Abhay
This is really helpful. I thank you very much.The second approach sounds interesting but how would one select a leader? Would it be the thread object that is available? What if there are no availabe threads in the pool? Should the current leader execute the task and wait for another thread to become available or should it keep his role as a leader?Is the connection pool basically some collection of threads? Should it be a vector? A list? A set? In this approach how would I get the tasks need to be done (Sent by clients)? Is the leader responsible for that?
the_drow
After recieving the name of those common patternes I googled and found this: http://www.kircher-schwanninger.de/michael/publications/lf.pdfI will read it through and it looks interesting.
the_drow
@the_drow: Leader is just another thread but which has to take the task. There always has to be a leader, if all threads in the pool are busy then the task has to wait in the request Q for the new leader to be elected. Once there is a leader it will take care of the task. Again this request Q may be FCFS or priority based. The link you found is indeed a decent one, you may consider the approaches given there.
Abhay
@the_drow: see http://www.cs.wustl.edu/~schmidt/PDF/lf-PLOPD.pdf on how ACE implements it.
Abhay