forked from mirror/redis
Add ErrTxFailed. Small tweaks.
This commit is contained in:
parent
4c504ab528
commit
39c42d4389
|
@ -19,6 +19,12 @@ type Cmder interface {
|
|||
Err() error
|
||||
}
|
||||
|
||||
func setCmdsErr(cmds []Cmder, e error) {
|
||||
for _, cmd := range cmds {
|
||||
cmd.setErr(e)
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type baseCmd struct {
|
||||
|
|
|
@ -108,8 +108,7 @@ func ExampleMulti() {
|
|||
|
||||
for {
|
||||
cmds, err := incr(tx)
|
||||
if err == redis.Nil {
|
||||
// Transaction failed. Repeat.
|
||||
if err == redis.TxFailedErr {
|
||||
continue
|
||||
} else if err != nil {
|
||||
panic(err)
|
||||
|
|
33
v2/multi.go
33
v2/multi.go
|
@ -50,6 +50,9 @@ func (c *Multi) Discard() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Exec always returns list of commands. If transaction fails
|
||||
// TxFailedErr is returned. Otherwise Exec returns error of the first
|
||||
// failed command or nil.
|
||||
func (c *Multi) Exec(f func()) ([]Cmder, error) {
|
||||
c.cmds = []Cmder{NewStatusCmd("MULTI")}
|
||||
f()
|
||||
|
@ -64,35 +67,38 @@ func (c *Multi) Exec(f func()) ([]Cmder, error) {
|
|||
|
||||
cn, err := c.conn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
setCmdsErr(cmds[1:len(cmds)-1], err)
|
||||
return cmds[1 : len(cmds)-1], err
|
||||
}
|
||||
|
||||
// Synchronize writes and reads to the connection using mutex.
|
||||
err = c.execCmds(cmds, cn)
|
||||
err = c.execCmds(cn, cmds)
|
||||
if err != nil {
|
||||
c.removeConn(cn)
|
||||
return nil, err
|
||||
c.freeConn(cn, err)
|
||||
return cmds[1 : len(cmds)-1], err
|
||||
}
|
||||
|
||||
c.putConn(cn)
|
||||
return cmds[1 : len(cmds)-1], nil
|
||||
}
|
||||
|
||||
func (c *Multi) execCmds(cmds []Cmder, cn *conn) error {
|
||||
func (c *Multi) execCmds(cn *conn, cmds []Cmder) error {
|
||||
err := c.writeCmd(cn, cmds...)
|
||||
if err != nil {
|
||||
setCmdsErr(cmds[1:len(cmds)-1], err)
|
||||
return err
|
||||
}
|
||||
|
||||
statusCmd := NewStatusCmd()
|
||||
|
||||
// Omit last cmduest (EXEC).
|
||||
// Omit last command (EXEC).
|
||||
cmdsLen := len(cmds) - 1
|
||||
|
||||
// Parse queued replies.
|
||||
for i := 0; i < cmdsLen; i++ {
|
||||
_, err = statusCmd.parseReply(cn.rd)
|
||||
if err != nil {
|
||||
setCmdsErr(cmds[1:len(cmds)-1], err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -100,15 +106,21 @@ func (c *Multi) execCmds(cmds []Cmder, cn *conn) error {
|
|||
// Parse number of replies.
|
||||
line, err := readLine(cn.rd)
|
||||
if err != nil {
|
||||
setCmdsErr(cmds[1:len(cmds)-1], err)
|
||||
return err
|
||||
}
|
||||
if line[0] != '*' {
|
||||
return fmt.Errorf("redis: expected '*', but got line %q", line)
|
||||
err := fmt.Errorf("redis: expected '*', but got line %q", line)
|
||||
setCmdsErr(cmds[1:len(cmds)-1], err)
|
||||
return err
|
||||
}
|
||||
if len(line) == 3 && line[1] == '-' && line[2] == '1' {
|
||||
return Nil
|
||||
setCmdsErr(cmds[1:len(cmds)-1], TxFailedErr)
|
||||
return TxFailedErr
|
||||
}
|
||||
|
||||
var firstCmdErr error
|
||||
|
||||
// Parse replies.
|
||||
// Loop starts from 1 to omit first cmduest (MULTI).
|
||||
for i := 1; i < cmdsLen; i++ {
|
||||
|
@ -116,10 +128,13 @@ func (c *Multi) execCmds(cmds []Cmder, cn *conn) error {
|
|||
val, err := cmd.parseReply(cn.rd)
|
||||
if err != nil {
|
||||
cmd.setErr(err)
|
||||
if firstCmdErr == nil {
|
||||
firstCmdErr = err
|
||||
}
|
||||
} else {
|
||||
cmd.setVal(val)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return firstCmdErr
|
||||
}
|
||||
|
|
|
@ -18,9 +18,12 @@ const (
|
|||
stringFloatMap
|
||||
)
|
||||
|
||||
// Represents Redis nil reply.
|
||||
// Redis nil reply.
|
||||
var Nil = errors.New("(nil)")
|
||||
|
||||
// Redis transaction failed.
|
||||
var TxFailedErr = errors.New("redis: transaction failed")
|
||||
|
||||
var (
|
||||
errReaderTooSmall = errors.New("redis: reader is too small")
|
||||
errValNotSet = errors.New("redis: value is not set")
|
||||
|
|
|
@ -35,7 +35,7 @@ func (c *Pipeline) Discard() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Always returns list of commands and error of the first failed
|
||||
// Exec always returns list of commands and error of the first failed
|
||||
// command if any.
|
||||
func (c *Pipeline) Exec() ([]Cmder, error) {
|
||||
cmds := c.cmds
|
||||
|
@ -47,10 +47,11 @@ func (c *Pipeline) Exec() ([]Cmder, error) {
|
|||
|
||||
cn, err := c.conn()
|
||||
if err != nil {
|
||||
setCmdsErr(cmds, err)
|
||||
return cmds, err
|
||||
}
|
||||
|
||||
if err := c.execCmds(cmds, cn); err != nil {
|
||||
if err := c.execCmds(cn, cmds); err != nil {
|
||||
c.freeConn(cn, err)
|
||||
return cmds, err
|
||||
}
|
||||
|
@ -59,12 +60,10 @@ func (c *Pipeline) Exec() ([]Cmder, error) {
|
|||
return cmds, nil
|
||||
}
|
||||
|
||||
func (c *Pipeline) execCmds(cmds []Cmder, cn *conn) error {
|
||||
func (c *Pipeline) execCmds(cn *conn, cmds []Cmder) error {
|
||||
err := c.writeCmd(cn, cmds...)
|
||||
if err != nil {
|
||||
for _, cmd := range cmds {
|
||||
cmd.setErr(err)
|
||||
}
|
||||
setCmdsErr(cmds, err)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -73,7 +73,7 @@ func (c *baseClient) init(cn *conn, password string, db int64) error {
|
|||
}
|
||||
|
||||
func (c *baseClient) freeConn(cn *conn, err error) {
|
||||
if err == Nil {
|
||||
if err == Nil || err == TxFailedErr {
|
||||
c.putConn(cn)
|
||||
} else {
|
||||
c.removeConn(cn)
|
||||
|
|
|
@ -2643,13 +2643,9 @@ func (t *RedisTest) transactionalIncr(c *C) ([]redis.Cmder, error) {
|
|||
v, err := strconv.ParseInt(get.Val(), 10, 64)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
cmds, err := multi.Exec(func() {
|
||||
return multi.Exec(func() {
|
||||
multi.Set("key", strconv.FormatInt(v+1, 10))
|
||||
})
|
||||
if err == redis.Nil {
|
||||
return t.transactionalIncr(c)
|
||||
}
|
||||
return cmds, err
|
||||
}
|
||||
|
||||
func (t *RedisTest) TestWatchUnwatch(c *C) {
|
||||
|
@ -2661,10 +2657,16 @@ func (t *RedisTest) TestWatchUnwatch(c *C) {
|
|||
for i := 0; i < 1000; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
cmds, err := t.transactionalIncr(c)
|
||||
c.Assert(cmds, HasLen, 1)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(cmds[0].Err(), IsNil)
|
||||
for {
|
||||
cmds, err := t.transactionalIncr(c)
|
||||
if err == redis.TxFailedErr {
|
||||
continue
|
||||
}
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(cmds, HasLen, 1)
|
||||
c.Assert(cmds[0].Err(), IsNil)
|
||||
break
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue