Documentation
¶
Overview ¶
Package sentinel 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, err := client.GetMaster("bucket0") if err != nil { return redisErr } defer client.PutMaster("bucket0", conn) i, err := conn.Cmd("GET", "foo").Int() if err != nil { return err } if err := conn.Cmd("SET", "foo", i+1); err != nil { return err } return nil }
This package only guarantees 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.
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
}
Client communicates with a sentinel instance and manages connection pools of active masters
func NewClient ¶
NewClient creates a sentinel client. Connects to the given sentinel instance, pulls the information for the masters of the given names, and creates an initial 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 NewClientCustom ¶
func NewClientCustom( network, address string, poolSize int, df DialFunc, names ...string, ) ( *Client, error, )
NewClientCustom is the same as NewClient, except it takes in a DialFunc which will be used to create all new connections to the master instances. This can be used to implement authentication, custom timeouts, etc...
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 }
ClientError is 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
Error implements the error protocol