cluster

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2014 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

The cluster package implements an almost drop-in replacement for a normal Client which accounts for a redis cluster setup. It will transparently redirect requests to the correct nodes, as well as keep track of which slots are mapped to which nodes and updating them accordingly so requests can remain as fast as possible.

This package will initially call `cluster slots` in order to retrieve an initial idea of the topology of the cluster, but other than that will not make any other extraneous calls.

Index

Constants

View Source
const NUM_SLOTS = 16384

Variables

View Source
var BadCmdNoKey = &redis.CmdError{errors.New("bad command, no key")}

Functions

func CRC16

func CRC16(buf []byte) uint16

CRC16 returns checksum for a given set of bytes based on the crc algorithm defined for hashing redis keys in a cluster setup

Types

type Cluster

type Cluster struct {

	// Number of slot misses. This is incremented everytime a command's reply is
	// a MOVED or ASK message
	Misses uint64
	// contains filtered or unexported fields
}

Cluster wraps a Client and accounts for all redis cluster logic

func NewCluster

func NewCluster(addr string) (*Cluster, error)

NewCluster will perform the following steps to initialize:

- Connect to the node given in the argument

- User that node to call CLUSTER SLOTS. The return from this is used to build a mapping of slot number -> connection. At the same time any new connections which need to be made are created here.

- *Cluster is returned

At this point the Cluster has a complete view of the cluster's topology and can immediately start performing commands with (theoretically) zero slot misses

func NewClusterTimeout

func NewClusterTimeout(addr string, timeout time.Duration) (*Cluster, error)

Same as NewCluster, but will use timeout as the read/write timeout when communicating with cluster nodes

func (*Cluster) ClientForKey

func (c *Cluster) ClientForKey(key string) (*redis.Client, string, error)

ClientForKey returns the Client which *ought* to handle the given key (along with the node address for that client), based on Cluster's understanding of the cluster topology at the given moment. If the slot isn't known or there is an error contacting the correct node, a random client is returned

func (*Cluster) Close

func (c *Cluster) Close()

Close calls Close on all connected clients

func (*Cluster) Cmd

func (c *Cluster) Cmd(cmd string, args ...interface{}) *redis.Reply

Cmd performs the given command on the correct cluster node and gives back the command's reply. The command *must* have a key parameter (i.e. len(args) >= 1). If any MOVED or ASK errors are returned they will be transparently handled by this method. This method will also increment the Misses field on the Cluster struct whenever a redirection occurs

func (*Cluster) Reset

func (c *Cluster) Reset() error

Reset will re-retrieve the cluster topology and set up/teardown connections as necessary. It begins by calling CLUSTER SLOTS on a random known connection. The return from that is used to re-create the topology, create any missing clients, and close any clients which are no longer needed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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