From 7c697bb680e9a19a15781179bea0a884bb82c79c Mon Sep 17 00:00:00 2001 From: lyric Date: Tue, 10 Sep 2019 22:35:22 +0800 Subject: [PATCH 1/2] Fixed redis options --- options.go | 157 -------------------------------------------------- redis.go | 8 +-- redis_test.go | 12 ++-- 3 files changed, 10 insertions(+), 167 deletions(-) delete mode 100644 options.go diff --git a/options.go b/options.go deleted file mode 100644 index 906b139..0000000 --- a/options.go +++ /dev/null @@ -1,157 +0,0 @@ -package redis - -import ( - "context" - "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(ctx context.Context, network, addr string) (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 - - // KeyNamespace used for prefixing/namespacing keys. - KeyNamespace string -} - -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 - - // KeyNamespace used for prefixing/namespacing keys. - KeyNamespace string -} - -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, - } -} diff --git a/redis.go b/redis.go index 6ae72bf..c7286de 100644 --- a/redis.go +++ b/redis.go @@ -18,11 +18,11 @@ var ( ) // NewRedisStore create an instance of a redis store -func NewRedisStore(opts *Options) *TokenStore { +func NewRedisStore(opts *redis.Options, keyNamespace ...string) *TokenStore { if opts == nil { panic("options cannot be nil") } - return NewRedisStoreWithCli(redis.NewClient(opts.redisOptions()), opts.KeyNamespace) + return NewRedisStoreWithCli(redis.NewClient(opts), keyNamespace...) } // NewRedisStoreWithCli create an instance of a redis store @@ -38,11 +38,11 @@ func NewRedisStoreWithCli(cli *redis.Client, keyNamespace ...string) *TokenStore } // NewRedisClusterStore create an instance of a redis cluster store -func NewRedisClusterStore(opts *ClusterOptions) *TokenStore { +func NewRedisClusterStore(opts *redis.ClusterOptions, keyNamespace ...string) *TokenStore { if opts == nil { panic("options cannot be nil") } - return NewRedisClusterStoreWithCli(redis.NewClusterClient(opts.redisClusterOptions()), opts.KeyNamespace) + return NewRedisClusterStoreWithCli(redis.NewClusterClient(opts), keyNamespace...) } // NewRedisClusterStoreWithCli create an instance of a redis cluster store diff --git a/redis_test.go b/redis_test.go index 945d700..0cf7c3c 100644 --- a/redis_test.go +++ b/redis_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + "github.com/go-redis/redis" "gopkg.in/oauth2.v3/models" . "github.com/smartystreets/goconvey/convey" @@ -16,7 +17,7 @@ const ( func TestTokenStore(t *testing.T) { Convey("Test redis token store", t, func() { - opts := &Options{ + opts := &redis.Options{ Addr: addr, DB: db, } @@ -104,12 +105,11 @@ func TestTokenStore(t *testing.T) { func TestTokenStoreWithKeyNamespace(t *testing.T) { Convey("Test redis token store", t, func() { - opts := &Options{ - Addr: addr, - DB: db, - KeyNamespace: "test:", + opts := &redis.Options{ + Addr: addr, + DB: db, } - store := NewRedisStore(opts) + store := NewRedisStore(opts, "test:") Convey("Test authorization code store", func() { info := &models.Token{ From b02ea389b7b6d461d10ef7c327d16ee9c4a4224d Mon Sep 17 00:00:00 2001 From: lyric Date: Tue, 10 Sep 2019 22:37:12 +0800 Subject: [PATCH 2/2] Update usage --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 219be21..e07c757 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,8 @@ $ go get -u -v gopkg.in/go-oauth2/redis.v3 package main import ( - "gopkg.in/go-oauth2/redis.v3" + "github.com/go-redis/redis" + oredis "gopkg.in/go-oauth2/redis.v3" "gopkg.in/oauth2.v3/manage" ) @@ -22,13 +23,13 @@ func main() { manager := manage.NewDefaultManager() // use redis token store - manager.MapTokenStorage(redis.NewRedisStore(&redis.Options{ + manager.MapTokenStorage(oredis.NewRedisStore(&redis.Options{ Addr: "127.0.0.1:6379", DB: 15, })) // use redis cluster store - // manager.MapTokenStorage(redis.NewRedisClusterStore(&redis.ClusterOptions{ + // manager.MapTokenStorage(oredis.NewRedisClusterStore(&redis.ClusterOptions{ // Addrs: []string{"127.0.0.1:6379"}, // DB: 15, // }))