-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
181 lines (154 loc) · 4.41 KB
/
connection.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package shardis
import (
"errors"
"fmt"
"strconv"
"time"
"github.com/garyburd/redigo/redis"
)
// Connection represents the connection to a single Redis instance
type Connection struct {
Host string
Pool *redis.Pool
BlockTimeout int
}
// NewConnection creats a new Connection instance
func NewConnection(host string, password string, blockTimeout int) *Connection {
pool := makeRedisPool(host, password)
return &Connection{
Host: host,
Pool: pool,
BlockTimeout: blockTimeout,
}
}
// makeRedisPool creates new redis.Pool instance
// given Redis server address and password
func makeRedisPool(host string, password string) *redis.Pool {
pool := &redis.Pool{
MaxIdle: 5,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", host)
if err != nil {
return c, nil
}
c.Do("AUTH", password)
/* the is needed only if "gores" is configured in Redis's configuration file redis.conf */
//c.Do("SELECT", "gores")
return c, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
return pool
}
/* Redis methods for single connection*/
// Set for calling Redis SET command on a single instance
func (conn *Connection) Set(key string, value interface{}) error {
cn := conn.Pool.Get()
if cn == nil {
return errors.New("error getting pool connection")
}
_, err := cn.Do("SET", key, value)
if err != nil {
return fmt.Errorf("error SET (%s) on host: %s", err, conn.Host)
}
return nil
}
// Get for calling Redis SET command on a single instance
func (conn *Connection) Get(key string) (interface{}, error) {
cn := conn.Pool.Get()
if cn == nil {
return nil, errors.New("error getting pool connection")
}
value, err := cn.Do("GET", key)
if value == nil || err != nil {
return nil, fmt.Errorf("error GET (%s) on host: %s", err, conn.Host)
}
value = convertType(value, err)
return value, nil
}
// Lpush for calling Redis SET command on a single instance
func (conn *Connection) Lpush(key string, value interface{}) error {
cn := conn.Pool.Get()
if cn == nil {
return errors.New("error getting pool connection")
}
_, err := cn.Do("LPUSH", key, value)
if err != nil {
return fmt.Errorf("error LPUSH (%s) on host: %s", err, conn.Host)
}
return nil
}
// Rpush for calling Redis SET command on a single instance
func (conn *Connection) Rpush(key string, value interface{}) error {
cn := conn.Pool.Get()
if cn == nil {
return errors.New("error getting pool connection")
}
/*
args := make([]interface{}, len(values) + 1)
args[0] = key
for i:=1; i < len(values)+1; i++ {
args[i] = values[i-1]
}
*/
_, err := cn.Do("RPUSH", key, value)
if err != nil {
return fmt.Errorf("error RPUSH (%s) on host: %s", err, conn.Host)
}
return nil
}
// Lpop for calling Redis SET command on a single instance
func (conn *Connection) Lpop(key string) (interface{}, error) {
cn := conn.Pool.Get()
if cn == nil {
return nil, errors.New("error getting pool connection")
}
value, err := cn.Do("LPOP", key)
if value == nil || err != nil {
return nil, fmt.Errorf("error LPOP (%s) on host: %s", err, conn.Host)
}
value = convertType(value, err)
return value, nil
}
// Rpop for calling Redis SET command on a single instance
func (conn *Connection) Rpop(key string) (interface{}, error) {
cn := conn.Pool.Get()
if cn == nil {
return nil, errors.New("error getting pool connection")
}
value, err := cn.Do("RPOP", key)
if value == nil || err != nil {
return nil, fmt.Errorf("error RPOP (%s) on host: %s", err, conn.Host)
}
value = convertType(value, err)
return value, nil
}
// Blpop for calling Redis SET command on a single instance
func (conn *Connection) Blpop(keys ...string) (interface{}, error) {
cn := conn.Pool.Get()
if cn == nil {
return nil, errors.New("error getting pool connection")
}
queueSlice := make([]interface{}, len(keys))
for i := 0; i < len(keys); i++ {
queueSlice[i] = keys[i]
}
args := append(queueSlice, conn.BlockTimeout)
value, err := cn.Do("BLPOP", args...)
if value == nil || err != nil {
return nil, err
}
return convertType(value, err), nil
}
// Convert replied data([]byte) to correct type(int or string)
func convertType(value interface{}, err error) interface{} {
stringValue, err := redis.String(value.([]byte), err)
if intValue, err := strconv.Atoi(stringValue); err == nil {
return intValue
}
return stringValue
}