开发者

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

开发者 https://www.devze.com 2023-01-23 23:34 出处:网络
I need to program an application that manages a pool of connection. I would like to kn开发者_StackOverflowow which algorithms should be studied.A typical Pool implementation would have a free bucket s

I need to program an application that manages a pool of connection. I would like to kn开发者_StackOverflowow which algorithms should be studied.


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.


Well there is really no complicated algorithm necessary here. You just need two buckets, one for used connections and for free connections. At the beginning all connections are in the free bucket. If a connection is requested, you take one from the free bucket and put it in the used bucket. If it is no longer used, you put it back in the free bucket.

0

精彩评论

暂无评论...
验证码 换一张
取 消