Add connnection pool and improve API.

This commit is contained in:
Vladimir Mihailenco 2012-08-05 15:09:43 +03:00
parent 859c5fb03b
commit 19a5db6632
7 changed files with 962 additions and 761 deletions

View File

@ -3,26 +3,52 @@ Readme
Redis client for Golang. Redis client for Golang.
Usage Getting Client instance
----- -----------------------
Example: Example 1:
import "github.com/vmihailenco/redis" import "github.com/vmihailenco/redis"
connect := func() (io.ReadWriter, error) { redisClient := redis.NewTCPClient(":6379", "", 0)
Example 2:
import "github.com/vmihailenco/redis"
openConn := func() (io.ReadWriter, error) {
fmt.Println("Connecting...") fmt.Println("Connecting...")
return net.Dial("tcp", "localhost:6379") return net.Dial("tcp", ":6379")
} }
disconnect := func(conn io.ReadWriter) error { closeConn := func(conn io.ReadWriter) error {
fmt.Println("Disconnecting...") fmt.Println("Disconnecting...")
conn.Close() conn.Close()
return nil return nil
} }
redisClient = redis.NewClient(connect, disconnect) initConn := func(client *Client) error {
_, err := client.Auth("foo").Reply()
if err != nil {
return err
}
_, err = client.Ping().Reply()
if err != nil {
return err
}
return nil
}
redisClient := redis.NewClient(openConn, closeConn, initConn)
`closeConn` and `initConn` functions can be `nil`.
Running commands
----------------
_, err := redisClient.Set("foo", "bar").Reply() _, err := redisClient.Set("foo", "bar").Reply()
if err != nil { if err != nil {
@ -41,10 +67,12 @@ Pipelining
Client has ability to run several commands with one read/write: Client has ability to run several commands with one read/write:
setReq := redisClient.Set("foo1", "bar1") // queue command SET multiClient := redisClient.Multi()
getReq := redisClient.Get("foo2") // queue command GET
reqs, err := redisClient.RunQueued() // run queued commands setReq := multiClient.Set("foo1", "bar1") // queue command SET
getReq := multiClient.Get("foo2") // queue command GET
reqs, err := multiClient.RunQueued() // run queued commands
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -64,16 +92,10 @@ Client has ability to run several commands with one read/write:
Multi/Exec Multi/Exec
---------- ----------
Getting multiClient: Example 1:
multiClient := redisClient.Multi() multiClient := redisClient.Multi()
Or:
multiClient = redis.NewMultiClient(connect, disconnect)
Working with multiClient:
futureGet1 := multiClient.Get("foo1") futureGet1 := multiClient.Get("foo1")
futureGet2 := multiClient.Get("foo2") futureGet2 := multiClient.Get("foo2")
_, err := multiClient.Exec() _, err := multiClient.Exec()
@ -95,9 +117,10 @@ Working with multiClient:
} }
} }
Or: Example 2:
multiClient := redisClient.Multi() multiClient := redisClient.Multi()
multiClient.Get("foo1") multiClient.Get("foo1")
multiClient.Get("foo2") multiClient.Get("foo2")
reqs, err := multiClient.Exec() reqs, err := multiClient.Exec()
@ -127,7 +150,6 @@ Publish:
Subscribe: Subscribe:
pubsub := redisClient.PubSubClient() pubsub := redisClient.PubSubClient()
// pubsub := redis.NewPubSubClient(connect, disconnect)
ch, err := pubsub.Subscribe("mychannel") ch, err := pubsub.Subscribe("mychannel")
if err != nil { if err != nil {
@ -146,12 +168,18 @@ Subscribe:
Thread safety Thread safety
------------- -------------
Client is thread safe. Internally sync.Mutex is used to synchronize writes and reads. redis.Client methods are thread safe. Following code is correct:
for i := 0; i < 1000; i++ {
go func() {
redisClient.Incr("foo").Reply()
}()
}
Custom commands Custom commands
--------------- ---------------
Lazy command: Example:
func Get(client *redis.Client, key string) *redis.BulkReq { func Get(client *redis.Client, key string) *redis.BulkReq {
req := redis.NewBulkReq("GET", key) req := redis.NewBulkReq("GET", key)
@ -166,21 +194,9 @@ Lazy command:
} }
} }
Immediate command:
func Quit(client *redis.Client) *redis.StatusReq {
req := redis.NewStatusReq("QUIT")
client.Run(req)
client.Close()
return req
}
status, err := Quit(redisClient).Reply()
if err != nil {
panic(err)
}
Connection pool Connection pool
--------------- ---------------
Client does not support connection pool. Client uses connection pool with default capacity of 10 connections. To change pool capacity:
redisClient.ConnPool.MaxCap = 1

View File

