forked from mirror/redis
Merge pull request #337 from go-redis/feature/fix-accessors
Ensure to use pointer methods where appropriate. Tidy up godoc.
This commit is contained in:
commit
5c3ab24e0a
404
commands.go
404
commands.go
File diff suppressed because it is too large
Load Diff
|
@ -3,7 +3,7 @@ package redis
|
|||
import "sync"
|
||||
|
||||
type Scanner struct {
|
||||
client cmdable
|
||||
client *cmdable
|
||||
*ScanCmd
|
||||
}
|
||||
|
||||
|
|
54
pipeline.go
54
pipeline.go
|
@ -22,32 +22,32 @@ type Pipeline struct {
|
|||
closed int32
|
||||
}
|
||||
|
||||
func (pipe *Pipeline) Process(cmd Cmder) error {
|
||||
pipe.mu.Lock()
|
||||
pipe.cmds = append(pipe.cmds, cmd)
|
||||
pipe.mu.Unlock()
|
||||
func (c *Pipeline) Process(cmd Cmder) error {
|
||||
c.mu.Lock()
|
||||
c.cmds = append(c.cmds, cmd)
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the pipeline, releasing any open resources.
|
||||
func (pipe *Pipeline) Close() error {
|
||||
atomic.StoreInt32(&pipe.closed, 1)
|
||||
pipe.Discard()
|
||||
func (c *Pipeline) Close() error {
|
||||
atomic.StoreInt32(&c.closed, 1)
|
||||
c.Discard()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pipe *Pipeline) isClosed() bool {
|
||||
return atomic.LoadInt32(&pipe.closed) == 1
|
||||
func (c *Pipeline) isClosed() bool {
|
||||
return atomic.LoadInt32(&c.closed) == 1
|
||||
}
|
||||
|
||||
// Discard resets the pipeline and discards queued commands.
|
||||
func (pipe *Pipeline) Discard() error {
|
||||
defer pipe.mu.Unlock()
|
||||
pipe.mu.Lock()
|
||||
if pipe.isClosed() {
|
||||
func (c *Pipeline) Discard() error {
|
||||
defer c.mu.Unlock()
|
||||
c.mu.Lock()
|
||||
if c.isClosed() {
|
||||
return pool.ErrClosed
|
||||
}
|
||||
pipe.cmds = pipe.cmds[:0]
|
||||
c.cmds = c.cmds[:0]
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -56,30 +56,30 @@ func (pipe *Pipeline) Discard() error {
|
|||
//
|
||||
// Exec always returns list of commands and error of the first failed
|
||||
// command if any.
|
||||
func (pipe *Pipeline) Exec() ([]Cmder, error) {
|
||||
if pipe.isClosed() {
|
||||
func (c *Pipeline) Exec() ([]Cmder, error) {
|
||||
if c.isClosed() {
|
||||
return nil, pool.ErrClosed
|
||||
}
|
||||
|
||||
defer pipe.mu.Unlock()
|
||||
pipe.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.mu.Lock()
|
||||
|
||||
if len(pipe.cmds) == 0 {
|
||||
return pipe.cmds, nil
|
||||
if len(c.cmds) == 0 {
|
||||
return c.cmds, nil
|
||||
}
|
||||
|
||||
cmds := pipe.cmds
|
||||
pipe.cmds = nil
|
||||
cmds := c.cmds
|
||||
c.cmds = nil
|
||||
|
||||
return cmds, pipe.exec(cmds)
|
||||
return cmds, c.exec(cmds)
|
||||
}
|
||||
|
||||
func (pipe *Pipeline) pipelined(fn func(*Pipeline) error) ([]Cmder, error) {
|
||||
if err := fn(pipe); err != nil {
|
||||
func (c *Pipeline) pipelined(fn func(*Pipeline) error) ([]Cmder, error) {
|
||||
if err := fn(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmds, err := pipe.Exec()
|
||||
_ = pipe.Close()
|
||||
cmds, err := c.Exec()
|
||||
_ = c.Close()
|
||||
return cmds, err
|
||||
}
|
||||
|
||||
|
|
106
ring.go
106
ring.go
|
@ -149,58 +149,58 @@ func NewRing(opt *RingOptions) *Ring {
|
|||
return ring
|
||||
}
|
||||
|
||||
func (ring *Ring) cmdInfo(name string) *CommandInfo {
|
||||
ring.cmdsInfoOnce.Do(func() {
|
||||
for _, shard := range ring.shards {
|
||||
func (c *Ring) cmdInfo(name string) *CommandInfo {
|
||||
c.cmdsInfoOnce.Do(func() {
|
||||
for _, shard := range c.shards {
|
||||
cmdsInfo, err := shard.Client.Command().Result()
|
||||
if err == nil {
|
||||
ring.cmdsInfo = cmdsInfo
|
||||
c.cmdsInfo = cmdsInfo
|
||||
return
|
||||
}
|
||||
}
|
||||
ring.cmdsInfoOnce = &sync.Once{}
|
||||
c.cmdsInfoOnce = &sync.Once{}
|
||||
})
|
||||
if ring.cmdsInfo == nil {
|
||||
if c.cmdsInfo == nil {
|
||||
return nil
|
||||
}
|
||||
return ring.cmdsInfo[name]
|
||||
return c.cmdsInfo[name]
|
||||
}
|
||||
|
||||
func (ring *Ring) cmdFirstKey(cmd Cmder) string {
|
||||
cmdInfo := ring.cmdInfo(cmd.arg(0))
|
||||
func (c *Ring) cmdFirstKey(cmd Cmder) string {
|
||||
cmdInfo := c.cmdInfo(cmd.arg(0))
|
||||
if cmdInfo == nil {
|
||||
return ""
|
||||
}
|
||||
return cmd.arg(int(cmdInfo.FirstKeyPos))
|
||||
}
|
||||
|
||||
func (ring *Ring) addClient(name string, cl *Client) {
|
||||
ring.mu.Lock()
|
||||
ring.hash.Add(name)
|
||||
ring.shards[name] = &ringShard{Client: cl}
|
||||
ring.mu.Unlock()
|
||||
func (c *Ring) addClient(name string, cl *Client) {
|
||||
c.mu.Lock()
|
||||
c.hash.Add(name)
|
||||
c.shards[name] = &ringShard{Client: cl}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (ring *Ring) getClient(key string) (*Client, error) {
|
||||
ring.mu.RLock()
|
||||
func (c *Ring) getClient(key string) (*Client, error) {
|
||||
c.mu.RLock()
|
||||
|
||||
if ring.closed {
|
||||
if c.closed {
|
||||
return nil, pool.ErrClosed
|
||||
}
|
||||
|
||||
name := ring.hash.Get(hashtag.Key(key))
|
||||
name := c.hash.Get(hashtag.Key(key))
|
||||
if name == "" {
|
||||
ring.mu.RUnlock()
|
||||
c.mu.RUnlock()
|
||||
return nil, errRingShardsDown
|
||||
}
|
||||
|
||||
cl := ring.shards[name].Client
|
||||
ring.mu.RUnlock()
|
||||
cl := c.shards[name].Client
|
||||
c.mu.RUnlock()
|
||||
return cl, nil
|
||||
}
|
||||
|
||||
func (ring *Ring) Process(cmd Cmder) error {
|
||||
cl, err := ring.getClient(ring.cmdFirstKey(cmd))
|
||||
func (c *Ring) Process(cmd Cmder) error {
|
||||
cl, err := c.getClient(c.cmdFirstKey(cmd))
|
||||
if err != nil {
|
||||
cmd.setErr(err)
|
||||
return err
|
||||
|
@ -208,34 +208,34 @@ func (ring *Ring) Process(cmd Cmder) error {
|
|||
return cl.baseClient.Process(cmd)
|
||||
}
|
||||
|
||||
// rebalance removes dead shards from the ring.
|
||||
func (ring *Ring) rebalance() {
|
||||
defer ring.mu.Unlock()
|
||||
ring.mu.Lock()
|
||||
// rebalance removes dead shards from the c.
|
||||
func (c *Ring) rebalance() {
|
||||
defer c.mu.Unlock()
|
||||
c.mu.Lock()
|
||||
|
||||
ring.hash = consistenthash.New(ring.nreplicas, nil)
|
||||
for name, shard := range ring.shards {
|
||||
c.hash = consistenthash.New(c.nreplicas, nil)
|
||||
for name, shard := range c.shards {
|
||||
if shard.IsUp() {
|
||||
ring.hash.Add(name)
|
||||
c.hash.Add(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// heartbeat monitors state of each shard in the ring.
|
||||
func (ring *Ring) heartbeat() {
|
||||
func (c *Ring) heartbeat() {
|
||||
ticker := time.NewTicker(100 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for _ = range ticker.C {
|
||||
var rebalance bool
|
||||
|
||||
ring.mu.RLock()
|
||||
c.mu.RLock()
|
||||
|
||||
if ring.closed {
|
||||
ring.mu.RUnlock()
|
||||
if c.closed {
|
||||
c.mu.RUnlock()
|
||||
break
|
||||
}
|
||||
|
||||
for _, shard := range ring.shards {
|
||||
for _, shard := range c.shards {
|
||||
err := shard.Client.Ping().Err()
|
||||
if shard.Vote(err == nil || err == pool.ErrPoolTimeout) {
|
||||
internal.Logf("ring shard state changed: %s", shard)
|
||||
|
@ -243,10 +243,10 @@ func (ring *Ring) heartbeat() {
|
|||
}
|
||||
}
|
||||
|
||||
ring.mu.RUnlock()
|
||||
c.mu.RUnlock()
|
||||
|
||||
if rebalance {
|
||||
ring.rebalance()
|
||||
c.rebalance()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -255,45 +255,45 @@ func (ring *Ring) heartbeat() {
|
|||
//
|
||||
// It is rare to Close a Ring, as the Ring is meant to be long-lived
|
||||
// and shared between many goroutines.
|
||||
func (ring *Ring) Close() (retErr error) {
|
||||
defer ring.mu.Unlock()
|
||||
ring.mu.Lock()
|
||||
func (c *Ring) Close() (retErr error) {
|
||||
defer c.mu.Unlock()
|
||||
c.mu.Lock()
|
||||
|
||||
if ring.closed {
|
||||
if c.closed {
|
||||
return nil
|
||||
}
|
||||
ring.closed = true
|
||||
c.closed = true
|
||||
|
||||
for _, shard := range ring.shards {
|
||||
for _, shard := range c.shards {
|
||||
if err := shard.Client.Close(); err != nil {
|
||||
retErr = err
|
||||
}
|
||||
}
|
||||
ring.hash = nil
|
||||
ring.shards = nil
|
||||
c.hash = nil
|
||||
c.shards = nil
|
||||
|
||||
return retErr
|
||||
}
|
||||
|
||||
func (ring *Ring) Pipeline() *Pipeline {
|
||||
func (c *Ring) Pipeline() *Pipeline {
|
||||
pipe := Pipeline{
|
||||
exec: ring.pipelineExec,
|
||||
exec: c.pipelineExec,
|
||||
}
|
||||
pipe.cmdable.process = pipe.Process
|
||||
pipe.statefulCmdable.process = pipe.Process
|
||||
return &pipe
|
||||
}
|
||||
|
||||
func (ring *Ring) Pipelined(fn func(*Pipeline) error) ([]Cmder, error) {
|
||||
return ring.Pipeline().pipelined(fn)
|
||||
func (c *Ring) Pipelined(fn func(*Pipeline) error) ([]Cmder, error) {
|
||||
return c.Pipeline().pipelined(fn)
|
||||
}
|
||||
|
||||
func (ring *Ring) pipelineExec(cmds []Cmder) error {
|
||||
func (c *Ring) pipelineExec(cmds []Cmder) error {
|
||||
var retErr error
|
||||
|
||||
cmdsMap := make(map[string][]Cmder)
|
||||
for _, cmd := range cmds {
|
||||
name := ring.hash.Get(hashtag.Key(ring.cmdFirstKey(cmd)))
|
||||
name := c.hash.Get(hashtag.Key(c.cmdFirstKey(cmd)))
|
||||
if name == "" {
|
||||
cmd.setErr(errRingShardsDown)
|
||||
if retErr == nil {
|
||||
|
@ -304,11 +304,11 @@ func (ring *Ring) pipelineExec(cmds []Cmder) error {
|
|||
cmdsMap[name] = append(cmdsMap[name], cmd)
|
||||
}
|
||||
|
||||
for i := 0; i <= ring.opt.MaxRetries; i++ {
|
||||
for i := 0; i <= c.opt.MaxRetries; i++ {
|
||||
failedCmdsMap := make(map[string][]Cmder)
|
||||
|
||||
for name, cmds := range cmdsMap {
|
||||
client := ring.shards[name].Client
|
||||
client := c.shards[name].Client
|
||||
cn, err := client.conn()
|
||||
if err != nil {
|
||||
setCmdsErr(cmds, err)
|
||||
|
|
54
tx.go
54
tx.go
|
@ -39,7 +39,7 @@ func (c *Client) Watch(fn func(*Tx) error, keys ...string) error {
|
|||
tx := c.newTx()
|
||||
if len(keys) > 0 {
|
||||
if err := tx.Watch(keys...).Err(); err != nil {
|
||||
tx.close()
|
||||
_ = tx.close()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -50,57 +50,57 @@ func (c *Client) Watch(fn func(*Tx) error, keys ...string) error {
|
|||
return retErr
|
||||
}
|
||||
|
||||
func (tx *Tx) Process(cmd Cmder) error {
|
||||
if tx.cmds == nil {
|
||||
return tx.baseClient.Process(cmd)
|
||||
func (c *Tx) Process(cmd Cmder) error {
|
||||
if c.cmds == nil {
|
||||
return c.baseClient.Process(cmd)
|
||||
}
|
||||
tx.cmds = append(tx.cmds, cmd)
|
||||
c.cmds = append(c.cmds, cmd)
|
||||
return nil
|
||||
}
|
||||
|
||||
// close closes the transaction, releasing any open resources.
|
||||
func (tx *Tx) close() error {
|
||||
if tx.closed {
|
||||
func (c *Tx) close() error {
|
||||
if c.closed {
|
||||
return nil
|
||||
}
|
||||
tx.closed = true
|
||||
if err := tx.Unwatch().Err(); err != nil {
|
||||
c.closed = true
|
||||
if err := c.Unwatch().Err(); err != nil {
|
||||
internal.Logf("Unwatch failed: %s", err)
|
||||
}
|
||||
return tx.baseClient.Close()
|
||||
return c.baseClient.Close()
|
||||
}
|
||||
|
||||
// Watch marks the keys to be watched for conditional execution
|
||||
// of a transaction.
|
||||
func (tx *Tx) Watch(keys ...string) *StatusCmd {
|
||||
func (c *Tx) Watch(keys ...string) *StatusCmd {
|
||||
args := make([]interface{}, 1+len(keys))
|
||||
args[0] = "WATCH"
|
||||
for i, key := range keys {
|
||||
args[1+i] = key
|
||||
}
|
||||
cmd := NewStatusCmd(args...)
|
||||
tx.Process(cmd)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// Unwatch flushes all the previously watched keys for a transaction.
|
||||
func (tx *Tx) Unwatch(keys ...string) *StatusCmd {
|
||||
func (c *Tx) Unwatch(keys ...string) *StatusCmd {
|
||||
args := make([]interface{}, 1+len(keys))
|
||||
args[0] = "UNWATCH"
|
||||
for i, key := range keys {
|
||||
args[1+i] = key
|
||||
}
|
||||
cmd := NewStatusCmd(args...)
|
||||
tx.Process(cmd)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// Discard discards queued commands.
|
||||
func (tx *Tx) Discard() error {
|
||||
if tx.cmds == nil {
|
||||
func (c *Tx) Discard() error {
|
||||
if c.cmds == nil {
|
||||
return errDiscard
|
||||
}
|
||||
tx.cmds = tx.cmds[:1]
|
||||
c.cmds = c.cmds[:1]
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -113,19 +113,19 @@ func (tx *Tx) Discard() error {
|
|||
// Exec always returns list of commands. If transaction fails
|
||||
// TxFailedErr is returned. Otherwise Exec returns error of the first
|
||||
// failed command or nil.
|
||||
func (tx *Tx) MultiExec(fn func() error) ([]Cmder, error) {
|
||||
if tx.closed {
|
||||
func (c *Tx) MultiExec(fn func() error) ([]Cmder, error) {
|
||||
if c.closed {
|
||||
return nil, pool.ErrClosed
|
||||
}
|
||||
|
||||
tx.cmds = []Cmder{NewStatusCmd("MULTI")}
|
||||
c.cmds = []Cmder{NewStatusCmd("MULTI")}
|
||||
if err := fn(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tx.cmds = append(tx.cmds, NewSliceCmd("EXEC"))
|
||||
c.cmds = append(c.cmds, NewSliceCmd("EXEC"))
|
||||
|
||||
cmds := tx.cmds
|
||||
tx.cmds = nil
|
||||
cmds := c.cmds
|
||||
c.cmds = nil
|
||||
|
||||
if len(cmds) == 2 {
|
||||
return []Cmder{}, nil
|
||||
|
@ -134,18 +134,18 @@ func (tx *Tx) MultiExec(fn func() error) ([]Cmder, error) {
|
|||
// Strip MULTI and EXEC commands.
|
||||
retCmds := cmds[1 : len(cmds)-1]
|
||||
|
||||
cn, err := tx.conn()
|
||||
cn, err := c.conn()
|
||||
if err != nil {
|
||||
setCmdsErr(retCmds, err)
|
||||
return retCmds, err
|
||||
}
|
||||
|
||||
err = tx.execCmds(cn, cmds)
|
||||
tx.putConn(cn, err, false)
|
||||
err = c.execCmds(cn, cmds)
|
||||
c.putConn(cn, err, false)
|
||||
return retCmds, err
|
||||
}
|
||||
|
||||
func (tx *Tx) execCmds(cn *pool.Conn, cmds []Cmder) error {
|
||||
func (c *Tx) execCmds(cn *pool.Conn, cmds []Cmder) error {
|
||||
err := writeCmd(cn, cmds...)
|
||||
if err != nil {
|
||||
setCmdsErr(cmds[1:len(cmds)-1], err)
|
||||
|
|
Loading…
Reference in New Issue