list block pop support milliseconds timeout

This commit is contained in:
siddontang 2014-10-17 11:41:05 +08:00
parent c30979e5b5
commit f715de3561
2 changed files with 10 additions and 6 deletions

View File

@ -494,15 +494,15 @@ func (db *DB) lEncodeMaxKey() []byte {
return ek return ek
} }
func (db *DB) BLPop(keys [][]byte, timeout int) ([]interface{}, error) { func (db *DB) BLPop(keys [][]byte, timeout time.Duration) ([]interface{}, error) {
return db.lblockPop(keys, listHeadSeq, timeout) return db.lblockPop(keys, listHeadSeq, timeout)
} }
func (db *DB) BRPop(keys [][]byte, timeout int) ([]interface{}, error) { func (db *DB) BRPop(keys [][]byte, timeout time.Duration) ([]interface{}, error) {
return db.lblockPop(keys, listTailSeq, timeout) return db.lblockPop(keys, listTailSeq, timeout)
} }
func (db *DB) lblockPop(keys [][]byte, whereSeq int32, timeout int) ([]interface{}, error) { func (db *DB) lblockPop(keys [][]byte, whereSeq int32, timeout time.Duration) ([]interface{}, error) {
ch := make(chan []byte) ch := make(chan []byte)
bkeys := [][]byte{} bkeys := [][]byte{}
@ -530,7 +530,7 @@ func (db *DB) lblockPop(keys [][]byte, whereSeq int32, timeout int) ([]interface
} }
}() }()
deadT := time.Now().Add(time.Duration(timeout) * time.Second) deadT := time.Now().Add(timeout)
for { for {
if timeout == 0 { if timeout == 0 {

View File

@ -4,6 +4,7 @@ import (
"github.com/siddontang/go/hack" "github.com/siddontang/go/hack"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"strconv" "strconv"
"time"
) )
func lpushCommand(c *client) error { func lpushCommand(c *client) error {
@ -280,17 +281,20 @@ func brpopCommand(c *client) error {
} }
func lParseBPopArgs(c *client) (keys [][]byte, timeout int, err error) { func lParseBPopArgs(c *client) (keys [][]byte, timeout time.Duration, err error) {
args := c.args args := c.args
if len(args) < 2 { if len(args) < 2 {
err = ErrCmdParams err = ErrCmdParams
return return
} }
if timeout, err = strconv.Atoi(hack.String(args[len(args)-1])); err != nil { var t float64
if t, err = strconv.ParseFloat(hack.String(args[len(args)-1]), 64); err != nil {
return return
} }
timeout = time.Duration(t * float64(time.Second))
keys = args[0 : len(args)-1] keys = args[0 : len(args)-1]
return return
} }