Content Entry

Pool design in Go

Published: 2013-03-27 Categories: Tech Tags: Pool Go

Network programming case:

  • Database connection
  • Network/Socket connection
  • Workflow Queue
  • Other ...
To improve system efficiency in the implementation, we need to create a fixed capacity of Pool.

Each task needs to wait and pull a free connection resources from Pool; when finished task must push the connection resources back to Pool immediately, so that it can be reused.

The typical method,  use the list and lock

  • Linked list used to persistence connection handle
  • the lock used to pull/push connection resources.

A more concise implementation, use go-channel 

  • channel itself can be maked as a fixed-size pool
  • channel's send/receive methods are equivalent to pull/push

The source code:

Compared to list / lock,  the go-channel is better

  • The code is simple, easy to understand
  • Higher performance.  go-channel 2,600,000 per-second; list-lock 1,200,000 per-second. (my CPU: Intel Core i5-750)
--EOF--

comments loading