Skip to content

Commit de39c2c

Browse files
committed
update redis client
1 parent 552c26c commit de39c2c

File tree

4 files changed

+78
-42
lines changed

4 files changed

+78
-42
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
## Install
1010

1111
``` bash
12-
$ go get -u github.com/go-oauth2/redis
12+
$ go get -u -v gopkg.in/go-oauth2/redis.v1
1313
```
1414

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

2020
import (
21-
"github.com/go-oauth2/redis"
21+
"gopkg.in/go-oauth2/redis.v1"
2222
"gopkg.in/oauth2.v3/manage"
2323
)
2424

config.go

+65-16
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,90 @@
11
package redis
22

3-
import "time"
3+
import (
4+
"crypto/tls"
5+
"net"
6+
"time"
47

5-
// Config Redis Configuration
8+
"github.com/go-redis/redis"
9+
)
10+
11+
// Config Redis parameter options
612
type Config struct {
713
// The network type, either tcp or unix.
814
// Default is tcp.
915
Network string
1016
// host:port address.
1117
Addr string
1218

13-
// An optional password. Must match the password specified in the
19+
// Dialer creates new network connection and has priority over
20+
// Network and Addr options.
21+
Dialer func() (net.Conn, error)
22+
23+
// Optional password. Must match the password specified in the
1424
// requirepass server configuration option.
1525
Password string
16-
// A database to be selected after connecting to server.
26+
// Database to be selected after connecting to the server.
1727
DB int
1828

19-
// The maximum number of retries before giving up.
29+
// Maximum number of retries before giving up.
2030
// Default is to not retry failed commands.
2131
MaxRetries int
32+
// Minimum backoff between each retry.
33+
// Default is 8 milliseconds; -1 disables backoff.
34+
MinRetryBackoff time.Duration
35+
// Maximum backoff between each retry.
36+
// Default is 512 milliseconds; -1 disables backoff.
37+
MaxRetryBackoff time.Duration
2238

23-
// Sets the deadline for establishing new connections. If reached,
24-
// dial will fail with a timeout.
39+
// Dial timeout for establishing new connections.
2540
// Default is 5 seconds.
2641
DialTimeout time.Duration
27-
// Sets the deadline for socket reads. If reached, commands will
28-
// fail with a timeout instead of blocking.
42+
// Timeout for socket reads. If reached, commands will fail
43+
// with a timeout instead of blocking.
44+
// Default is 3 seconds.
2945
ReadTimeout time.Duration
30-
// Sets the deadline for socket writes. If reached, commands will
31-
// fail with a timeout instead of blocking.
46+
// Timeout for socket writes. If reached, commands will fail
47+
// with a timeout instead of blocking.
48+
// Default is ReadTimeout.
3249
WriteTimeout time.Duration
3350

34-
// The maximum number of socket connections.
35-
// Default is 10 connections.
51+
// Maximum number of socket connections.
52+
// Default is 10 connections per every CPU as reported by runtime.NumCPU.
3653
PoolSize int
37-
// Specifies amount of time client waits for connection if all
38-
// connections are busy before returning an error.
39-
// Default is 1 second.
54+
// Amount of time client waits for connection if all connections
55+
// are busy before returning an error.
56+
// Default is ReadTimeout + 1 second.
4057
PoolTimeout time.Duration
58+
// Amount of time after which client closes idle connections.
59+
// Should be less than server's timeout.
60+
// Default is 5 minutes.
61+
IdleTimeout time.Duration
62+
// Frequency of idle checks.
63+
// Default is 1 minute.
64+
// When minus value is set, then idle check is disabled.
65+
IdleCheckFrequency time.Duration
66+
67+
// TLS Config to use. When set TLS will be negotiated.
68+
TLSConfig *tls.Config
69+
}
70+
71+
func (o *Config) redisOptions() *redis.Options {
72+
return &redis.Options{
73+
Network: o.Network,
74+
Addr: o.Addr,
75+
Dialer: o.Dialer,
76+
Password: o.Password,
77+
DB: o.DB,
78+
MaxRetries: o.MaxRetries,
79+
MinRetryBackoff: o.MinRetryBackoff,
80+
MaxRetryBackoff: o.MaxRetryBackoff,
81+
DialTimeout: o.DialTimeout,
82+
ReadTimeout: o.ReadTimeout,
83+
WriteTimeout: o.WriteTimeout,
84+
PoolSize: o.PoolSize,
85+
PoolTimeout: o.PoolTimeout,
86+
IdleTimeout: o.IdleTimeout,
87+
IdleCheckFrequency: o.IdleCheckFrequency,
88+
TLSConfig: o.TLSConfig,
89+
}
4190
}

token.go

+10-23
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,18 @@ import (
44
"encoding/json"
55
"time"
66

7+
"github.com/go-redis/redis"
78
"github.com/satori/go.uuid"
8-
"gopkg.in/redis.v5"
9-
109
"gopkg.in/oauth2.v3"
1110
"gopkg.in/oauth2.v3/models"
1211
)
1312

1413
// NewTokenStore Create a token store instance based on redis
1514
func NewTokenStore(cfg *Config) (ts oauth2.TokenStore, err error) {
16-
opt := &redis.Options{
17-
Network: cfg.Network,
18-
Addr: cfg.Addr,
19-
Password: cfg.Password,
20-
DB: cfg.DB,
21-
MaxRetries: cfg.MaxRetries,
22-
DialTimeout: cfg.DialTimeout,
23-
ReadTimeout: cfg.ReadTimeout,
24-
WriteTimeout: cfg.WriteTimeout,
25-
PoolSize: cfg.PoolSize,
26-
PoolTimeout: cfg.PoolTimeout,
15+
if cfg == nil {
16+
panic("config cannot be nil")
2717
}
28-
cli := redis.NewClient(opt)
18+
cli := redis.NewClient(cfg.redisOptions())
2919
if verr := cli.Ping().Err(); verr != nil {
3020
err = verr
3121
return
@@ -46,16 +36,12 @@ func (rs *TokenStore) Create(info oauth2.TokenInfo) (err error) {
4636
if err != nil {
4737
return
4838
}
39+
4940
pipe := rs.cli.Pipeline()
5041
if code := info.GetCode(); code != "" {
5142
pipe.Set(code, jv, info.GetCodeExpiresIn())
5243
} else {
53-
basicID, uerr := uuid.NewV4()
54-
if uerr != nil {
55-
err = uerr
56-
return
57-
}
58-
basicIDStr := basicID.String()
44+
basicID := uuid.Must(uuid.NewV4()).String()
5945
aexp := info.GetAccessExpiresIn()
6046
rexp := aexp
6147

@@ -64,10 +50,11 @@ func (rs *TokenStore) Create(info oauth2.TokenInfo) (err error) {
6450
if aexp.Seconds() > rexp.Seconds() {
6551
aexp = rexp
6652
}
67-
pipe.Set(refresh, basicIDStr, rexp)
53+
pipe.Set(refresh, basicID, rexp)
6854
}
69-
pipe.Set(info.GetAccess(), basicIDStr, aexp)
70-
pipe.Set(basicIDStr, jv, rexp)
55+
56+
pipe.Set(info.GetAccess(), basicID, aexp)
57+
pipe.Set(basicID, jv, rexp)
7158
}
7259

7360
if _, verr := pipe.Exec(); verr != nil {

token_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55
"time"
66

7-
"github.com/go-oauth2/redis"
7+
"gopkg.in/go-oauth2/redis.v1"
88
"gopkg.in/oauth2.v3/models"
99

1010
. "github.com/smartystreets/goconvey/convey"

0 commit comments

Comments
 (0)