From 027eb503b55422da400c2ed16d897bc510e3b3f0 Mon Sep 17 00:00:00 2001 From: lonnc Date: Sat, 20 Nov 2010 17:46:12 +0000 Subject: [PATCH] Refactor how to obtain a connection to the redis server. --- redis.go | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/redis.go b/redis.go index 69e50af..4247586 100644 --- a/redis.go +++ b/redis.go @@ -188,21 +188,11 @@ func (client *Client) openConnection() (c *net.TCPConn, err os.Error) { func (client *Client) sendCommand(cmd string, args ...string) (data interface{}, err os.Error) { - if client.pool == nil { - client.pool = make(chan *net.TCPConn, MaxPoolSize) - for i := 0; i < MaxPoolSize; i++ { - //add dummy values to the pool - client.pool <- nil - } - } // grab a connection from the pool - c := <-client.pool + c, err := client.popCon() - if c == nil { - c, err = client.openConnection() - if err != nil { - goto End - } + if err != nil { + goto End } b := commandBytes(cmd, args...) @@ -219,11 +209,32 @@ func (client *Client) sendCommand(cmd string, args ...string) (data interface{}, End: //add the client back to the queue - client.pool <- c + client.pushCon(c) return data, err } +func (client *Client) popCon() (*net.TCPConn, os.Error) { + if client.pool == nil { + client.pool = make(chan *net.TCPConn, MaxPoolSize) + for i := 0; i < MaxPoolSize; i++ { + //add dummy values to the pool + client.pool <- nil + } + } + // grab a connection from the pool + c := <-client.pool + + if c == nil { + return client.openConnection() + } + return c, nil +} + +func (client *Client) pushCon(c *net.TCPConn) { + client.pool <- c +} + // General Commands func (client *Client) Auth(password string) os.Error {