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 ¶
const NUM_SLOTS = 16384
Variables ¶
var BadCmdNoKey = &redis.CmdError{errors.New("bad command, no key")}
Functions ¶
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 ¶
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 ¶
Same as NewCluster, but will use timeout as the read/write timeout when communicating with cluster nodes
func (*Cluster) ClientForKey ¶
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) Cmd ¶
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 ¶
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.