mirror of
https://github.com/redis/go-redis.git
synced 2025-07-31 05:04:23 +03:00
Reduce number of various options structs. Slightly reword comments.
This commit is contained in:
75
sentinel.go
75
sentinel.go
@ -11,75 +11,41 @@ import (
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// FailoverOptions are used to configure a failover client and should
|
||||
// be passed to NewFailoverClient.
|
||||
type FailoverOptions struct {
|
||||
// The master name.
|
||||
MasterName string
|
||||
// Seed addresses of sentinel nodes.
|
||||
// A seed list of host:port addresses of sentinel nodes.
|
||||
SentinelAddrs []string
|
||||
|
||||
// An optional password. Must match the password specified in the
|
||||
// `requirepass` server configuration option.
|
||||
Password string
|
||||
// Select a database.
|
||||
// Default: 0
|
||||
DB int64
|
||||
// Following options are copied from Options struct.
|
||||
|
||||
// Sets the deadline for establishing new connections. If reached,
|
||||
// deal attepts will fail with a timeout.
|
||||
DialTimeout time.Duration
|
||||
// Sets the deadline for socket reads. If reached, commands will
|
||||
// fail with a timeout instead of blocking.
|
||||
ReadTimeout time.Duration
|
||||
// Sets the deadline for socket writes. If reached, commands will
|
||||
// fail with a timeout instead of blocking.
|
||||
Password string
|
||||
DB int64
|
||||
|
||||
DialTimeout time.Duration
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
|
||||
// The maximum number of socket connections.
|
||||
// Default: 10
|
||||
PoolSize int
|
||||
// If all socket connections is the pool are busy, the pool will wait
|
||||
// this amount of time for a conection to become available, before
|
||||
// returning an error.
|
||||
// Default: 5s
|
||||
PoolSize int
|
||||
PoolTimeout time.Duration
|
||||
// Evict connections from the pool after they have been idle for longer
|
||||
// than specified in this option.
|
||||
// Default: 0 = no eviction
|
||||
IdleTimeout time.Duration
|
||||
}
|
||||
|
||||
func (opt *FailoverOptions) getPoolSize() int {
|
||||
if opt.PoolSize == 0 {
|
||||
return 10
|
||||
}
|
||||
return opt.PoolSize
|
||||
}
|
||||
func (opt *FailoverOptions) options() *Options {
|
||||
return &Options{
|
||||
Addr: "FailoverClient",
|
||||
|
||||
func (opt *FailoverOptions) getPoolTimeout() time.Duration {
|
||||
if opt.PoolTimeout == 0 {
|
||||
return 5 * time.Second
|
||||
}
|
||||
return opt.PoolTimeout
|
||||
}
|
||||
|
||||
func (opt *FailoverOptions) getDialTimeout() time.Duration {
|
||||
if opt.DialTimeout == 0 {
|
||||
return 5 * time.Second
|
||||
}
|
||||
return opt.DialTimeout
|
||||
}
|
||||
|
||||
func (opt *FailoverOptions) options() *options {
|
||||
return &options{
|
||||
DB: opt.DB,
|
||||
Password: opt.Password,
|
||||
|
||||
DialTimeout: opt.getDialTimeout(),
|
||||
DialTimeout: opt.DialTimeout,
|
||||
ReadTimeout: opt.ReadTimeout,
|
||||
WriteTimeout: opt.WriteTimeout,
|
||||
|
||||
PoolSize: opt.getPoolSize(),
|
||||
PoolTimeout: opt.getPoolTimeout(),
|
||||
PoolSize: opt.PoolSize,
|
||||
PoolTimeout: opt.PoolTimeout,
|
||||
IdleTimeout: opt.IdleTimeout,
|
||||
}
|
||||
}
|
||||
@ -104,11 +70,10 @@ type sentinelClient struct {
|
||||
*baseClient
|
||||
}
|
||||
|
||||
func newSentinel(clOpt *Options) *sentinelClient {
|
||||
opt := clOpt.options()
|
||||
func newSentinel(opt *Options) *sentinelClient {
|
||||
base := &baseClient{
|
||||
opt: opt,
|
||||
connPool: newConnPool(opt.connPoolOptions()),
|
||||
connPool: newConnPool(opt),
|
||||
}
|
||||
return &sentinelClient{
|
||||
baseClient: base,
|
||||
@ -141,7 +106,7 @@ type sentinelFailover struct {
|
||||
masterName string
|
||||
sentinelAddrs []string
|
||||
|
||||
opt *options
|
||||
opt *Options
|
||||
|
||||
pool pool
|
||||
poolOnce sync.Once
|
||||
@ -161,7 +126,7 @@ func (d *sentinelFailover) dial() (net.Conn, error) {
|
||||
func (d *sentinelFailover) Pool() pool {
|
||||
d.poolOnce.Do(func() {
|
||||
d.opt.Dialer = d.dial
|
||||
d.pool = newConnPool(d.opt.connPoolOptions())
|
||||
d.pool = newConnPool(d.opt)
|
||||
})
|
||||
return d.pool
|
||||
}
|
||||
|
Reference in New Issue
Block a user