@@ -18,28 +18,22 @@ const (
18
18
MaxPoolSize = 5
19
19
)
20
20
21
- var pool chan * net.TCPConn
22
-
23
21
var defaultAddr , _ = net .ResolveTCPAddr ("127.0.0.1:6379" )
24
22
25
23
type Client struct {
26
24
Addr string
27
25
Db int
28
26
Password string
27
+ //the channel for pub/sub commands
28
+ Messages chan []byte
29
+ //the connection pool
30
+ pool chan * net.TCPConn
29
31
}
30
32
31
33
type RedisError string
32
34
33
35
func (err RedisError ) String () string { return "Redis Error: " + string (err ) }
34
36
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
-
43
37
// reads a bulk reply (i.e $5\r\nhello)
44
38
func readBulk (reader * bufio.Reader , head string ) ([]byte , os.Error ) {
45
39
var err os.Error
@@ -181,8 +175,15 @@ func (client *Client) sendCommand(cmd string, args []string) (data interface{},
181
175
cmdbuf .WriteString (fmt .Sprintf ("$%d\r \n %s\r \n " , len (s ), s ))
182
176
}
183
177
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
+ }
184
185
// grab a connection from the pool
185
- c := <- pool
186
+ c := <- client . pool
186
187
187
188
if c == nil {
188
189
c , err = client .openConnection ()
@@ -203,7 +204,7 @@ func (client *Client) sendCommand(cmd string, args []string) (data interface{},
203
204
End:
204
205
205
206
//add the client back to the queue
206
- pool <- c
207
+ client . pool <- c
207
208
208
209
return data , err
209
210
}
@@ -950,7 +951,7 @@ func valueToString(v reflect.Value) (string, os.Error) {
950
951
typ := v .Type ().(* reflect.SliceType )
951
952
if _ , ok := typ .Elem ().(* reflect.UintType ); ok {
952
953
if v .Len () > 0 {
953
- if v .Elem (1 ).(* reflect.UintValue ).Overflow (257 ) {
954
+ if v .Elem (1 ).(* reflect.UintValue ).Overflow (257 ) {
954
955
return string (v .Interface ().([]byte )), nil
955
956
}
956
957
}
0 commit comments