mirror of https://github.com/go-redis/redis.git
Refactor Tx using Pipeline to implement Cmdable interface.
This commit is contained in:
parent
3490ff5d21
commit
20bc3ec5a6
|
@ -343,8 +343,8 @@ var _ = Describe("ClusterClient", func() {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = tx.MultiExec(func() error {
|
_, err = tx.Pipelined(func(pipe *redis.Pipeline) error {
|
||||||
tx.Set(key, strconv.FormatInt(n+1, 10), 0)
|
pipe.Set(key, strconv.FormatInt(n+1, 10), 0)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -190,8 +190,8 @@ func ExampleClient_Watch() {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = tx.MultiExec(func() error {
|
_, err = tx.Pipelined(func(pipe *redis.Pipeline) error {
|
||||||
tx.Set(key, strconv.FormatInt(n+1, 10), 0)
|
pipe.Set(key, strconv.FormatInt(n+1, 10), 0)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -35,13 +35,13 @@ var _ = Describe("pool", func() {
|
||||||
Expect(pool.Len()).To(Equal(pool.FreeLen()))
|
Expect(pool.Len()).To(Equal(pool.FreeLen()))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("srespect max size on multi", func() {
|
It("respects max size on multi", func() {
|
||||||
perform(1000, func(id int) {
|
perform(1000, func(id int) {
|
||||||
var ping *redis.StatusCmd
|
var ping *redis.StatusCmd
|
||||||
|
|
||||||
err := client.Watch(func(tx *redis.Tx) error {
|
err := client.Watch(func(tx *redis.Tx) error {
|
||||||
cmds, err := tx.MultiExec(func() error {
|
cmds, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
|
||||||
ping = tx.Ping()
|
ping = pipe.Ping()
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
|
@ -222,8 +222,8 @@ var _ = Describe("races", func() {
|
||||||
num, err := strconv.ParseInt(val, 10, 64)
|
num, err := strconv.ParseInt(val, 10, 64)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
cmds, err := tx.MultiExec(func() error {
|
cmds, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
|
||||||
tx.Set("key", strconv.FormatInt(num+1, 10), 0)
|
pipe.Set("key", strconv.FormatInt(num+1, 10), 0)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
Expect(cmds).To(HaveLen(1))
|
Expect(cmds).To(HaveLen(1))
|
||||||
|
|
|
@ -67,8 +67,8 @@ var _ = Describe("Client", func() {
|
||||||
|
|
||||||
It("should close Tx without closing the client", func() {
|
It("should close Tx without closing the client", func() {
|
||||||
err := client.Watch(func(tx *redis.Tx) error {
|
err := client.Watch(func(tx *redis.Tx) error {
|
||||||
_, err := tx.MultiExec(func() error {
|
_, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
|
||||||
tx.Ping()
|
pipe.Ping()
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
|
|
75
tx.go
75
tx.go
|
@ -11,8 +11,6 @@ import (
|
||||||
// Redis transaction failed.
|
// Redis transaction failed.
|
||||||
const TxFailedErr = internal.RedisError("redis: transaction failed")
|
const TxFailedErr = internal.RedisError("redis: transaction failed")
|
||||||
|
|
||||||
var errDiscard = internal.RedisError("redis: Discard can be used only inside Exec")
|
|
||||||
|
|
||||||
// Tx implements Redis transactions as described in
|
// Tx implements Redis transactions as described in
|
||||||
// http://redis.io/topics/transactions. It's NOT safe for concurrent use
|
// http://redis.io/topics/transactions. It's NOT safe for concurrent use
|
||||||
// by multiple goroutines, because Exec resets list of watched keys.
|
// by multiple goroutines, because Exec resets list of watched keys.
|
||||||
|
@ -22,10 +20,11 @@ type Tx struct {
|
||||||
statefulCmdable
|
statefulCmdable
|
||||||
baseClient
|
baseClient
|
||||||
|
|
||||||
cmds []Cmder
|
|
||||||
closed bool
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Cmdable = (*Tx)(nil)
|
||||||
|
|
||||||
func (c *Client) newTx() *Tx {
|
func (c *Client) newTx() *Tx {
|
||||||
tx := Tx{
|
tx := Tx{
|
||||||
baseClient: baseClient{
|
baseClient: baseClient{
|
||||||
|
@ -53,14 +52,6 @@ func (c *Client) Watch(fn func(*Tx) error, keys ...string) error {
|
||||||
return retErr
|
return retErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tx) Process(cmd Cmder) error {
|
|
||||||
if c.cmds == nil {
|
|
||||||
return c.baseClient.Process(cmd)
|
|
||||||
}
|
|
||||||
c.cmds = append(c.cmds, cmd)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// close closes the transaction, releasing any open resources.
|
// close closes the transaction, releasing any open resources.
|
||||||
func (c *Tx) close() error {
|
func (c *Tx) close() error {
|
||||||
if c.closed {
|
if c.closed {
|
||||||
|
@ -98,16 +89,16 @@ func (c *Tx) Unwatch(keys ...string) *StatusCmd {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// Discard discards queued commands.
|
func (c *Tx) Pipeline() *Pipeline {
|
||||||
func (c *Tx) Discard() error {
|
pipe := Pipeline{
|
||||||
if c.cmds == nil {
|
exec: c.exec,
|
||||||
return errDiscard
|
|
||||||
}
|
}
|
||||||
c.cmds = c.cmds[:1]
|
pipe.cmdable.process = pipe.Process
|
||||||
return nil
|
pipe.statefulCmdable.process = pipe.Process
|
||||||
|
return &pipe
|
||||||
}
|
}
|
||||||
|
|
||||||
// MultiExec executes all previously queued commands in a transaction
|
// Pipelined executes commands queued in the fn in a transaction
|
||||||
// and restores the connection state to normal.
|
// and restores the connection state to normal.
|
||||||
//
|
//
|
||||||
// When using WATCH, EXEC will execute commands only if the watched keys
|
// When using WATCH, EXEC will execute commands only if the watched keys
|
||||||
|
@ -116,36 +107,29 @@ func (c *Tx) Discard() error {
|
||||||
// Exec always returns list of commands. If transaction fails
|
// Exec always returns list of commands. If transaction fails
|
||||||
// TxFailedErr is returned. Otherwise Exec returns error of the first
|
// TxFailedErr is returned. Otherwise Exec returns error of the first
|
||||||
// failed command or nil.
|
// failed command or nil.
|
||||||
func (c *Tx) MultiExec(fn func() error) ([]Cmder, error) {
|
func (c *Tx) Pipelined(fn func(*Pipeline) error) ([]Cmder, error) {
|
||||||
|
return c.Pipeline().pipelined(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Tx) exec(cmds []Cmder) error {
|
||||||
if c.closed {
|
if c.closed {
|
||||||
return nil, pool.ErrClosed
|
return pool.ErrClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
c.cmds = []Cmder{NewStatusCmd("MULTI")}
|
|
||||||
if err := fn(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
c.cmds = append(c.cmds, NewSliceCmd("EXEC"))
|
|
||||||
|
|
||||||
cmds := c.cmds
|
|
||||||
c.cmds = nil
|
|
||||||
|
|
||||||
if len(cmds) == 2 {
|
|
||||||
return []Cmder{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strip MULTI and EXEC commands.
|
|
||||||
retCmds := cmds[1 : len(cmds)-1]
|
|
||||||
|
|
||||||
cn, _, err := c.conn()
|
cn, _, err := c.conn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
setCmdsErr(retCmds, err)
|
setCmdsErr(cmds, err)
|
||||||
return retCmds, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.execCmds(cn, cmds)
|
multiExec := make([]Cmder, 0, len(cmds)+2)
|
||||||
|
multiExec = append(multiExec, NewStatusCmd("MULTI"))
|
||||||
|
multiExec = append(multiExec, cmds...)
|
||||||
|
multiExec = append(multiExec, NewSliceCmd("EXEC"))
|
||||||
|
|
||||||
|
err = c.execCmds(cn, multiExec)
|
||||||
c.putConn(cn, err, false)
|
c.putConn(cn, err, false)
|
||||||
return retCmds, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tx) execCmds(cn *pool.Conn, cmds []Cmder) error {
|
func (c *Tx) execCmds(cn *pool.Conn, cmds []Cmder) error {
|
||||||
|
@ -155,12 +139,11 @@ func (c *Tx) execCmds(cn *pool.Conn, cmds []Cmder) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
statusCmd := NewStatusCmd()
|
|
||||||
|
|
||||||
// Omit last command (EXEC).
|
// Omit last command (EXEC).
|
||||||
cmdsLen := len(cmds) - 1
|
cmdsLen := len(cmds) - 1
|
||||||
|
|
||||||
// Parse queued replies.
|
// Parse queued replies.
|
||||||
|
statusCmd := cmds[0]
|
||||||
for i := 0; i < cmdsLen; i++ {
|
for i := 0; i < cmdsLen; i++ {
|
||||||
if err := statusCmd.readReply(cn); err != nil {
|
if err := statusCmd.readReply(cn); err != nil {
|
||||||
setCmdsErr(cmds[1:len(cmds)-1], err)
|
setCmdsErr(cmds[1:len(cmds)-1], err)
|
||||||
|
@ -183,18 +166,18 @@ func (c *Tx) execCmds(cn *pool.Conn, cmds []Cmder) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var firstCmdErr error
|
var firstErr error
|
||||||
|
|
||||||
// Parse replies.
|
// Parse replies.
|
||||||
// Loop starts from 1 to omit MULTI cmd.
|
// Loop starts from 1 to omit MULTI cmd.
|
||||||
for i := 1; i < cmdsLen; i++ {
|
for i := 1; i < cmdsLen; i++ {
|
||||||
cmd := cmds[i]
|
cmd := cmds[i]
|
||||||
if err := cmd.readReply(cn); err != nil {
|
if err := cmd.readReply(cn); err != nil {
|
||||||
if firstCmdErr == nil {
|
if firstErr == nil {
|
||||||
firstCmdErr = err
|
firstErr = err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return firstCmdErr
|
return firstErr
|
||||||
}
|
}
|
||||||
|
|
24
tx_test.go
24
tx_test.go
|
@ -33,8 +33,8 @@ var _ = Describe("Tx", func() {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = tx.MultiExec(func() error {
|
_, err = tx.Pipelined(func(pipe *redis.Pipeline) error {
|
||||||
tx.Set(key, strconv.FormatInt(n+1, 10), 0)
|
pipe.Set(key, strconv.FormatInt(n+1, 10), 0)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
|
@ -65,10 +65,10 @@ var _ = Describe("Tx", func() {
|
||||||
|
|
||||||
It("should discard", func() {
|
It("should discard", func() {
|
||||||
err := client.Watch(func(tx *redis.Tx) error {
|
err := client.Watch(func(tx *redis.Tx) error {
|
||||||
cmds, err := tx.MultiExec(func() error {
|
cmds, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
|
||||||
tx.Set("key1", "hello1", 0)
|
pipe.Set("key1", "hello1", 0)
|
||||||
tx.Discard()
|
pipe.Discard()
|
||||||
tx.Set("key2", "hello2", 0)
|
pipe.Set("key2", "hello2", 0)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
@ -88,7 +88,7 @@ var _ = Describe("Tx", func() {
|
||||||
|
|
||||||
It("should exec empty", func() {
|
It("should exec empty", func() {
|
||||||
err := client.Watch(func(tx *redis.Tx) error {
|
err := client.Watch(func(tx *redis.Tx) error {
|
||||||
cmds, err := tx.MultiExec(func() error { return nil })
|
cmds, err := tx.Pipelined(func(*redis.Pipeline) error { return nil })
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(cmds).To(HaveLen(0))
|
Expect(cmds).To(HaveLen(0))
|
||||||
return err
|
return err
|
||||||
|
@ -104,9 +104,9 @@ var _ = Describe("Tx", func() {
|
||||||
const N = 20000
|
const N = 20000
|
||||||
|
|
||||||
err := client.Watch(func(tx *redis.Tx) error {
|
err := client.Watch(func(tx *redis.Tx) error {
|
||||||
cmds, err := tx.MultiExec(func() error {
|
cmds, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
|
||||||
for i := 0; i < N; i++ {
|
for i := 0; i < N; i++ {
|
||||||
tx.Incr("key")
|
pipe.Incr("key")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -135,8 +135,8 @@ var _ = Describe("Tx", func() {
|
||||||
|
|
||||||
do := func() error {
|
do := func() error {
|
||||||
err := client.Watch(func(tx *redis.Tx) error {
|
err := client.Watch(func(tx *redis.Tx) error {
|
||||||
_, err := tx.MultiExec(func() error {
|
_, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
|
||||||
tx.Ping()
|
pipe.Ping()
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
|
@ -162,7 +162,7 @@ var _ = Describe("Tx", func() {
|
||||||
|
|
||||||
do := func() error {
|
do := func() error {
|
||||||
err := client.Watch(func(tx *redis.Tx) error {
|
err := client.Watch(func(tx *redis.Tx) error {
|
||||||
_, err := tx.MultiExec(func() error {
|
_, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue