forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
179 lines (152 loc) · 3.51 KB
/
redis.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
package redis
import (
"crypto/tls"
"log"
"net"
"os"
"sync"
)
// Package logger.
var Logger = log.New(os.Stdout, "redis: ", log.Ldate|log.Ltime)
type OpenConnFunc func() (net.Conn, error)
type CloseConnFunc func(net.Conn) error
type InitConnFunc func(*Client) error
func TCPConnector(addr string) OpenConnFunc {
return func() (net.Conn, error) {
return net.Dial("tcp", addr)
}
}
func TLSConnector(addr string, tlsConfig *tls.Config) OpenConnFunc {
return func() (net.Conn, error) {
return tls.Dial("tcp", addr, tlsConfig)
}
}
func AuthSelectFunc(password string, db int64) InitConnFunc {
if password == "" && db < 0 {
return nil
}
return func(client *Client) error {
if password != "" {
auth := client.Auth(password)
if auth.Err() != nil {
return auth.Err()
}
}
if db >= 0 {
sel := client.Select(db)
if sel.Err() != nil {
return sel.Err()
}
}
return nil
}
}
//------------------------------------------------------------------------------
type BaseClient struct {
ConnPool ConnPool
InitConn InitConnFunc
reqs []Req
reqsMtx sync.Mutex
}
func (c *BaseClient) WriteReq(conn *Conn, reqs ...Req) error {
buf := make([]byte, 0, 1000)
for _, req := range reqs {
buf = appendReq(buf, req.Args())
}
_, err := conn.RW.Write(buf)
return err
}
func (c *BaseClient) conn() (*Conn, error) {
conn, isNew, err := c.ConnPool.Get()
if err != nil {
return nil, err
}
if isNew && c.InitConn != nil {
client := &Client{
BaseClient: &BaseClient{
ConnPool: NewSingleConnPoolConn(c.ConnPool, conn, true),
},
}
err = c.InitConn(client)
if err != nil {
if err := c.ConnPool.Remove(conn); err != nil {
Logger.Printf("ConnPool.Remove error: %v", err)
}
return nil, err
}
}
return conn, nil
}
func (c *BaseClient) Process(req Req) {
if c.reqs == nil {
c.Run(req)
} else {
c.Queue(req)
}
}
func (c *BaseClient) Run(req Req) {
conn, err := c.conn()
if err != nil {
req.SetErr(err)
return
}
err = c.WriteReq(conn, req)
if err != nil {
if err := c.ConnPool.Remove(conn); err != nil {
Logger.Printf("ConnPool.Remove error: %v", err)
}
req.SetErr(err)
return
}
val, err := req.ParseReply(conn.Rd)
if err != nil {
if _, ok := err.(*parserError); ok {
if err := c.ConnPool.Remove(conn); err != nil {
Logger.Printf("ConnPool.Remove error: %v", err)
}
} else {
if err := c.ConnPool.Add(conn); err != nil {
Logger.Printf("ConnPool.Add error: %v", err)
}
}
req.SetErr(err)
return
}
if err := c.ConnPool.Add(conn); err != nil {
Logger.Printf("ConnPool.Add error: %v", err)
}
req.SetVal(val)
}
// Queues request to be executed later.
func (c *BaseClient) Queue(req Req) {
c.reqsMtx.Lock()
c.reqs = append(c.reqs, req)
c.reqsMtx.Unlock()
}
func (c *BaseClient) Close() error {
return c.ConnPool.Close()
}
//------------------------------------------------------------------------------
type Client struct {
*BaseClient
TryEvalShaMinLen int
}
func NewClient(openConn OpenConnFunc, closeConn CloseConnFunc, initConn InitConnFunc) *Client {
return &Client{
BaseClient: &BaseClient{
ConnPool: NewMultiConnPool(openConn, closeConn, 10),
InitConn: initConn,
},
TryEvalShaMinLen: 80,
}
}
func NewTCPClient(addr string, password string, db int64) *Client {
return NewClient(TCPConnector(addr), nil, AuthSelectFunc(password, db))
}
func NewTLSClient(addr string, tlsConfig *tls.Config, password string, db int64) *Client {
return NewClient(
TLSConnector(addr, tlsConfig),
nil,
AuthSelectFunc(password, db),
)
}