Skip to content
New issue

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

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

Already on GitHub? # to your account

fix bug issues #2 #3

Open
wants to merge 1 commit 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
195 changes: 47 additions & 148 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,150 +1,49 @@
package redis

import (
"crypto/tls"
"net"
"time"

"github.com/go-redis/redis"
)

// Options Redis parameter options
type Options struct {
// The network type, either tcp or unix.
// Default is tcp.
Network string
// host:port address.
Addr string

// Dialer creates new network connection and has priority over
// Network and Addr options.
Dialer func() (net.Conn, error)

// Optional password. Must match the password specified in the
// requirepass server configuration option.
Password string
// Database to be selected after connecting to the server.
DB int

// Maximum number of retries before giving up.
// Default is to not retry failed commands.
MaxRetries int
// Minimum backoff between each retry.
// Default is 8 milliseconds; -1 disables backoff.
MinRetryBackoff time.Duration
// Maximum backoff between each retry.
// Default is 512 milliseconds; -1 disables backoff.
MaxRetryBackoff time.Duration

// Dial timeout for establishing new connections.
// Default is 5 seconds.
DialTimeout time.Duration
// Timeout for socket reads. If reached, commands will fail
// with a timeout instead of blocking.
// Default is 3 seconds.
ReadTimeout time.Duration
// Timeout for socket writes. If reached, commands will fail
// with a timeout instead of blocking.
// Default is ReadTimeout.
WriteTimeout time.Duration

// Maximum number of socket connections.
// Default is 10 connections per every CPU as reported by runtime.NumCPU.
PoolSize int
// Amount of time client waits for connection if all connections
// are busy before returning an error.
// Default is ReadTimeout + 1 second.
PoolTimeout time.Duration
// Amount of time after which client closes idle connections.
// Should be less than server's timeout.
// Default is 5 minutes.
IdleTimeout time.Duration
// Frequency of idle checks.
// Default is 1 minute.
// When minus value is set, then idle check is disabled.
IdleCheckFrequency time.Duration

// TLS Config to use. When set TLS will be negotiated.
TLSConfig *tls.Config
}

func (o *Options) redisOptions() *redis.Options {
return &redis.Options{
Network: o.Network,
Addr: o.Addr,
Dialer: o.Dialer,
Password: o.Password,
DB: o.DB,
MaxRetries: o.MaxRetries,
MinRetryBackoff: o.MinRetryBackoff,
MaxRetryBackoff: o.MaxRetryBackoff,
DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
PoolSize: o.PoolSize,
PoolTimeout: o.PoolTimeout,
IdleTimeout: o.IdleTimeout,
IdleCheckFrequency: o.IdleCheckFrequency,
TLSConfig: o.TLSConfig,
}
}

// ClusterOptions are used to configure a cluster client and should be
// passed to NewClusterClient.
type ClusterOptions struct {
// A seed list of host:port addresses of cluster nodes.
Addrs []string

// The maximum number of retries before giving up. Command is retried
// on network errors and MOVED/ASK redirects.
// Default is 8.
MaxRedirects int

// Enables read-only commands on slave nodes.
ReadOnly bool
// Allows routing read-only commands to the closest master or slave node.
RouteByLatency bool
// Allows routing read-only commands to the random master or slave node.
RouteRandomly bool

// Following options are copied from Options struct.

OnConnect func(*redis.Conn) error

MaxRetries int
MinRetryBackoff time.Duration
MaxRetryBackoff time.Duration
Password string

DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration

// PoolSize applies per cluster node and not for the whole cluster.
PoolSize int
PoolTimeout time.Duration
IdleTimeout time.Duration
IdleCheckFrequency time.Duration
}

func (o *ClusterOptions) redisClusterOptions() *redis.ClusterOptions {
return &redis.ClusterOptions{
Addrs: o.Addrs,
MaxRedirects: o.MaxRedirects,
ReadOnly: o.ReadOnly,
RouteByLatency: o.RouteByLatency,
RouteRandomly: o.RouteRandomly,
OnConnect: o.OnConnect,
MaxRetries: o.MaxRetries,
MinRetryBackoff: o.MinRetryBackoff,
MaxRetryBackoff: o.MaxRetryBackoff,
Password: o.Password,
DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
PoolSize: o.PoolSize,
PoolTimeout: o.PoolTimeout,
IdleTimeout: o.IdleTimeout,
IdleCheckFrequency: o.IdleCheckFrequency,
}
}
// import (
// "github.com/go-redis/redis"
// )

// func (o *redis.Options) redisOptions() *redis.Options {
// return &redis.Options{
// Network: o.Network,
// Addr: o.Addr,
// Dialer: o.Dialer,
// Password: o.Password,
// DB: o.DB,
// MaxRetries: o.MaxRetries,
// MinRetryBackoff: o.MinRetryBackoff,
// MaxRetryBackoff: o.MaxRetryBackoff,
// DialTimeout: o.DialTimeout,
// ReadTimeout: o.ReadTimeout,
// WriteTimeout: o.WriteTimeout,
// PoolSize: o.PoolSize,
// PoolTimeout: o.PoolTimeout,
// IdleTimeout: o.IdleTimeout,
// IdleCheckFrequency: o.IdleCheckFrequency,
// TLSConfig: o.TLSConfig,
// }
// }


// func (o *redis.ClusterOptions) redisClusterOptions() *redis.ClusterOptions {
// return &redis.ClusterOptions{
// Addrs: o.Addrs,
// MaxRedirects: o.MaxRedirects,
// ReadOnly: o.ReadOnly,
// RouteByLatency: o.RouteByLatency,
// RouteRandomly: o.RouteRandomly,
// OnConnect: o.OnConnect,
// MaxRetries: o.MaxRetries,
// MinRetryBackoff: o.MinRetryBackoff,
// MaxRetryBackoff: o.MaxRetryBackoff,
// Password: o.Password,
// DialTimeout: o.DialTimeout,
// ReadTimeout: o.ReadTimeout,
// WriteTimeout: o.WriteTimeout,
// PoolSize: o.PoolSize,
// PoolTimeout: o.PoolTimeout,
// IdleTimeout: o.IdleTimeout,
// IdleCheckFrequency: o.IdleCheckFrequency,
// }
// }
8 changes: 4 additions & 4 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ var (
)

// NewRedisStore create an instance of a redis store
func NewRedisStore(opts *Options, prefix ...string) session.ManagerStore {
func NewRedisStore(opts *redis.Options, prefix ...string) session.ManagerStore {
if opts == nil {
panic("options cannot be nil")
}
return NewRedisStoreWithCli(
redis.NewClient(opts.redisOptions()),
redis.NewClient(opts),
prefix...,
)
}
Expand All @@ -40,12 +40,12 @@ func NewRedisStoreWithCli(cli *redis.Client, prefix ...string) session.ManagerSt
}

// NewRedisClusterStore create an instance of a redis cluster store
func NewRedisClusterStore(opts *ClusterOptions, prefix ...string) session.ManagerStore {
func NewRedisClusterStore(opts *redis.ClusterOptions, prefix ...string) session.ManagerStore {
if opts == nil {
panic("options cannot be nil")
}
return NewRedisClusterStoreWithCli(
redis.NewClusterClient(opts.redisClusterOptions()),
redis.NewClusterClient(opts),
prefix...,
)
}
Expand Down