-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcluster.go
74 lines (67 loc) · 1.75 KB
/
cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package goredis
import (
"time"
"github.com/gomodule/redigo/redis"
"github.com/mna/redisc"
)
type ClusterClient struct {
cli *redisc.Cluster
dbName string
option *Option
}
func NewClusterClient(dbName string, addrs []string, option *Option) (*ClusterClient, error) {
cli := &ClusterClient{}
err := cli.Init(dbName, addrs, option)
return cli, err
}
func (this *ClusterClient) Init(dbName string, addrs []string, option *Option) error {
this.dbName = dbName
this.option = option
this.cli = &redisc.Cluster{
StartupNodes: addrs,
DialOptions: []redis.DialOption{redis.DialConnectTimeout(5 * time.Second)},
CreatePool: this.createPool,
}
if err := this.cli.Refresh(); err != nil {
return err
}
return nil
}
func (this *ClusterClient) createPool(addr string, options ...redis.DialOption) (*redis.Pool, error) {
return &redis.Pool{
MaxIdle: this.option.PoolMaxIdle,
MaxActive: this.option.PoolMaxActive,
Wait: this.option.PoolWait,
IdleTimeout: this.option.PoolIdleTimeout,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", addr, options...)
if err != nil {
return nil, err
}
if this.option.Password != "" {
if _, err := c.Do("AUTH", this.option.Password); err != nil {
c.Close()
return nil, err
}
}
if _, err := c.Do("SELECT", this.option.DBIndex); err != nil {
c.Close()
return nil, err
}
return c, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}, nil
}
func (this *ClusterClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
conn := this.cli.Get()
if conn.Err() == nil {
defer conn.Close()
return conn.Do(commandName, args...)
} else {
return nil, conn.Err()
}
}