sentinel

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2015 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

The sentinel package provides a convenient interface with a redis sentinel which will automatically handle pooling connections and automatic failover.

Here's an example of creating a sentinel client and then using it to perform some commands

func example() error {
	// If there exists sentinel masters "bucket0" and "bucket1", and we want out
	// client to create pools for both:
	client, err := sentinel.NewClient("tcp", "localhost:6379", 100, "bucket0", "bucket1")
	if err != nil {
		return err
	}

	if err := exampleCmd(client); err != nil {
		return err
	}

	return nil
}

func exampleCmd(client *sentinel.Client) error {
	conn, redisErr := client.GetMaster("bucket0")
	if redisErr != nil {
		return redisErr
	}
	// We use CarefullyPutMaster to conditionally put the connection back in the
	// pool depending on the last error seen
	defer client.CarefullyPutMaster("bucket0", conn, &redisErr)

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

	if redisErr = conn.Cmd("SET", "foo", i+1); redisErr != nil {
		return redisErr
	}

	return nil
}

This package only gaurantees that when GetMaster is called the returned connection will be a connection to the master as of the moment that method is called. It is still possible that there is a failover as that connection is being used by the application. The Readonly() method on CmdError will be helpful if you want to gracefully handle this case.

As a final note, a Client can be interacted with from multiple routines at once safely, except for the Close method. To safely Close, ensure that only one routine ever makes the call and that once the call is made no other methods are ever called by any routines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(
	network, address string, poolSize int, names ...string,
) (
	*Client, error,
)

Creates a sentinel client. Connects to the given sentinel instance, pulls the information for the masters of the given names, and creates an intial pool of connections for each master. The client will automatically replace the pool for any master should sentinel decide to fail the master over. The returned error is a *ClientError.

func (*Client) CarefullyPutMaster added in v0.5.1

func (c *Client) CarefullyPutMaster(
	name string, client *redis.Client, potentialErr *error,
)

A useful helper method, analagous to the pool package's CarefullyPut method. Since we don't want to Put a connection which is having connectivity issues, this can be defered inside a function to make sure we only put back a connection when we should. It should be used like the following:

func doSomeThings(c *Client) error {
	conn, redisErr := c.GetMaster("bucket0")
	if redisErr != nil {
		return redisErr
	}
	defer c.CarefullyPutMaster("bucket0", 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
}

func (*Client) Close

func (c *Client) Close()

Closes all connection pools as well as the connection to sentinel.

func (*Client) GetMaster

func (c *Client) GetMaster(name string) (*redis.Client, error)

Retrieves a connection for the master of the given name. If sentinel has become unreachable this will always return an error. Close should be called in that case. The returned error is a *ClientError.

func (*Client) PutMaster

func (c *Client) PutMaster(name string, client *redis.Client)

Return a connection for a master of a given name. As with the pool package, do not return a connection which is having connectivity issues, or which is otherwise unable to perform requests.

type ClientError

type ClientError struct {

	// If this is true the error is due to a problem with the sentinel
	// connection, either it being closed or otherwise unavailable. If false the
	// error is due to some other circumstances. This is useful if you want to
	// implement some kind of reconnecting to sentinel on an error.
	SentinelErr bool
	// contains filtered or unexported fields
}

An error wrapper returned by operations in this package. It implements the error interface and can therefore be passed around as a normal error.

func (*ClientError) Error

func (ce *ClientError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL