top

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 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.

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.

Imports 3 package(s)

  1. github.com/mediocregopher/radix.v2/pool
  2. github.com/mediocregopher/radix.v2/pubsub
  3. github.com/mediocregopher/radix.v2/redis

Test imports 2 package(s)

  1. github.com/stretchr/testify/require
  2. github.com/stretchr/testify/assert