Simplify internal API

This commit is contained in:
Dimitrij Denissenko 2015-04-17 14:18:44 +01:00
parent 60e0221c3d
commit a5891da2f6
4 changed files with 19 additions and 39 deletions

View File

@ -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

View File

@ -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)
c.base.putConn(cn, err)
return cmds[1 : len(cmds)-1], err
}
c.base.putConn(cn)
return cmds[1 : len(cmds)-1], nil
}
func (c *Multi) execCmds(cn *conn, cmds []Cmder) error {

View File

@ -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)
err = c.execCmds(cn, cmds)
c.client.putConn(cn, err)
return cmds, err
}
c.client.putConn(cn)
return cmds, nil
}
func (c *Pipeline) execCmds(cn *conn, cmds []Cmder) error {

View File

@ -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.