Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support redis DialOption #132

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions redisearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 22 additions & 17 deletions redisearch/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,9 +18,9 @@ type SingleHostPool struct {
*redis.Pool
}

func NewSingleHostPool(host string) *SingleHostPool {
func NewSingleHostPool(host string, options ...redis.DialOption) *SingleHostPool {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function NewSingleHostPool should have comment or be unexported

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 {
Expand All @@ -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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function NewMultiHostPool should have comment or be unexported


return &MultiHostPool{
pools: make(map[string]*redis.Pool, len(hosts)),
hosts: hosts,
pools: make(map[string]*redis.Pool, len(hosts)),
hosts: hosts,
options: options,
}
}

Expand All @@ -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
Expand Down