diff --git a/redisearch/client.go b/redisearch/client.go index f071d5d..721c58c 100644 --- a/redisearch/client.go +++ b/redisearch/client.go @@ -21,14 +21,14 @@ var maxConns = 500 // NewClient creates a new client connecting to the redis host, and using the given name as key prefix. // Addr can be a single host:port pair, or a comma separated list of host:port,host:port... // In the case of multiple hosts we create a multi-pool and select connections at random -func NewClient(addr, name string) *Client { +func NewClient(addr, name string,options ...redis.DialOption) *Client { addrs := strings.Split(addr, ",") var pool ConnPool if len(addrs) == 1 { - pool = NewSingleHostPool(addrs[0]) + pool = NewSingleHostPool(addrs[0],options...) } else { - pool = NewMultiHostPool(addrs) + pool = NewMultiHostPool(addrs,options...) } ret := &Client{ pool: pool, diff --git a/redisearch/pool.go b/redisearch/pool.go index c1524ac..f7c639b 100644 --- a/redisearch/pool.go +++ b/redisearch/pool.go @@ -2,10 +2,11 @@ package redisearch import ( "fmt" - "github.com/gomodule/redigo/redis" "math/rand" "sync" "time" + + "github.com/gomodule/redigo/redis" ) type ConnPool interface { @@ -17,9 +18,9 @@ type SingleHostPool struct { *redis.Pool } -func NewSingleHostPool(host string) *SingleHostPool { +func NewSingleHostPool(host string, options ...redis.DialOption) *SingleHostPool { pool := &redis.Pool{Dial: func() (redis.Conn, error) { - return redis.Dial("tcp", host) + return redis.Dial("tcp", host, options...) }, MaxIdle: maxConns} pool.TestOnBorrow = func(c redis.Conn, t time.Time) (err error) { if time.Since(t) > time.Second { @@ -32,15 +33,17 @@ func NewSingleHostPool(host string) *SingleHostPool { type MultiHostPool struct { sync.Mutex - pools map[string]*redis.Pool - hosts []string + pools map[string]*redis.Pool + hosts []string + options []redis.DialOption } -func NewMultiHostPool(hosts []string) *MultiHostPool { +func NewMultiHostPool(hosts []string, options ...redis.DialOption) *MultiHostPool { return &MultiHostPool{ - pools: make(map[string]*redis.Pool, len(hosts)), - hosts: hosts, + pools: make(map[string]*redis.Pool, len(hosts)), + hosts: hosts, + options: options, } } @@ -50,15 +53,17 @@ func (p *MultiHostPool) Get() redis.Conn { host := p.hosts[rand.Intn(len(p.hosts))] pool, found := p.pools[host] if !found { - pool = redis.NewPool(func() (redis.Conn, error) { - // TODO: Add timeouts. and 2 separate pools for indexing and querying, with different timeouts - return redis.Dial("tcp", host) - }, maxConns) - pool.TestOnBorrow = func(c redis.Conn, t time.Time) (err error) { - if time.Since(t) > time.Second { - _, err = c.Do("PING") - } - return err + pool = &redis.Pool{ + Dial: func() (redis.Conn, error) { + return redis.Dial("tcp", host, p.options...) + }, + TestOnBorrow: func(c redis.Conn, t time.Time) error { + if time.Since(t) < time.Minute { + return nil + } + _, err := c.Do("PING") + return err + }, } p.pools[host] = pool