From a5891da2f69bebc79b8e3a65bd8ed7e614627104 Mon Sep 17 00:00:00 2001 From: Dimitrij Denissenko Date: Fri, 17 Apr 2015 14:18:44 +0100 Subject: [PATCH] Simplify internal API --- cluster_pipeline.go | 2 +- multi.go | 9 ++------- pipeline.go | 10 +++------- redis.go | 37 +++++++++++++------------------------ 4 files changed, 19 insertions(+), 39 deletions(-) diff --git a/cluster_pipeline.go b/cluster_pipeline.go index a9e61b2..780477c 100644 --- a/cluster_pipeline.go +++ b/cluster_pipeline.go @@ -85,7 +85,7 @@ func (c *ClusterPipeline) Exec() (cmds []Cmder, retErr error) { if err != nil { retErr = err } - client.freeConn(cn, err) + client.putConn(cn, err) } cmdsMap = failedCmds diff --git a/multi.go b/multi.go index 3557ac5..161275a 100644 --- a/multi.go +++ b/multi.go @@ -84,13 +84,8 @@ func (c *Multi) Exec(f func() error) ([]Cmder, error) { } err = c.execCmds(cn, cmds) - if err != nil { - c.base.freeConn(cn, err) - return cmds[1 : len(cmds)-1], err - } - - c.base.putConn(cn) - return cmds[1 : len(cmds)-1], nil + c.base.putConn(cn, err) + return cmds[1 : len(cmds)-1], err } func (c *Multi) execCmds(cn *conn, cmds []Cmder) error { diff --git a/pipeline.go b/pipeline.go index 9490696..8cfd5a1 100644 --- a/pipeline.go +++ b/pipeline.go @@ -67,13 +67,9 @@ func (c *Pipeline) Exec() ([]Cmder, error) { return cmds, err } - if err := c.execCmds(cn, cmds); err != nil { - c.client.freeConn(cn, err) - return cmds, err - } - - c.client.putConn(cn) - return cmds, nil + err = c.execCmds(cn, cmds) + c.client.putConn(cn, err) + return cmds, err } func (c *Pipeline) execCmds(cn *conn, cmds []Cmder) error { diff --git a/redis.go b/redis.go index b5e335d..3f44693 100644 --- a/redis.go +++ b/redis.go @@ -19,7 +19,7 @@ func (c *baseClient) conn() (*conn, error) { if isNew { if err := c.initConn(cn); err != nil { - c.removeConn(cn) + c.putConn(cn, err) return nil, err } } @@ -53,26 +53,19 @@ func (c *baseClient) initConn(cn *conn) error { return nil } -func (c *baseClient) freeConn(cn *conn, ei error) error { +func (c *baseClient) putConn(cn *conn, ei error) { + var err error if cn.rd.Buffered() > 0 { - return c.connPool.Remove(cn) + err = c.connPool.Remove(cn) } else if ei == nil { - return c.connPool.Put(cn) + err = c.connPool.Put(cn) } else if _, ok := ei.(redisError); ok { - return c.connPool.Put(cn) + err = c.connPool.Put(cn) + } else { + err = c.connPool.Remove(cn) } - return c.connPool.Remove(cn) -} - -func (c *baseClient) removeConn(cn *conn) { - if err := c.connPool.Remove(cn); err != nil { - log.Printf("pool.Remove failed: %s", err) - } -} - -func (c *baseClient) putConn(cn *conn) { - if err := c.connPool.Put(cn); err != nil { - log.Printf("pool.Put failed: %s", err) + if err != nil { + log.Printf("redis: putConn failed: %s", err) } } @@ -96,17 +89,13 @@ func (c *baseClient) process(cmd Cmder) { } if err := cn.writeCmds(cmd); err != nil { - c.freeConn(cn, err) + c.putConn(cn, err) cmd.setErr(err) return } - if err := cmd.parseReply(cn.rd); err != nil { - c.freeConn(cn, err) - return - } - - c.putConn(cn) + err = cmd.parseReply(cn.rd) + c.putConn(cn, err) } // Close closes the client, releasing any open resources.