Skip to content

Commit 26de00e

Browse files
author
Michael Hoisie
committed
make the connection pool per-client
1 parent b8e0b76 commit 26de00e

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

redis.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,22 @@ const (
1818
MaxPoolSize = 5
1919
)
2020

21-
var pool chan *net.TCPConn
22-
2321
var defaultAddr, _ = net.ResolveTCPAddr("127.0.0.1:6379")
2422

2523
type Client struct {
2624
Addr string
2725
Db int
2826
Password string
27+
//the channel for pub/sub commands
28+
Messages chan []byte
29+
//the connection pool
30+
pool chan *net.TCPConn
2931
}
3032

3133
type RedisError string
3234

3335
func (err RedisError) String() string { return "Redis Error: " + string(err) }
3436

35-
func init() {
36-
pool = make(chan *net.TCPConn, MaxPoolSize)
37-
for i := 0; i < MaxPoolSize; i++ {
38-
//add dummy values to the pool
39-
pool <- nil
40-
}
41-
}
42-
4337
// reads a bulk reply (i.e $5\r\nhello)
4438
func readBulk(reader *bufio.Reader, head string) ([]byte, os.Error) {
4539
var err os.Error
@@ -181,8 +175,15 @@ func (client *Client) sendCommand(cmd string, args []string) (data interface{},
181175
cmdbuf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(s), s))
182176
}
183177

178+
if client.pool == nil {
179+
client.pool = make(chan *net.TCPConn, MaxPoolSize)
180+
for i := 0; i < MaxPoolSize; i++ {
181+
//add dummy values to the pool
182+
client.pool <- nil
183+
}
184+
}
184185
// grab a connection from the pool
185-
c := <-pool
186+
c := <-client.pool
186187

187188
if c == nil {
188189
c, err = client.openConnection()
@@ -203,7 +204,7 @@ func (client *Client) sendCommand(cmd string, args []string) (data interface{},
203204
End:
204205

205206
//add the client back to the queue
206-
pool <- c
207+
client.pool <- c
207208

208209
return data, err
209210
}
@@ -950,7 +951,7 @@ func valueToString(v reflect.Value) (string, os.Error) {
950951
typ := v.Type().(*reflect.SliceType)
951952
if _, ok := typ.Elem().(*reflect.UintType); ok {
952953
if v.Len() > 0 {
953-
if v.Elem(1).(*reflect.UintValue).Overflow(257) {
954+
if v.Elem(1).(*reflect.UintValue).Overflow(257) {
954955
return string(v.Interface().([]byte)), nil
955956
}
956957
}

0 commit comments

Comments
 (0)