Documentation
¶
Overview ¶
Package pool implements a connection pool for redis connections which is thread-safe.
Basic usage ¶
The basic use-case is to create a pool and then pass that pool amongst multiple go-routines, each of which can use it safely. To retrieve a connection you use Get, and to return the connection to the pool when you're done with it you use Put.
p, err := pool.New("tcp", "localhost:6379", 10) if err != nil { // handle error } // In another go-routine conn, err := p.Get() if err != nil { // handle error } if conn.Cmd("SOME", "CMD").Err != nil { // handle error } p.Put(conn)
Shortcuts ¶
If you're doing multiple operations you may find it useful to defer the Put right after retrieving a connection, so that you don't have to always remember to do so
conn, err := p.Get() if err != nil { // handle error } defer p.Put(conn) if conn.Cmd("SOME", "CMD").Err != nil { // handle error } if conn.Cmd("SOME", "OTHER", "CMD").Err != nil { // handle error }
Additionally there is the Cmd method on Pool, which handles Get-ing and Put-ing for you in the case of only wanting to execute a single command
r := p.Cmd("SOME", "CMD") if r.Err != nil { // handle error }
Custom connections ¶
Sometimes it's necessary to run some code on each connection in a pool upon its creation, for example in the case of AUTH. This can be done with NewCustom, like so
df := func(network, addr string) (*redis.Client, error) { client, err := redis.Dial(network, addr) if err != nil { return nil, err } if err = client.Cmd("AUTH", "SUPERSECRET").Err; err != nil { client.Close() return nil, err } return client, nil } p, err := pool.NewCustom("tcp", "127.0.0.1:6379", 10, df)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pool ¶
type Pool struct {
// The network/address that the pool is connecting to. These are going to be
// whatever was passed into the New function. These should not be
// changed after the pool is initialized
Network, Addr string
// contains filtered or unexported fields
}
Pool is a simple connection pool for redis Clients. It will create a small pool of initial connections, and if more connections are needed they will be created on demand. If a connection is Put back and the pool is full it will be closed.
func New ¶
New creates a new Pool whose connections are all created using redis.Dial(network, addr). The size indicates the maximum number of idle connections to have waiting to be used at any given moment. If an error is encountered an empty (but still usable) pool is returned alongside that error
func NewCustom ¶
NewCustom is like New except you can specify a DialFunc which will be used when creating new connections for the pool. The common use-case is to do authentication for new connections.
func (*Pool) Avail ¶
Avail returns the number of connections currently available to be gotten from the Pool using Get. If the number is zero then subsequent calls to Get will be creating new connections on the fly
func (*Pool) Cmd ¶
Cmd automatically gets one client from the pool, executes the given command (returning its result), and puts the client back in the pool
func (*Pool) Empty ¶
func (p *Pool) Empty()
Empty removes and calls Close() on all the connections currently in the pool. Assuming there are no other connections waiting to be Put back this method effectively closes and cleans up the pool.