-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
394 lines (348 loc) · 8.85 KB
/
client.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package redjet
import (
"bufio"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/url"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
type Client struct {
// ConnectionPoolSize limits the size of the connection pool. If 0, connections
// are not pooled.
ConnectionPoolSize int
// IdleTimeout is the amount of time after which an idle connection will
// be closed.
IdleTimeout time.Duration
// Dial is the function used to create new connections.
Dial func(ctx context.Context) (net.Conn, error)
// Setup is called after a new connection is established, but before any
// commands are sent. It is useful for selecting a database or authenticating.
//
// See SetupAuth for authenticating with a username and password.
Setup func(ctx context.Context, client *Client, pipe *Pipeline) error
poolMu sync.Mutex
pool *connPool
}
// New returns a new client that connects to addr with default settings.
func New(addr string) *Client {
c := &Client{
ConnectionPoolSize: 8,
IdleTimeout: 5 * time.Minute,
Dial: func(ctx context.Context) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, "tcp", addr)
},
}
return c
}
func NewFromURL(rawURL string) (*Client, error) {
u, err := url.Parse(rawURL)
if err != nil {
return nil, fmt.Errorf("parse url: %w", err)
}
client := New(u.Host)
var (
addr string
isUnixSocket bool
)
if u.Host == "" && u.Path != "" {
// Likely using a unix socket.
addr = u.Path
isUnixSocket = true
} else {
addr = u.Host
}
if !isUnixSocket && u.Port() == "" {
addr = net.JoinHostPort(addr, "6379")
}
switch u.Scheme {
case "redis":
client.Dial = func(ctx context.Context) (net.Conn, error) {
var d net.Dialer
proto := "tcp"
if isUnixSocket {
proto = "unix"
}
return d.DialContext(ctx, proto, addr)
}
case "rediss":
client.Dial = func(ctx context.Context) (net.Conn, error) {
var d tls.Dialer
return d.DialContext(ctx, "tcp", addr)
}
default:
return nil, fmt.Errorf("unsupported scheme: %s", u.Scheme)
}
if u.User != nil {
pass, _ := u.User.Password()
client.Setup = SetupAuth(u.User.Username(), pass)
}
return client, nil
}
func (c *Client) initPool() {
c.poolMu.Lock()
defer c.poolMu.Unlock()
if c.pool == nil {
c.pool = newConnPool(c.ConnectionPoolSize, c.IdleTimeout)
}
}
type PoolStats struct {
FreeConns int
Returns int64
FullPoolCloses int64
CleanCycles int64
}
// PoolStats returns statistics about the connection pool.
func (c *Client) PoolStats() PoolStats {
if c.pool == nil {
return PoolStats{}
}
return PoolStats{
FreeConns: len(c.pool.free),
Returns: atomic.LoadInt64(&c.pool.returns),
FullPoolCloses: atomic.LoadInt64(&c.pool.fullPoolCloses),
CleanCycles: atomic.LoadInt64(&c.pool.cleanCycles),
}
}
// getConn gets a new conn, wrapped in a Pipeline. The conn is already authenticated.
func (c *Client) getConn(ctx context.Context) (*Pipeline, error) {
c.initPool()
if conn, ok := c.pool.tryGet(); ok {
return c.newResult(conn), nil
}
nc, err := c.Dial(ctx)
if err != nil {
return nil, fmt.Errorf("dial: %w", err)
}
conn := &conn{
Conn: nc,
lastUsed: time.Now(),
wr: bufio.NewWriterSize(nc, 32*1024),
rd: bufio.NewReaderSize(nc, 32*1024),
miscBuf: make([]byte, 32*1024),
}
r := c.newResult(conn)
if c.Setup != nil {
err = c.Setup(ctx, c, r)
if err != nil {
nc.Close()
return nil, fmt.Errorf("setup: %w", err)
}
}
return r, nil
}
// SetupAuth returns a Setup function that authenticates with the given username and password.
//
// AuthUsername is the username used for authentication.
//
// If set, AuthPassword must also be set. If not using Redis ACLs, just
// set AuthPassword.
//
// See more: https://redis.io/commands/auth/
// AuthPassword is the password used for authentication.
// Authentication must be set before any other commands are sent, and
// must not change during the lifetime of the client.
//
// See more: https://redis.io/commands/auth/
func SetupAuth(
username string,
password string,
) func(ctx context.Context, client *Client, pipe *Pipeline) error {
return func(ctx context.Context, client *Client, pipe *Pipeline) error {
switch {
case username != "" && password != "":
pipe = client.Pipeline(ctx, pipe, "AUTH", username, password)
case password != "":
pipe = client.Pipeline(ctx, pipe, "AUTH", password)
default:
return fmt.Errorf("username is set but password is not")
}
return pipe.Ok()
}
}
func (c *Client) putConn(conn *conn) {
if conn == nil {
panic("cannot put nil conn")
}
c.initPool()
// Clear any deadline.
conn.SetDeadline(time.Time{})
c.pool.put(conn)
}
var crlf = []byte("\r\n")
func writeBulkString(w *bufio.Writer, s string) {
w.WriteString("$")
w.WriteString(strconv.Itoa(len(s)))
w.Write(crlf)
w.WriteString(s)
w.Write(crlf)
}
func writeBulkBytes(w *bufio.Writer, b []byte) {
w.WriteString("$")
w.WriteString(strconv.Itoa(len(b)))
w.Write(crlf)
w.Write(b)
w.Write(crlf)
}
func writeBulkReader(w *bufio.Writer, rd LenReader) {
w.WriteString("$")
w.WriteString(strconv.Itoa(rd.Len()))
w.Write(crlf)
io.CopyN(w, rd, int64(rd.Len()))
w.Write(crlf)
}
// LenReader is an io.Reader that also knows its length.
// A new one may be created with NewLenReader.
type LenReader interface {
Len() int
io.Reader
}
type lenReader struct {
io.Reader
size int
}
func (r *lenReader) Len() int {
return r.size
}
func NewLenReader(r io.Reader, size int) LenReader {
return &lenReader{
Reader: r,
size: size,
}
}
func (c *Client) newResult(conn *conn) *Pipeline {
return &Pipeline{
closeCh: make(chan struct{}),
conn: conn,
client: c,
}
}
// Pipeline sends a command to the server and returns the promise of a result.
// r may be nil, as in the case of the first command in a pipeline. Each successive
// call to Pipeline should re-use the last returned Pipeline.
//
// Known arg types are strings, []byte, LenReader, and fmt.Stringer. All other types
// will be converted to JSON.
//
// It is safe to keep a pipeline running for a long time, with many send and
// receive cycles.
//
// Example:
//
// p := client.Pipeline(ctx, nil, "SET", "foo", "bar")
// defer p.Close()
//
// p = client.Pipeline(ctx, r, "GET", "foo")
// // Read the result of SET first.
// err := p.Ok()
// if err != nil {
// // handle error
// }
//
// got, err := p.Bytes()
// if err != nil {
// // handle error
// }
// fmt.Println(string(got))
func (c *Client) Pipeline(ctx context.Context, p *Pipeline, cmd string, args ...any) *Pipeline {
var err error
if p == nil {
p, err = c.getConn(ctx)
if err != nil {
return &Pipeline{
protoErr: fmt.Errorf("get conn: %w", err),
}
}
// We must take great care that Close is eventually called on the pipeline to
// avoid leaking connections.
runtime.SetFinalizer(p, func(p *Pipeline) {
p.Close()
})
go func() {
select {
case <-ctx.Done():
p.Close()
case <-p.closeCh:
}
}()
}
cmd = strings.ToUpper(cmd)
// Redis already gives a nice error if we send a non-subscribe command
// while in subscribe mode.
if isSubscribeCmd(cmd) {
p.subscribeMode = true
}
// We're instructing redis that we're sending an array of the command
// and its arguments.
p.conn.wr.WriteByte('*')
p.conn.wr.WriteString(strconv.Itoa(len(args) + 1))
p.conn.wr.Write(crlf)
writeBulkString(p.conn.wr, cmd)
for _, arg := range args {
switch arg := arg.(type) {
case string:
writeBulkString(p.conn.wr, arg)
case []byte:
writeBulkBytes(p.conn.wr, arg)
case LenReader:
writeBulkReader(p.conn.wr, arg)
case fmt.Stringer:
writeBulkString(p.conn.wr, arg.String())
default:
v, err := json.Marshal(arg)
if err != nil {
// It's relatively rare to get an error here.
panic(fmt.Sprintf("failed to marshal %T: %v", arg, err))
}
writeBulkBytes(p.conn.wr, v)
}
}
p.pipeline.end++
return p
}
// Command sends a command to the server and returns the result. The error
// is encoded into the result for ergonomics.
//
// See Pipeline for more information on argument types.
//
// The caller should call Close on the result when finished with it.
func (c *Client) Command(ctx context.Context, cmd string, args ...any) *Pipeline {
if isSubscribeCmd(cmd) {
return &Pipeline{
// Close behavior becomes confusing when combining subscription
// and CloseOnRead.
protoErr: fmt.Errorf("cannot use Command with subscribe command %s, use Pipeline instead", cmd),
}
}
r := c.Pipeline(ctx, nil, cmd, args...)
r.CloseOnRead = true
return r
}
func (c *Client) Close() error {
c.poolMu.Lock()
defer c.poolMu.Unlock()
var merr error
if c.pool != nil {
close(c.pool.cancelClean)
<-c.pool.cleanExited
// The cleaner may read free until it exits.
close(c.pool.free)
for conn := range c.pool.free {
err := conn.Close()
merr = errors.Join(merr, err)
}
c.pool.cleanTicker.Stop()
c.pool = nil
}
return merr
}