@ -18,32 +18,25 @@ func NewLimit(offset, count int64) *Limit {
func (c *Client) Auth(password string) *StatusReq { func (c *Client) Auth(password string) *StatusReq {
req := NewStatusReq("AUTH", password) req := NewStatusReq("AUTH", password)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Echo(message string) *BulkReq { func (c *Client) Echo(message string) *BulkReq {
req := NewBulkReq("ECHO", message) req := NewBulkReq("ECHO", message)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Ping() *StatusReq { func (c *Client) Ping() *StatusReq {
req := NewStatusReq("PING") req := NewStatusReq("PING")
c.Queue(req) c.Process(req)
return req
}
func (c *Client) Quit() *StatusReq {
req := NewStatusReq("QUIT")
c.Run(req)
c.Close()
return req return req
} }
func (c *Client) Select(index int64) *StatusReq { func (c *Client) Select(index int64) *StatusReq {
req := NewStatusReq("SELECT", strconv.FormatInt(index, 10)) req := NewStatusReq("SELECT", strconv.FormatInt(index, 10))
c.Queue(req) c.Process(req)
return req return req
} }
@ -51,13 +44,13 @@ func (c *Client) Select(index int64) *StatusReq {
func (c *Client) Flushall() *StatusReq { func (c *Client) Flushall() *StatusReq {
req := NewStatusReq("FLUSHALL") req := NewStatusReq("FLUSHALL")
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Flushdb() *StatusReq { func (c *Client) Flushdb() *StatusReq {
req := NewStatusReq("FLUSHDB") req := NewStatusReq("FLUSHDB")
c.Queue(req) c.Process(req)
return req return req
} }
@ -66,37 +59,37 @@ func (c *Client) Flushdb() *StatusReq {
func (c *Client) Del(keys ...string) *IntReq { func (c *Client) Del(keys ...string) *IntReq {
args := append([]string{"DEL"}, keys...) args := append([]string{"DEL"}, keys...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Dump(key string) *BulkReq { func (c *Client) Dump(key string) *BulkReq {
req := NewBulkReq("DUMP", key) req := NewBulkReq("DUMP", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Exists(key string) *BoolReq { func (c *Client) Exists(key string) *BoolReq {
req := NewBoolReq("EXISTS", key) req := NewBoolReq("EXISTS", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Expire(key string, seconds int64) *BoolReq { func (c *Client) Expire(key string, seconds int64) *BoolReq {
req := NewBoolReq("EXPIRE", key, strconv.FormatInt(seconds, 10)) req := NewBoolReq("EXPIRE", key, strconv.FormatInt(seconds, 10))
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ExpireAt(key string, timestamp int64) *BoolReq { func (c *Client) ExpireAt(key string, timestamp int64) *BoolReq {
req := NewBoolReq("EXPIREAT", key, strconv.FormatInt(timestamp, 10)) req := NewBoolReq("EXPIREAT", key, strconv.FormatInt(timestamp, 10))
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Keys(pattern string) *MultiBulkReq { func (c *Client) Keys(pattern string) *MultiBulkReq {
req := NewMultiBulkReq("KEYS", pattern) req := NewMultiBulkReq("KEYS", pattern)
c.Queue(req) c.Process(req)
return req return req
} }
@ -109,76 +102,76 @@ func (c *Client) Migrate(host string, port int32, key, db string, timeout int64)
db, db,
strconv.FormatInt(timeout, 10), strconv.FormatInt(timeout, 10),
) )
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Move(key string, db int64) *BoolReq { func (c *Client) Move(key string, db int64) *BoolReq {
req := NewBoolReq("MOVE", key, strconv.FormatInt(db, 10)) req := NewBoolReq("MOVE", key, strconv.FormatInt(db, 10))
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ObjectRefCount(keys ...string) *IntReq { func (c *Client) ObjectRefCount(keys ...string) *IntReq {
args := append([]string{"OBJECT", "REFCOUNT"}, keys...) args := append([]string{"OBJECT", "REFCOUNT"}, keys...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ObjectEncoding(keys ...string) *BulkReq { func (c *Client) ObjectEncoding(keys ...string) *BulkReq {
args := append([]string{"OBJECT", "ENCODING"}, keys...) args := append([]string{"OBJECT", "ENCODING"}, keys...)
req := NewBulkReq(args...) req := NewBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ObjectIdleTime(keys ...string) *IntReq { func (c *Client) ObjectIdleTime(keys ...string) *IntReq {
args := append([]string{"OBJECT", "IDLETIME"}, keys...) args := append([]string{"OBJECT", "IDLETIME"}, keys...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Persist(key string) *BoolReq { func (c *Client) Persist(key string) *BoolReq {
req := NewBoolReq("PERSIST", key) req := NewBoolReq("PERSIST", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Pexpire(key string, milliseconds int64) *BoolReq { func (c *Client) PExpire(key string, milliseconds int64) *BoolReq {
req := NewBoolReq("PEXPIRE", key, strconv.FormatInt(milliseconds, 10)) req := NewBoolReq("PEXPIRE", key, strconv.FormatInt(milliseconds, 10))
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) PexpireAt(key string, milliseconds int64) *BoolReq { func (c *Client) PExpireAt(key string, milliseconds int64) *BoolReq {
req := NewBoolReq("PEXPIREAT", key, strconv.FormatInt(milliseconds, 10)) req := NewBoolReq("PEXPIREAT", key, strconv.FormatInt(milliseconds, 10))
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) PTTL(key string) *IntReq { func (c *Client) PTTL(key string) *IntReq {
req := NewIntReq("PTTL", key) req := NewIntReq("PTTL", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) RandomKey() *BulkReq { func (c *Client) RandomKey() *BulkReq {
req := NewBulkReq("RANDOMKEY") req := NewBulkReq("RANDOMKEY")
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Rename(key, newkey string) *StatusReq { func (c *Client) Rename(key, newkey string) *StatusReq {
req := NewStatusReq("RENAME", key, newkey) req := NewStatusReq("RENAME", key, newkey)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) RenameNX(key, newkey string) *BoolReq { func (c *Client) RenameNX(key, newkey string) *BoolReq {
req := NewBoolReq("RENAMENX", key, newkey) req := NewBoolReq("RENAMENX", key, newkey)
c.Queue(req) c.Process(req)
return req return req
} }
@ -188,26 +181,26 @@ func (c *Client) Restore(key, ttl int64, value string) *StatusReq {
strconv.FormatInt(ttl, 10), strconv.FormatInt(ttl, 10),
value, value,
) )
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Sort(key string, params ...string) *MultiBulkReq { func (c *Client) Sort(key string, params ...string) *MultiBulkReq {
args := append([]string{"SORT", key}, params...) args := append([]string{"SORT", key}, params...)
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) TTL(key string) *IntReq { func (c *Client) TTL(key string) *IntReq {
req := NewIntReq("TTL", key) req := NewIntReq("TTL", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Type(key string) *StatusReq { func (c *Client) Type(key string) *StatusReq {
req := NewStatusReq("TYPE", key) req := NewStatusReq("TYPE", key)
c.Queue(req) c.Process(req)
return req return req
} }
@ -215,7 +208,7 @@ func (c *Client) Type(key string) *StatusReq {
func (c *Client) Append(key, value string) *IntReq { func (c *Client) Append(key, value string) *IntReq {
req := NewIntReq("APPEND", key, value) req := NewIntReq("APPEND", key, value)
c.Queue(req) c.Process(req)
return req return req
} }
@ -225,25 +218,25 @@ func (c *Client) Append(key, value string) *IntReq {
func (c *Client) Decr(key string) *IntReq { func (c *Client) Decr(key string) *IntReq {
req := NewIntReq("DECR", key) req := NewIntReq("DECR", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) DecrBy(key string, decrement int64) *IntReq { func (c *Client) DecrBy(key string, decrement int64) *IntReq {
req := NewIntReq("DECRBY", key, strconv.FormatInt(decrement, 10)) req := NewIntReq("DECRBY", key, strconv.FormatInt(decrement, 10))
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Get(key string) *BulkReq { func (c *Client) Get(key string) *BulkReq {
req := NewBulkReq("GET", key) req := NewBulkReq("GET", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) GetBit(key string, offset int64) *IntReq { func (c *Client) GetBit(key string, offset int64) *IntReq {
req := NewIntReq("GETBIT", key, strconv.FormatInt(offset, 10)) req := NewIntReq("GETBIT", key, strconv.FormatInt(offset, 10))
c.Queue(req) c.Process(req)
return req return req
} }
@ -254,25 +247,25 @@ func (c *Client) GetRange(key string, start, end int64) *BulkReq {
strconv.FormatInt(start, 10), strconv.FormatInt(start, 10),
strconv.FormatInt(end, 10), strconv.FormatInt(end, 10),
) )
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) GetSet(key, value string) *BulkReq { func (c *Client) GetSet(key, value string) *BulkReq {
req := NewBulkReq("GETSET", key, value) req := NewBulkReq("GETSET", key, value)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Incr(key string) *IntReq { func (c *Client) Incr(key string) *IntReq {
req := NewIntReq("INCR", key) req := NewIntReq("INCR", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) IncrBy(key string, value int64) *IntReq { func (c *Client) IncrBy(key string, value int64) *IntReq {
req := NewIntReq("INCRBY", key, strconv.FormatInt(value, 10)) req := NewIntReq("INCRBY", key, strconv.FormatInt(value, 10))
c.Queue(req) c.Process(req)
return req return req
} }
@ -281,21 +274,21 @@ func (c *Client) IncrBy(key string, value int64) *IntReq {
func (c *Client) MGet(keys ...string) *MultiBulkReq { func (c *Client) MGet(keys ...string) *MultiBulkReq {
args := append([]string{"MGET"}, keys...) args := append([]string{"MGET"}, keys...)
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) MSet(pairs ...string) *StatusReq { func (c *Client) MSet(pairs ...string) *StatusReq {
args := append([]string{"MSET"}, pairs...) args := append([]string{"MSET"}, pairs...)
req := NewStatusReq(args...) req := NewStatusReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) MSetNX(pairs ...string) *BoolReq { func (c *Client) MSetNX(pairs ...string) *BoolReq {
args := append([]string{"MSETNX"}, pairs...) args := append([]string{"MSETNX"}, pairs...)
req := NewBoolReq(args...) req := NewBoolReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
@ -306,13 +299,13 @@ func (c *Client) PSetEx(key string, milliseconds int64, value string) *StatusReq
strconv.FormatInt(milliseconds, 10), strconv.FormatInt(milliseconds, 10),
value, value,
) )
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) Set(key, value string) *StatusReq { func (c *Client) Set(key, value string) *StatusReq {
req := NewStatusReq("SET", key, value) req := NewStatusReq("SET", key, value)
c.Queue(req) c.Process(req)
return req return req
} }
@ -323,31 +316,31 @@ func (c *Client) SetBit(key string, offset int64, value int) *IntReq {
strconv.FormatInt(offset, 10), strconv.FormatInt(offset, 10),
strconv.FormatInt(int64(value), 10), strconv.FormatInt(int64(value), 10),
) )
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SetEx(key string, seconds int64, value string) *StatusReq { func (c *Client) SetEx(key string, seconds int64, value string) *StatusReq {
req := NewStatusReq("SETEX", key, strconv.FormatInt(seconds, 10), value) req := NewStatusReq("SETEX", key, strconv.FormatInt(seconds, 10), value)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SetNx(key, value string) *BoolReq { func (c *Client) SetNx(key, value string) *BoolReq {
req := NewBoolReq("SETNX", key, value) req := NewBoolReq("SETNX", key, value)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SetRange(key string, offset int64, value string) *IntReq { func (c *Client) SetRange(key string, offset int64, value string) *IntReq {
req := NewIntReq("SETRANGE", key, strconv.FormatInt(offset, 10), value) req := NewIntReq("SETRANGE", key, strconv.FormatInt(offset, 10), value)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) StrLen(key string) *IntReq { func (c *Client) StrLen(key string) *IntReq {
req := NewIntReq("STRLEN", key) req := NewIntReq("STRLEN", key)
c.Queue(req) c.Process(req)
return req return req
} }
@ -356,31 +349,31 @@ func (c *Client) StrLen(key string) *IntReq {
func (c *Client) HDel(key string, fields ...string) *IntReq { func (c *Client) HDel(key string, fields ...string) *IntReq {
args := append([]string{"HDEL", key}, fields...) args := append([]string{"HDEL", key}, fields...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) HExists(key, field string) *BoolReq { func (c *Client) HExists(key, field string) *BoolReq {
req := NewBoolReq("HEXISTS", key, field) req := NewBoolReq("HEXISTS", key, field)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) HGet(key, field string) *BulkReq { func (c *Client) HGet(key, field string) *BulkReq {
req := NewBulkReq("HGET", key, field) req := NewBulkReq("HGET", key, field)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) HGetAll(key string) *MultiBulkReq { func (c *Client) HGetAll(key string) *MultiBulkReq {
req := NewMultiBulkReq("HGETALL", key) req := NewMultiBulkReq("HGETALL", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) HIncrBy(key, field string, incr int64) *IntReq { func (c *Client) HIncrBy(key, field string, incr int64) *IntReq {
req := NewIntReq("HINCRBY", key, field, strconv.FormatInt(incr, 10)) req := NewIntReq("HINCRBY", key, field, strconv.FormatInt(incr, 10))
c.Queue(req) c.Process(req)
return req return req
} }
@ -388,45 +381,45 @@ func (c *Client) HIncrBy(key, field string, incr int64) *IntReq {
func (c *Client) HKeys(key string) *MultiBulkReq { func (c *Client) HKeys(key string) *MultiBulkReq {
req := NewMultiBulkReq("HKEYS", key) req := NewMultiBulkReq("HKEYS", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) HLen(key string) *IntReq { func (c *Client) HLen(key string) *IntReq {
req := NewIntReq("HLEN", key) req := NewIntReq("HLEN", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) HMGet(key string, fields ...string) *MultiBulkReq { func (c *Client) HMGet(key string, fields ...string) *MultiBulkReq {
args := append([]string{"HMGET", key}, fields...) args := append([]string{"HMGET", key}, fields...)
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) HMSet(key, field, value string, pairs ...string) *StatusReq { func (c *Client) HMSet(key, field, value string, pairs ...string) *StatusReq {
args := append([]string{"HMSET", key, field, value}, pairs...) args := append([]string{"HMSET", key, field, value}, pairs...)
req := NewStatusReq(args...) req := NewStatusReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) HSet(key, field, value string) *BoolReq { func (c *Client) HSet(key, field, value string) *BoolReq {
req := NewBoolReq("HSET", key, field, value) req := NewBoolReq("HSET", key, field, value)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) HSetNX(key, field, value string) *BoolReq { func (c *Client) HSetNX(key, field, value string) *BoolReq {
req := NewBoolReq("HSETNX", key, field, value) req := NewBoolReq("HSETNX", key, field, value)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) HVals(key string) *MultiBulkReq { func (c *Client) HVals(key string) *MultiBulkReq {
req := NewMultiBulkReq("HVALS", key) req := NewMultiBulkReq("HVALS", key)
c.Queue(req) c.Process(req)
return req return req
} }
@ -436,7 +429,7 @@ func (c *Client) BLPop(timeout int64, keys ...string) *MultiBulkReq {
args := append([]string{"BLPOP"}, keys...) args := append([]string{"BLPOP"}, keys...)
args = append(args, strconv.FormatInt(timeout, 10)) args = append(args, strconv.FormatInt(timeout, 10))
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
@ -444,7 +437,7 @@ func (c *Client) BRPop(timeout int64, keys ...string) *MultiBulkReq {
args := append([]string{"BRPOP"}, keys...) args := append([]string{"BRPOP"}, keys...)
args = append(args, strconv.FormatInt(timeout, 10)) args = append(args, strconv.FormatInt(timeout, 10))
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
@ -455,44 +448,44 @@ func (c *Client) BRPopLPush(source, destination string, timeout int64) *BulkReq
destination, destination,
strconv.FormatInt(timeout, 10), strconv.FormatInt(timeout, 10),
) )
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) LIndex(key string, index int64) *BulkReq { func (c *Client) LIndex(key string, index int64) *BulkReq {
req := NewBulkReq("LINDEX", key, strconv.FormatInt(index, 10)) req := NewBulkReq("LINDEX", key, strconv.FormatInt(index, 10))
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) LInsert(key, op, pivot, value string) *IntReq { func (c *Client) LInsert(key, op, pivot, value string) *IntReq {
req := NewIntReq("LINSERT", key, op, pivot, value) req := NewIntReq("LINSERT", key, op, pivot, value)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) LLen(key string) *IntReq { func (c *Client) LLen(key string) *IntReq {
req := NewIntReq("LLEN", key) req := NewIntReq("LLEN", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) LPop(key string) *BulkReq { func (c *Client) LPop(key string) *BulkReq {
req := NewBulkReq("LPOP", key) req := NewBulkReq("LPOP", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) LPush(key string, values ...string) *IntReq { func (c *Client) LPush(key string, values ...string) *IntReq {
args := append([]string{"LPUSH", key}, values...) args := append([]string{"LPUSH", key}, values...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) LPushX(key, value string) *IntReq { func (c *Client) LPushX(key, value string) *IntReq {
req := NewIntReq("LPUSHX", key, value) req := NewIntReq("LPUSHX", key, value)
c.Queue(req) c.Process(req)
return req return req
} }
@ -503,19 +496,19 @@ func (c *Client) LRange(key string, start, stop int64) *MultiBulkReq {
strconv.FormatInt(start, 10), strconv.FormatInt(start, 10),
strconv.FormatInt(stop, 10), strconv.FormatInt(stop, 10),
) )
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) LRem(key string, count int64, value string) *IntReq { func (c *Client) LRem(key string, count int64, value string) *IntReq {
req := NewIntReq("LREM", key, strconv.FormatInt(count, 10), value) req := NewIntReq("LREM", key, strconv.FormatInt(count, 10), value)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) LSet(key string, index int64, value string) *StatusReq { func (c *Client) LSet(key string, index int64, value string) *StatusReq {
req := NewStatusReq("LSET", key, strconv.FormatInt(index, 10), value) req := NewStatusReq("LSET", key, strconv.FormatInt(index, 10), value)
c.Queue(req) c.Process(req)
return req return req
} }
@ -526,32 +519,32 @@ func (c *Client) LTrim(key string, start, stop int64) *StatusReq {
strconv.FormatInt(start, 10), strconv.FormatInt(start, 10),
strconv.FormatInt(stop, 10), strconv.FormatInt(stop, 10),
) )
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) RPop(key string) *BulkReq { func (c *Client) RPop(key string) *BulkReq {
req := NewBulkReq("RPOP", key) req := NewBulkReq("RPOP", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) RPopLPush(source, destination string) *BulkReq { func (c *Client) RPopLPush(source, destination string) *BulkReq {
req := NewBulkReq("RPOPLPUSH", source, destination) req := NewBulkReq("RPOPLPUSH", source, destination)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) RPush(key string, values ...string) *IntReq { func (c *Client) RPush(key string, values ...string) *IntReq {
args := append([]string{"RPUSH", key}, values...) args := append([]string{"RPUSH", key}, values...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) RPushX(key string, value string) *IntReq { func (c *Client) RPushX(key string, value string) *IntReq {
req := NewIntReq("RPUSHX", key, value) req := NewIntReq("RPUSHX", key, value)
c.Queue(req) c.Process(req)
return req return req
} }
@ -560,92 +553,92 @@ func (c *Client) RPushX(key string, value string) *IntReq {
func (c *Client) SAdd(key string, members ...string) *IntReq { func (c *Client) SAdd(key string, members ...string) *IntReq {
args := append([]string{"SADD", key}, members...) args := append([]string{"SADD", key}, members...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SCard(key string) *IntReq { func (c *Client) SCard(key string) *IntReq {
req := NewIntReq("SCARD", key) req := NewIntReq("SCARD", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SDiff(keys ...string) *MultiBulkReq { func (c *Client) SDiff(keys ...string) *MultiBulkReq {
args := append([]string{"SDIFF"}, keys...) args := append([]string{"SDIFF"}, keys...)
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SDiffStore(destination string, keys ...string) *IntReq { func (c *Client) SDiffStore(destination string, keys ...string) *IntReq {
args := append([]string{"SDIFFSTORE", destination}, keys...) args := append([]string{"SDIFFSTORE", destination}, keys...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SInter(keys ...string) *MultiBulkReq { func (c *Client) SInter(keys ...string) *MultiBulkReq {
args := append([]string{"SINTER"}, keys...) args := append([]string{"SINTER"}, keys...)
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SInterStore(destination string, keys ...string) *IntReq { func (c *Client) SInterStore(destination string, keys ...string) *IntReq {
args := append([]string{"SINTERSTORE", destination}, keys...) args := append([]string{"SINTERSTORE", destination}, keys...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SIsMember(key, member string) *BoolReq { func (c *Client) SIsMember(key, member string) *BoolReq {
req := NewBoolReq("SISMEMBER", key, member) req := NewBoolReq("SISMEMBER", key, member)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SMembers(key string) *MultiBulkReq { func (c *Client) SMembers(key string) *MultiBulkReq {
req := NewMultiBulkReq("SMEMBERS", key) req := NewMultiBulkReq("SMEMBERS", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SMove(source, destination, member string) *BoolReq { func (c *Client) SMove(source, destination, member string) *BoolReq {
req := NewBoolReq("SMOVE", source, destination, member) req := NewBoolReq("SMOVE", source, destination, member)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SPop(key string) *BulkReq { func (c *Client) SPop(key string) *BulkReq {
req := NewBulkReq("SPOP", key) req := NewBulkReq("SPOP", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SRandMember(key string) *BulkReq { func (c *Client) SRandMember(key string) *BulkReq {
req := NewBulkReq("SRANDMEMBER", key) req := NewBulkReq("SRANDMEMBER", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SRem(key string, members ...string) *IntReq { func (c *Client) SRem(key string, members ...string) *IntReq {
args := append([]string{"SREM", key}, members...) args := append([]string{"SREM", key}, members...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SUnion(keys ...string) *MultiBulkReq { func (c *Client) SUnion(keys ...string) *MultiBulkReq {
args := append([]string{"SUNION"}, keys...) args := append([]string{"SUNION"}, keys...)
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) SUnionStore(destination string, keys ...string) *IntReq { func (c *Client) SUnionStore(destination string, keys ...string) *IntReq {
args := append([]string{"SUNIONSTORE", destination}, keys...) args := append([]string{"SUNIONSTORE", destination}, keys...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
@ -670,25 +663,25 @@ func (c *Client) ZAdd(key string, members ...*ZMember) *IntReq {
args = append(args, m.ScoreString(), m.Member) args = append(args, m.ScoreString(), m.Member)
} }
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ZCard(key string) *IntReq { func (c *Client) ZCard(key string) *IntReq {
req := NewIntReq("ZCARD", key) req := NewIntReq("ZCARD", key)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ZCount(key, min, max string) *IntReq { func (c *Client) ZCount(key, min, max string) *IntReq {
req := NewIntReq("ZCOUNT", key, min, max) req := NewIntReq("ZCOUNT", key, min, max)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ZIncrBy(key string, increment int64, member string) *IntReq { func (c *Client) ZIncrBy(key string, increment int64, member string) *IntReq {
req := NewIntReq("ZINCRBY", key, strconv.FormatInt(increment, 10), member) req := NewIntReq("ZINCRBY", key, strconv.FormatInt(increment, 10), member)
c.Queue(req) c.Process(req)
return req return req
} }
@ -711,7 +704,7 @@ func (c *Client) ZInterStore(
args = append(args, "AGGREGATE", aggregate) args = append(args, "AGGREGATE", aggregate)
} }
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
@ -726,7 +719,7 @@ func (c *Client) ZRange(key string, start, stop int64, withScores bool) *MultiBu
args = append(args, "WITHSCORES") args = append(args, "WITHSCORES")
} }
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
@ -749,20 +742,20 @@ func (c *Client) ZRangeByScore(
) )
} }
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ZRank(key, member string) *IntNilReq { func (c *Client) ZRank(key, member string) *IntNilReq {
req := NewIntNilReq("ZRANK", key, member) req := NewIntNilReq("ZRANK", key, member)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ZRem(key string, members ...string) *IntReq { func (c *Client) ZRem(key string, members ...string) *IntReq {
args := append([]string{"ZREM", key}, members...) args := append([]string{"ZREM", key}, members...)
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
@ -773,13 +766,13 @@ func (c *Client) ZRemRangeByRank(key string, start, stop int64) *IntReq {
strconv.FormatInt(start, 10), strconv.FormatInt(start, 10),
strconv.FormatInt(stop, 10), strconv.FormatInt(stop, 10),
) )
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ZRemRangeByScore(key, min, max string) *IntReq { func (c *Client) ZRemRangeByScore(key, min, max string) *IntReq {
req := NewIntReq("ZREMRANGEBYSCORE", key, min, max) req := NewIntReq("ZREMRANGEBYSCORE", key, min, max)
c.Queue(req) c.Process(req)
return req return req
} }
@ -789,7 +782,7 @@ func (c *Client) ZRevRange(key, start, stop string, withScores bool) *MultiBulkR
args = append(args, "WITHSCORES") args = append(args, "WITHSCORES")
} }
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
@ -811,19 +804,19 @@ func (c *Client) ZRevRangeByScore(
) )
} }
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ZRevRank(key, member string) *IntNilReq { func (c *Client) ZRevRank(key, member string) *IntNilReq {
req := NewIntNilReq("ZREVRANK", key, member) req := NewIntNilReq("ZREVRANK", key, member)
c.Queue(req) c.Process(req)
return req return req
} }
func (c *Client) ZScore(key, member string) *FloatReq { func (c *Client) ZScore(key, member string) *FloatReq {
req := NewFloatReq("ZSCORE", key, member) req := NewFloatReq("ZSCORE", key, member)
c.Queue(req) c.Process(req)
return req return req
} }
@ -846,24 +839,29 @@ func (c *Client) ZUnionStore(
args = append(args, "AGGREGATE", aggregate) args = append(args, "AGGREGATE", aggregate)
} }
req := NewIntReq(args...) req := NewIntReq(args...)
c.Queue(req) c.Process(req)
return req return req
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
func (c *Client) PubSubClient() *PubSubClient { func (c *Client) PubSubClient() (*PubSubClient, error) {
return NewPubSubClient(c.connect, c.disconnect) return newPubSubClient(c)
} }
func (c *Client) Publish(channel, message string) *IntReq { func (c *Client) Publish(channel, message string) *IntReq {
req := NewIntReq("PUBLISH", channel, message) req := NewIntReq("PUBLISH", channel, message)
c.Queue(req) c.Process(req)
return req return req
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
func (c *Client) Multi() *Client { func (c *Client) Multi() *Client {
return NewMultiClient(c.connect, c.disconnect) return &Client{
ConnPool: c.ConnPool,
InitConn: c.InitConn,
reqs: make([]Req, 0),
}
} }

95
connpool.go Normal file
View File

@ -0,0 +1,95 @@
package redis
import (
"io"
"log"
"os"
"sync"
"github.com/vmihailenco/bufreader"
)
type Conn struct {
RW io.ReadWriter
Rd *bufreader.Reader
}
func NewConn(rw io.ReadWriter) *Conn {
return &Conn{
RW: rw,
Rd: bufreader.NewSizedReader(8024),
}
}
type ConnPool struct {
Logger *log.Logger
cond *sync.Cond
conns []*Conn
OpenConn OpenConnFunc
CloseConn CloseConnFunc
cap, MaxCap int64
}
func NewConnPool(openConn OpenConnFunc, closeConn CloseConnFunc, maxCap int64) *ConnPool {
logger := log.New(
os.Stdout,
"redis.connpool: ",
log.Ldate|log.Ltime|log.Lshortfile,
)
return &ConnPool{
cond: sync.NewCond(&sync.Mutex{}),
Logger: logger,
conns: make([]*Conn, 0),
OpenConn: openConn,
CloseConn: closeConn,
MaxCap: maxCap,
}
}
func (p *ConnPool) Get() (*Conn, bool, error) {
p.cond.L.Lock()
defer p.cond.L.Unlock()
for len(p.conns) == 0 && p.cap >= p.MaxCap {
p.cond.Wait()
}
if len(p.conns) == 0 {
rw, err := p.OpenConn()
if err != nil {
return nil, false, err
}
p.cap++
return NewConn(rw), true, nil
}
last := len(p.conns) - 1
conn := p.conns[last]
p.conns[last] = nil
p.conns = p.conns[:last]
return conn, false, nil
}
func (p *ConnPool) Add(conn *Conn) {
p.cond.L.Lock()
defer p.cond.L.Unlock()
p.conns = append(p.conns, conn)
p.cond.Signal()
}
func (p *ConnPool) Remove(conn *Conn) {
p.cond.L.Lock()
p.cap--
p.cond.Signal()
p.cond.L.Unlock()
if p.CloseConn != nil && conn != nil {
p.CloseConn(conn.RW)
}
}
func (p *ConnPool) Len() int {
return len(p.conns)
}

View File

@ -2,20 +2,28 @@ package redis
import ( import (
"fmt" "fmt"
"sync"
) )
type PubSubClient struct { type PubSubClient struct {
*Client *Client
isSubscribed bool conn *Conn
ch chan *Message ch chan *Message
once sync.Once
} }
func NewPubSubClient(connect connectFunc, disconnect disconnectFunc) *PubSubClient { func newPubSubClient(client *Client) (*PubSubClient, error) {
conn, _, err := client.ConnPool.Get()
if err != nil {
return nil, err
}
c := &PubSubClient{ c := &PubSubClient{
Client: NewClient(connect, disconnect), Client: client,
conn: conn,
ch: make(chan *Message), ch: make(chan *Message),
} }
return c return c, nil
} }
type Message struct { type Message struct {
@ -32,16 +40,7 @@ func (c *PubSubClient) consumeMessages() {
// Replies can arrive in batches. // Replies can arrive in batches.
// Read whole reply and parse messages one by one. // Read whole reply and parse messages one by one.
rd, err := c.readerPool.Get() err := c.ReadReply(c.conn)
if err != nil {
msg := &Message{}
msg.Err = err
c.ch <- msg
return
}
defer c.readerPool.Add(rd)
err = c.ReadReply(rd)
if err != nil { if err != nil {
msg := &Message{} msg := &Message{}
msg.Err = err msg.Err = err
@ -52,7 +51,7 @@ func (c *PubSubClient) consumeMessages() {
for { for {
msg := &Message{} msg := &Message{}
replyI, err := req.ParseReply(rd) replyI, err := req.ParseReply(c.conn.Rd)
if err != nil { if err != nil {
msg.Err = err msg.Err = err
c.ch <- msg c.ch <- msg
@ -75,7 +74,7 @@ func (c *PubSubClient) consumeMessages() {
} }
c.ch <- msg c.ch <- msg
if !rd.HasUnread() { if !c.conn.Rd.HasUnread() {
break break
} }
} }
@ -86,16 +85,13 @@ func (c *PubSubClient) Subscribe(channels ...string) (chan *Message, error) {
args := append([]string{"SUBSCRIBE"}, channels...) args := append([]string{"SUBSCRIBE"}, channels...)
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
if err := c.WriteReq(req.Req()); err != nil { if err := c.WriteReq(req.Req(), c.conn); err != nil {
return nil, err return nil, err
} }
c.mtx.Lock() c.once.Do(func() {
if !c.isSubscribed {
c.isSubscribed = true
go c.consumeMessages() go c.consumeMessages()
} })
c.mtx.Unlock()
return c.ch, nil return c.ch, nil
} }
@ -103,5 +99,5 @@ func (c *PubSubClient) Subscribe(channels ...string) (chan *Message, error) {
func (c *PubSubClient) Unsubscribe(channels ...string) error { func (c *PubSubClient) Unsubscribe(channels ...string) error {
args := append([]string{"UNSUBSCRIBE"}, channels...) args := append([]string{"UNSUBSCRIBE"}, channels...)
req := NewMultiBulkReq(args...) req := NewMultiBulkReq(args...)
return c.WriteReq(req.Req()) return c.WriteReq(req.Req(), c.conn)
} }

189
redis.go
View File

@ -1,145 +1,156 @@
package redis package redis
import ( import (
"crypto/tls"
"fmt" "fmt"
"io" "io"
"net"
"sync" "sync"
"github.com/vmihailenco/bufreader" "github.com/vmihailenco/bufreader"
) )
type connectFunc func() (io.ReadWriter, error) type OpenConnFunc func() (io.ReadWriter, error)
type disconnectFunc func(io.ReadWriter) type CloseConnFunc func(io.ReadWriter)
type InitConnFunc func(*Client) error
func TCPConnector(addr string) OpenConnFunc {
return func() (io.ReadWriter, error) {
return net.Dial("tcp", addr)
}
}
func TLSConnector(addr string, tlsConfig *tls.Config) OpenConnFunc {
return func() (io.ReadWriter, error) {
return tls.Dial("tcp", addr, tlsConfig)
}
}
func AuthSelectFunc(password string, db int64) InitConnFunc {
return func(client *Client) error {
if password != "" {
_, err := client.Auth(password).Reply()
if err != nil {
return err
}
}
_, err := client.Select(db).Reply()
if err != nil {
return err
}
return nil
}
}
func createReader() (*bufreader.Reader, error) { func createReader() (*bufreader.Reader, error) {
return bufreader.NewSizedReader(8192), nil return bufreader.NewSizedReader(8192), nil
} }
type Client struct { type Client struct {
mtx sync.Mutex mtx sync.Mutex
connect connectFunc ConnPool *ConnPool
disconnect disconnectFunc InitConn InitConnFunc
currConn io.ReadWriter
readerPool *bufreader.ReaderPool
reqs []Req reqs []Req
} }
func NewClient(connect connectFunc, disconnect disconnectFunc) *Client { func NewClient(openConn OpenConnFunc, closeConn CloseConnFunc, initConn InitConnFunc) *Client {
return &Client{ return &Client{
readerPool: bufreader.NewReaderPool(100, createReader), ConnPool: NewConnPool(openConn, closeConn, 10),
connect: connect, InitConn: initConn,
disconnect: disconnect,
reqs: make([]Req, 0),
} }
} }
func NewMultiClient(connect connectFunc, disconnect disconnectFunc) *Client { func NewTCPClient(addr string, password string, db int64) *Client {
return &Client{ return NewClient(TCPConnector(addr), nil, AuthSelectFunc(password, db))
readerPool: bufreader.NewReaderPool(100, createReader),
connect: connect,
disconnect: disconnect,
reqs: make([]Req, 0),
}
} }
func (c *Client) Close() error { func NewTLSClient(addr string, tlsConfig *tls.Config, password string, db int64) *Client {
if c.disconnect != nil { return NewClient(
c.disconnect(c.currConn) TLSConnector(addr, tlsConfig),
} nil,
c.currConn = nil AuthSelectFunc(password, db),
return nil )
} }
func (c *Client) conn() (io.ReadWriter, error) { func (c *Client) WriteReq(buf []byte, conn *Conn) error {
if c.currConn == nil { _, err := conn.RW.Write(buf)
currConn, err := c.connect()
if err != nil {
return nil, err
}
c.currConn = currConn
}
return c.currConn, nil
}
func (c *Client) WriteReq(buf []byte) error {
conn, err := c.conn()
if err != nil {
return err
}
_, err = conn.Write(buf)
if err != nil {
c.Close()
}
return err return err
} }
func (c *Client) ReadReply(rd *bufreader.Reader) error { func (c *Client) ReadReply(conn *Conn) error {
conn, err := c.conn() _, err := conn.Rd.ReadFrom(conn.RW)
if err != nil { if err != nil {
return err return err
} }
_, err = rd.ReadFrom(conn)
if err != nil {
c.Close()
return err
}
return nil return nil
} }
func (c *Client) WriteRead(buf []byte, rd *bufreader.Reader) error { func (c *Client) WriteRead(buf []byte, conn *Conn) error {
c.mtx.Lock() c.mtx.Lock()
defer c.mtx.Unlock() defer c.mtx.Unlock()
if err := c.WriteReq(buf); err != nil { if err := c.WriteReq(buf, conn); err != nil {
return err return err
} }
return c.ReadReply(rd) return c.ReadReply(conn)
}
func (c *Client) Process(req Req) {
if c.reqs == nil {
c.Run(req)
} else {
c.Queue(req)
}
} }
func (c *Client) Queue(req Req) { func (c *Client) Queue(req Req) {
req.SetClient(c)
c.mtx.Lock() c.mtx.Lock()
c.reqs = append(c.reqs, req) c.reqs = append(c.reqs, req)
c.mtx.Unlock() c.mtx.Unlock()
} }
func (c *Client) Run(req Req) { func (c *Client) Run(req Req) {
rd, err := c.readerPool.Get() conn, _, err := c.ConnPool.Get()
if err != nil {
req.SetErr(err)
return
}
defer c.readerPool.Add(rd)
err = c.WriteRead(req.Req(), rd)
if err != nil { if err != nil {
c.ConnPool.Remove(conn)
req.SetErr(err) req.SetErr(err)
return return
} }
val, err := req.ParseReply(rd) err = c.WriteRead(req.Req(), conn)
if err != nil { if err != nil {
c.ConnPool.Remove(conn)
req.SetErr(err) req.SetErr(err)
return return
} }
val, err := req.ParseReply(conn.Rd)
if err != nil {
c.ConnPool.Remove(conn)
req.SetErr(err)
return
}
c.ConnPool.Add(conn)
req.SetVal(val) req.SetVal(val)
} }
func (c *Client) RunQueued() ([]Req, error) { func (c *Client) RunQueued() ([]Req, error) {
c.mtx.Lock()
if len(c.reqs) == 0 { if len(c.reqs) == 0 {
c.mtx.Unlock()
return c.reqs, nil return c.reqs, nil
} }
c.mtx.Lock()
reqs := c.reqs reqs := c.reqs
c.reqs = make([]Req, 0) c.reqs = make([]Req, 0)
c.mtx.Unlock() c.mtx.Unlock()
return c.RunReqs(reqs)
}
func (c *Client) RunReqs(reqs []Req) ([]Req, error) {
var multiReq []byte var multiReq []byte
if len(reqs) == 1 { if len(reqs) == 1 {
multiReq = reqs[0].Req() multiReq = reqs[0].Req()
@ -150,18 +161,20 @@ func (c *Client) RunQueued() ([]Req, error) {
} }
} }
rd, err := c.readerPool.Get() conn, _, err := c.ConnPool.Get()
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer c.readerPool.Add(rd)
err = c.WriteRead(multiReq, rd) err = c.WriteRead(multiReq, conn)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, req := range reqs {
val, err := req.ParseReply(rd) for i := 0; i < len(reqs); i++ {
req := reqs[i]
val, err := req.ParseReply(conn.Rd)
if err != nil { if err != nil {
req.SetErr(err) req.SetErr(err)
} else { } else {
@ -181,11 +194,11 @@ func (c *Client) Discard() {
} }
func (c *Client) Exec() ([]Req, error) { func (c *Client) Exec() ([]Req, error) {
c.mtx.Lock()
if len(c.reqs) == 0 { if len(c.reqs) == 0 {
c.mtx.Unlock()
return c.reqs, nil return c.reqs, nil
} }
c.mtx.Lock()
reqs := c.reqs reqs := c.reqs
c.reqs = make([]Req, 0) c.reqs = make([]Req, 0)
c.mtx.Unlock() c.mtx.Unlock()
@ -197,13 +210,12 @@ func (c *Client) Exec() ([]Req, error) {
} }
multiReq = append(multiReq, PackReq([]string{"EXEC"})...) multiReq = append(multiReq, PackReq([]string{"EXEC"})...)
rd, err := c.readerPool.Get() conn, _, err := c.ConnPool.Get()
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer c.readerPool.Add(rd)
err = c.WriteRead(multiReq, rd) err = c.WriteRead(multiReq, conn)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -211,31 +223,32 @@ func (c *Client) Exec() ([]Req, error) {
statusReq := NewStatusReq() statusReq := NewStatusReq()
// Parse MULTI command reply. // Parse MULTI command reply.
_, err = statusReq.ParseReply(rd) _, err = statusReq.ParseReply(conn.Rd)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Parse queued replies. // Parse queued replies.
for _ = range reqs { for _ = range reqs {
_, err = statusReq.ParseReply(rd) _, err = statusReq.ParseReply(conn.Rd)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
// Parse number of replies. // Parse number of replies.
line, err := rd.ReadLine('\n') line, err := conn.Rd.ReadLine('\n')
if err != nil { if err != nil {
return nil, err return nil, err
} }
if line[0] != '*' { if line[0] != '*' {
return nil, fmt.Errorf("Expected '*', but got line %q of %q.", line, rd.Bytes()) return nil, fmt.Errorf("Expected '*', but got line %q of %q.", line, conn.Rd.Bytes())
} }
// Parse replies. // Parse replies.
for _, req := range reqs { for i := 0; i < len(reqs); i++ {
val, err := req.ParseReply(rd) req := reqs[i]
val, err := req.ParseReply(conn.Rd)
if err != nil { if err != nil {
req.SetErr(err) req.SetErr(err)
} else { } else {

File diff suppressed because it is too large Load Diff

View File

@ -11,6 +11,8 @@ import (
var Nil = errors.New("(nil)") var Nil = errors.New("(nil)")
var errResultMissing = errors.New("Request was not run properly.")
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
func isNil(buf []byte) bool { func isNil(buf []byte) bool {
@ -73,9 +75,10 @@ func PackReq(args []string) []byte {
type Req interface { type Req interface {
Req() []byte Req() []byte
ParseReply(*bufreader.Reader) (interface{}, error) ParseReply(*bufreader.Reader) (interface{}, error)
SetClient(*Client)
SetErr(error) SetErr(error)
Err() error
SetVal(interface{}) SetVal(interface{})
Val() interface{}
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -83,10 +86,6 @@ type Req interface {
type BaseReq struct { type BaseReq struct {
args []string args []string
client *Client
// TODO: use int32 and atomic access?
done bool
val interface{} val interface{}
err error err error
} }
@ -101,26 +100,28 @@ func (r *BaseReq) Req() []byte {
return PackReq(r.args) return PackReq(r.args)
} }
func (r *BaseReq) SetClient(c *Client) {
r.client = c
}
func (r *BaseReq) SetErr(err error) { func (r *BaseReq) SetErr(err error) {
if err == nil { if err == nil {
panic("non-nil value expected") panic("non-nil value expected")
} }
r.done = true
r.err = err r.err = err
} }
func (r *BaseReq) Err() error {
return r.err
}
func (r *BaseReq) SetVal(val interface{}) { func (r *BaseReq) SetVal(val interface{}) {
if val == nil { if val == nil {
panic("non-nil value expected") panic("non-nil value expected")
} }
r.done = true
r.val = val r.val = val
} }
func (r *BaseReq) Val() interface{} {
return r.val
}
func (r *BaseReq) ParseReply(rd *bufreader.Reader) (interface{}, error) { func (r *BaseReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
panic("abstract") panic("abstract")
} }
@ -153,17 +154,9 @@ func (r *StatusReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
} }
func (r *StatusReq) Reply() (string, error) { func (r *StatusReq) Reply() (string, error) {
if !r.done { if r.val == nil && r.err == nil {
_, err := r.client.RunQueued() return "", errResultMissing
if err != nil { } else if r.err != nil {
return "", err
}
if !r.done {
panic("req is not ready")
}
}
if r.err != nil {
return "", r.err return "", r.err
} }
return r.val.(string), nil return r.val.(string), nil
@ -197,17 +190,9 @@ func (r *IntReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
} }
func (r *IntReq) Reply() (int64, error) { func (r *IntReq) Reply() (int64, error) {
if !r.done { if r.val == nil && r.err == nil {
_, err := r.client.RunQueued() return 0, errResultMissing
if err != nil { } else if r.err != nil {
return 0, err
}
if !r.done {
panic("req is not ready")
}
}
if r.err != nil {
return 0, r.err return 0, r.err
} }
return r.val.(int64), nil return r.val.(int64), nil
@ -243,17 +228,9 @@ func (r *IntNilReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
} }
func (r *IntNilReq) Reply() (int64, error) { func (r *IntNilReq) Reply() (int64, error) {
if !r.done { if r.val == nil && r.err == nil {
_, err := r.client.RunQueued() return 0, errResultMissing
if err != nil { } else if r.err != nil {
return 0, err
}
if !r.done {
panic("req is not ready")
}
}
if r.err != nil {
return 0, r.err return 0, r.err
} }
return r.val.(int64), nil return r.val.(int64), nil
@ -287,17 +264,9 @@ func (r *BoolReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
} }
func (r *BoolReq) Reply() (bool, error) { func (r *BoolReq) Reply() (bool, error) {
if !r.done { if r.val == nil && r.err == nil {
_, err := r.client.RunQueued() return false, errResultMissing
if err != nil { } else if r.err != nil {
return false, err
}
if !r.done {
panic("req is not ready")
}
}
if r.err != nil {
return false, r.err return false, r.err
} }
return r.val.(bool), nil return r.val.(bool), nil
@ -340,17 +309,9 @@ func (r *BulkReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
} }
func (r *BulkReq) Reply() (string, error) { func (r *BulkReq) Reply() (string, error) {
if !r.done { if r.val == nil && r.err == nil {
_, err := r.client.RunQueued() return "", errResultMissing
if err != nil { } else if r.err != nil {
return "", err
}
if !r.done {
panic("req is not ready")
}
}
if r.err != nil {
return "", r.err return "", r.err
} }
return r.val.(string), nil return r.val.(string), nil
@ -393,17 +354,9 @@ func (r *FloatReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
} }
func (r *FloatReq) Reply() (float64, error) { func (r *FloatReq) Reply() (float64, error) {
if !r.done { if r.val == nil && r.err == nil {
_, err := r.client.RunQueued() return 0, errResultMissing
if err != nil { } else if r.err != nil {
return 0, err
}
if !r.done {
panic("req is not ready")
}
}
if r.err != nil {
return 0, r.err return 0, r.err
} }
return r.val.(float64), nil return r.val.(float64), nil
@ -488,17 +441,9 @@ func (r *MultiBulkReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
} }
func (r *MultiBulkReq) Reply() ([]interface{}, error) { func (r *MultiBulkReq) Reply() ([]interface{}, error) {
if !r.done { if r.val == nil && r.err == nil {
_, err := r.client.RunQueued() return nil, errResultMissing
if err != nil { } else if r.err != nil {
return nil, err
}
if !r.done {
panic("req is not ready")
}
}
if r.err != nil {
return nil, r.err return nil, r.err
} }
return r.val.([]interface{}), nil return r.val.([]interface{}), nil