radix: github.com/fzzy/radix/extra/pool Index | Files

package pool

import "github.com/fzzy/radix/extra/pool"

The pool package implements a connection pool for redis connections which is thread-safe

Index

Package Files

pool.go

type DialFunc Uses

type DialFunc func(network, addr string) (*redis.Client, error)

A function which can be passed into NewCustomPool

type Pool Uses

type Pool struct {
    // contains filtered or unexported fields
}

A simple connection pool. 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 returned and the pool is full it will be closed.

func NewCustomPool Uses

func NewCustomPool(network, addr string, size int, df DialFunc) (*Pool, error)

A Pool whose connections are all created using f(network, addr). The size indicates the maximum number of idle connections to have waiting to be used at any given moment. f will be used to create all new connections associated with this pool.

The following is an example of using NewCustomPool to have all connections automatically get AUTH called on them upon creation

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, _ := pool.NewCustomPool("tcp", "127.0.0.1:6379", 10, df)

func NewOrEmptyPool Uses

func NewOrEmptyPool(network, addr string, size int) *Pool

Calls NewPool, but if there is an error it return a pool of the same size but without any connections pre-initialized (can be used the same way, but if this happens there might be something wrong with the redis instance you're connecting to)

func NewPool Uses

func NewPool(network, addr string, size int) (*Pool, error)

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

func (*Pool) CarefullyPut Uses

func (p *Pool) CarefullyPut(conn *redis.Client, potentialErr *error)

A useful helper method which acts as a wrapper around Put. It will only actually Put the conn back if potentialErr is not an error or is a redis.CmdError. It would be used like the following:

func doSomeThings(p *Pool) error {
	conn, redisErr := p.Get()
	if redisErr != nil {
		return redisErr
	}
	defer p.CarefullyPut(conn, &redisErr)

	var i int
	i, redisErr = conn.Cmd("GET", "foo").Int()
	if redisErr != nil {
		return redisErr
	}

	redisErr = conn.Cmd("SET", "foo", i * 3).Err
	return redisErr
}

If we were just using the normal Put we wouldn't be able to defer it because we don't want to Put back a connection which is broken. This method takes care of doing that check so we can still use the convenient defer

func (*Pool) Empty Uses

func (p *Pool) 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.

func (*Pool) Get Uses

func (p *Pool) Get() (*redis.Client, error)

Retrieves an available redis client. If there are none available it will create a new one on the fly

func (*Pool) Put Uses

func (p *Pool) Put(conn *redis.Client)

Returns a client back to the pool. If the pool is full the client is closed instead. If the client is already closed (due to connection failure or what-have-you) it should not be put back in the pool. The pool will create more connections as needed.

Package pool imports 1 packages (graph) and is imported by 31 packages. Updated 2018-01-17. Refresh now. Tools for package owners.