Skip to content

Commit

Permalink
Refactor how to obtain a connection to the redis server.
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnc committed Nov 20, 2010
1 parent 4c4b65a commit 027eb50
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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 {
Expand Down

0 comments on commit 027eb50

Please sign in to comment.