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 { if err != nil {
retErr = err retErr = err
} }
client.freeConn(cn, err) client.putConn(cn, err)
} }
cmdsMap = failedCmds cmdsMap = failedCmds

View File

@ -84,13 +84,8 @@ func (c *Multi) Exec(f func() error) ([]Cmder, error) {
} }
err = c.execCmds(cn, cmds) err = c.execCmds(cn, cmds)
if err != nil { c.base.putConn(cn, err)
c.base.freeConn(cn, err)
return cmds[1 : len(cmds)-1], 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 { func (c *Multi) execCmds(cn *conn, cmds []Cmder) error {

View File

@ -67,13 +67,9 @@ func (c *Pipeline) Exec() ([]Cmder, error) {
return cmds, err return cmds, err
} }
if err := c.execCmds(cn, cmds); err != nil { err = c.execCmds(cn, cmds)
c.client.freeConn(cn, err) c.client.putConn(cn, err)
return cmds, err return cmds, err
}
c.client.putConn(cn)
return cmds, nil
} }
func (c *Pipeline) execCmds(cn *conn, cmds []Cmder) error { func (c *Pipeline) execCmds(cn *conn, cmds []Cmder) error {

View File

@ -19,7 +19,7 @@ func (c *baseClient) conn() (*conn, error) {
if isNew { if isNew {
if err := c.initConn(cn); err != nil { if err := c.initConn(cn); err != nil {
c.removeConn(cn) c.putConn(cn, err)
return nil, err return nil, err
} }
} }
@ -53,26 +53,19 @@ func (c *baseClient) initConn(cn *conn) error {
return nil 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 { if cn.rd.Buffered() > 0 {
return c.connPool.Remove(cn) err = c.connPool.Remove(cn)
} else if ei == nil { } else if ei == nil {
return c.connPool.Put(cn) err = c.connPool.Put(cn)
} else if _, ok := ei.(redisError); ok { } 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) if err != nil {
} log.Printf("redis: putConn failed: %s", err)
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)
} }
} }
@ -96,17 +89,13 @@ func (c *baseClient) process(cmd Cmder) {
} }
if err := cn.writeCmds(cmd); err != nil { if err := cn.writeCmds(cmd); err != nil {
c.freeConn(cn, err) c.putConn(cn, err)
cmd.setErr(err) cmd.setErr(err)
return return
} }
if err := cmd.parseReply(cn.rd); err != nil { err = cmd.parseReply(cn.rd)
c.freeConn(cn, err) c.putConn(cn, err)
return
}
c.putConn(cn)
} }
// Close closes the client, releasing any open resources. // Close closes the client, releasing any open resources.