Skip to content

Commit

Permalink
update redis client
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Apr 25, 2018
1 parent 552c26c commit de39c2c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## Install

``` bash
$ go get -u github.com/go-oauth2/redis
$ go get -u -v gopkg.in/go-oauth2/redis.v1
```

## Usage
Expand All @@ -18,7 +18,7 @@ $ go get -u github.com/go-oauth2/redis
package main

import (
"github.com/go-oauth2/redis"
"gopkg.in/go-oauth2/redis.v1"
"gopkg.in/oauth2.v3/manage"
)

Expand Down
81 changes: 65 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,90 @@
package redis

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

// Config Redis Configuration
"github.com/go-redis/redis"
)

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

// An optional password. Must match the password specified in the
// 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
// A database to be selected after connecting to server.
// Database to be selected after connecting to the server.
DB int

// The maximum number of retries before giving up.
// 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

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

// The maximum number of socket connections.
// Default is 10 connections.
// Maximum number of socket connections.
// Default is 10 connections per every CPU as reported by runtime.NumCPU.
PoolSize int
// Specifies amount of time client waits for connection if all
// connections are busy before returning an error.
// Default is 1 second.
// 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 *Config) 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,
}
}
33 changes: 10 additions & 23 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,18 @@ import (
"encoding/json"
"time"

"github.com/go-redis/redis"
"github.com/satori/go.uuid"
"gopkg.in/redis.v5"

"gopkg.in/oauth2.v3"
"gopkg.in/oauth2.v3/models"
)

// NewTokenStore Create a token store instance based on redis
func NewTokenStore(cfg *Config) (ts oauth2.TokenStore, err error) {
opt := &redis.Options{
Network: cfg.Network,
Addr: cfg.Addr,
Password: cfg.Password,
DB: cfg.DB,
MaxRetries: cfg.MaxRetries,
DialTimeout: cfg.DialTimeout,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
PoolSize: cfg.PoolSize,
PoolTimeout: cfg.PoolTimeout,
if cfg == nil {
panic("config cannot be nil")
}
cli := redis.NewClient(opt)
cli := redis.NewClient(cfg.redisOptions())
if verr := cli.Ping().Err(); verr != nil {
err = verr
return
Expand All @@ -46,16 +36,12 @@ func (rs *TokenStore) Create(info oauth2.TokenInfo) (err error) {
if err != nil {
return
}

pipe := rs.cli.Pipeline()
if code := info.GetCode(); code != "" {
pipe.Set(code, jv, info.GetCodeExpiresIn())
} else {
basicID, uerr := uuid.NewV4()
if uerr != nil {
err = uerr
return
}
basicIDStr := basicID.String()
basicID := uuid.Must(uuid.NewV4()).String()
aexp := info.GetAccessExpiresIn()
rexp := aexp

Expand All @@ -64,10 +50,11 @@ func (rs *TokenStore) Create(info oauth2.TokenInfo) (err error) {
if aexp.Seconds() > rexp.Seconds() {
aexp = rexp
}
pipe.Set(refresh, basicIDStr, rexp)
pipe.Set(refresh, basicID, rexp)
}
pipe.Set(info.GetAccess(), basicIDStr, aexp)
pipe.Set(basicIDStr, jv, rexp)

pipe.Set(info.GetAccess(), basicID, aexp)
pipe.Set(basicID, jv, rexp)
}

if _, verr := pipe.Exec(); verr != nil {
Expand Down
2 changes: 1 addition & 1 deletion token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
"time"

"github.com/go-oauth2/redis"
"gopkg.in/go-oauth2/redis.v1"
"gopkg.in/oauth2.v3/models"

. "github.com/smartystreets/goconvey/convey"
Expand Down

0 comments on commit de39c2c

Please sign in to comment.