pulled functions out of New()
This commit is contained in:
parent
2759dd6e2f
commit
7d99b0ea9e
1 changed files with 37 additions and 31 deletions
|
@ -19,41 +19,47 @@ func (d *driver) New(conf *config.Storage) (storage.Conn, error) {
|
||||||
return &Conn{
|
return &Conn{
|
||||||
conf: conf,
|
conf: conf,
|
||||||
pool: &redis.Pool{
|
pool: &redis.Pool{
|
||||||
MaxIdle: 3,
|
MaxIdle: 3,
|
||||||
IdleTimeout: 240 * time.Second,
|
IdleTimeout: 240 * time.Second,
|
||||||
Dial: func() (redis.Conn, error) {
|
Dial: makeDialFunc(conf),
|
||||||
var (
|
TestOnBorrow: testOnBorrow,
|
||||||
conn redis.Conn
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
if conf.ConnectTimeout != nil &&
|
|
||||||
conf.ReadTimeout != nil &&
|
|
||||||
conf.WriteTimeout != nil {
|
|
||||||
|
|
||||||
conn, err = redis.DialTimeout(
|
|
||||||
conf.Network,
|
|
||||||
conf.Addr,
|
|
||||||
conf.ConnectTimeout.Duration,
|
|
||||||
conf.ReadTimeout.Duration,
|
|
||||||
conf.WriteTimeout.Duration,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
conn, err = redis.Dial(conf.Network, conf.Addr)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return conn, nil
|
|
||||||
},
|
|
||||||
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
|
||||||
_, err := c.Do("PING")
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeDialFunc(conf *config.Storage) func() (redis.Conn, error) {
|
||||||
|
return func() (redis.Conn, error) {
|
||||||
|
var (
|
||||||
|
conn redis.Conn
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if conf.ConnectTimeout != nil &&
|
||||||
|
conf.ReadTimeout != nil &&
|
||||||
|
conf.WriteTimeout != nil {
|
||||||
|
|
||||||
|
conn, err = redis.DialTimeout(
|
||||||
|
conf.Network,
|
||||||
|
conf.Addr,
|
||||||
|
conf.ConnectTimeout.Duration,
|
||||||
|
conf.ReadTimeout.Duration,
|
||||||
|
conf.WriteTimeout.Duration,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
conn, err = redis.Dial(conf.Network, conf.Addr)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testOnBorrow(c redis.Conn, t time.Time) error {
|
||||||
|
_, err := c.Do("PING")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
conf *config.Storage
|
conf *config.Storage
|
||||||
pool *redis.Pool
|
pool *redis.Pool
|
||||||
|
|
Loading…
Add table
Reference in a new issue