softshipper
softshipper

Reputation: 34071

Writing connection pool

How can I write a connection for example for database. The awesome package Redis provide the type pool for cache redis database connections.

How can I write this kind of connection pool, that caches database connections?

Upvotes: 2

Views: 150

Answers (1)

thwd
thwd

Reputation: 24818

These are the exported fields of redis.Pool:

type Pool struct {
    Dial         func() (Conn, error)
    TestOnBorrow func(c Conn, t time.Time) error
    MaxIdle      int
    MaxActive    int
    IdleTimeout  time.Duration
}
  • Dial is needed to create new connections on-demand. That is, when the pool has lended all its connections out and a new connection is requested from it.
  • TestOnBorrow will check a connection's health before it is lended to a user. If it returns an error, a new connection will be created (using Dial) and the old connection will be closed/discarded.
  • MaxIdle is the amount of not-lended connections which are contained in the pool. A pool will not create any new connections (through Dial) if it has this many connections.
  • MaxActive is the amount of total connections that the pool will ever manage at any given time. Lended plus not-lended connections.
  • IdleTimeout is a duration after which a connection that has been sitting in the pool unlended will be closed and a new one opened in it's stead.

I use the word "lending" instead of "borrowing" because the direction in which data flows (who's providing and who's consuming) is clearer.

To implement such a pool you also need to wrap your connections (as the redis package does) to return them to the pool when a user calls Close() on them. Additionally, a read/write timestamp is kept on each connection in order to provide the idle-timeout functionality.

In order to be concurrently usable, you also need to provide access-locking and atomic counting of connections so that you can never surpass the maximum amount of idle/active connections.

The actual set of connections managed by a redis connection pool is kept in a list.List structure in the Pool's idle field.

Upvotes: 1

Related Questions