-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconn.go
430 lines (381 loc) · 11.9 KB
/
conn.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Package memcache provides a client for the memcached cache server.
package memcache
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"strconv"
"strings"
)
var (
// ErrCacheMiss means that a Get failed because the item wasn't present.
ErrCacheMiss = errors.New("memcache: cache miss")
// ErrCASConflict means that a CompareAndSwap call failed due to the
// cached value being modified between the Get and the CompareAndSwap.
// If the cached value was simply evicted rather than replaced,
// ErrNotStored will be returned instead.
ErrCASConflict = errors.New("memcache: compare-and-swap conflict")
// ErrNotStored means that a conditional write operation (i.e. Add or
// CompareAndSwap) failed because the condition was not satisfied.
ErrNotStored = errors.New("memcache: item not stored")
// ErrMalformedKey is returned when an invalid key is used.
// Keys must be at maximum 250 bytes long and not
// contain whitespace or control characters.
ErrMalformedKey = errors.New("malformed: key is too long or contains invalid characters")
)
var (
crlf = []byte("\r\n")
space = []byte(" ")
resultStored = []byte("STORED\r\n")
resultNotStored = []byte("NOT_STORED\r\n")
resultExists = []byte("EXISTS\r\n")
resultNotFound = []byte("NOT_FOUND\r\n")
resultDeleted = []byte("DELETED\r\n")
resultEnd = []byte("END\r\n")
resultOk = []byte("OK\r\n")
resultError = []byte("ERROR\r\n")
resultTouched = []byte("TOUCHED\r\n")
resultClientErrorPrefix = []byte("CLIENT_ERROR ")
resultServerErrorPrefix = []byte("SERVER_ERROR ")
)
// Conn is a memcache client.
// It is safe for unlocked use by multiple concurrent goroutines.
type Conn struct {
rw *bufio.ReadWriter
}
// NewConn create a new memcache connection.
func NewConn(c net.Conn) *Conn {
r := bufio.NewReader(c)
w := bufio.NewWriter(c)
rw := bufio.NewReadWriter(r, w)
return &Conn{rw}
}
// Item is an item to be got or stored in a memcached server.
type Item struct {
// Key is the Item's key (250 bytes maximum).
Key string
// Value is the Item's value.
Value []byte
// Flags are server-opaque flags whose semantics are entirely
// up to the app.
Flags uint32
// Expiration is the cache expiration time, in seconds: either a relative
// time from now (up to 1 month), or an absolute Unix epoch time.
// Zero means the Item has no expiration time.
Expiration int32
// Compare and swap ID.
casid uint64
}
// Get gets the item for the given key. ErrCacheMiss is returned for a
// memcache cache miss. The key must be at most 250 bytes in length.
func (c *Conn) Get(key string) (*Item, error) {
if _, err := fmt.Fprintf(c.rw, "get %s\r\n", key); err != nil {
return nil, err
}
if err := c.rw.Flush(); err != nil {
return nil, err
}
items, err := parseGetResponse(c.rw.Reader)
if err != nil {
return nil, err
}
it, ok := items[key]
if !ok {
return nil, ErrCacheMiss
}
return it, nil
}
// GetMulti is a batch version of Get. The returned map from keys to
// items may have fewer elements than the input slice, due to memcache
// cache misses. Each key must be at most 250 bytes in length.
// If no error is returned, the returned map will also be non-nil.
func (c *Conn) GetMulti(keys []string) (map[string]*Item, error) {
if _, err := fmt.Fprintf(c.rw, "gets %s\r\n", strings.Join(keys, " ")); err != nil {
return nil, err
}
if err := c.rw.Flush(); err != nil {
return nil, err
}
return parseGetResponse(c.rw.Reader)
}
func parseGetResponse(r *bufio.Reader) (map[string]*Item, error) {
items := make(map[string]*Item)
for {
line, err := r.ReadSlice('\n')
if err != nil {
return nil, err
}
if bytes.Equal(line, resultEnd) {
return items, err
}
it := new(Item)
size, err := scanGetResponseLine(line, it)
if err != nil {
return nil, err
}
it.Value, err = ioutil.ReadAll(io.LimitReader(r, int64(size)+2))
if err != nil {
return nil, err
}
if !bytes.HasSuffix(it.Value, crlf) {
return nil, fmt.Errorf("memcache: corrupt get result read")
}
it.Value = it.Value[:size]
items[it.Key] = it
}
}
// scanGetResponseLine populates it and returns the declared size of the item.
// It does not read the bytes of the item.
func scanGetResponseLine(line []byte, it *Item) (size int, err error) {
pattern := "VALUE %s %d %d %d\r\n"
dest := []interface{}{&it.Key, &it.Flags, &size, &it.casid}
if bytes.Count(line, space) == 3 {
pattern = "VALUE %s %d %d\r\n"
dest = dest[:3]
}
n, err := fmt.Sscanf(string(line), pattern, dest...)
if err != nil || n != len(dest) {
return -1, fmt.Errorf("memcache: unexpected line in get response: %q", line)
}
return size, nil
}
// Set writes the given item, unconditionally.
func (c *Conn) Set(item *Item) error {
return c.populateOne(c.rw, "set", item)
}
// Add writes the given item, if no value already exists for its
// key. ErrNotStored is returned if that condition is not met.
func (c *Conn) Add(item *Item) error {
return c.populateOne(c.rw, "add", item)
}
// Replace writes the given item, but only if the server *does*
// already hold data for this key
func (c *Conn) Replace(item *Item) error {
return c.populateOne(c.rw, "replace", item)
}
// CompareAndSwap writes the given item that was previously returned
// by Get, if the value was neither modified or evicted between the
// Get and the CompareAndSwap calls. The item's Key should not change
// between calls but all other item fields may differ. ErrCASConflict
// is returned if the value was modified in between the
// calls. ErrNotStored is returned if the value was evicted in between
// the calls.
func (c *Conn) CompareAndSwap(item *Item) error {
return c.populateOne(c.rw, "cas", item)
}
func (c *Conn) populateOne(rw *bufio.ReadWriter, verb string, item *Item) error {
if !legalKey(item.Key) {
return ErrMalformedKey
}
var err error
if verb == "cas" {
_, err = fmt.Fprintf(rw, "%s %s %d %d %d %d\r\n",
verb, item.Key, item.Flags, item.Expiration, len(item.Value), item.casid)
} else {
_, err = fmt.Fprintf(rw, "%s %s %d %d %d\r\n",
verb, item.Key, item.Flags, item.Expiration, len(item.Value))
}
if err != nil {
return err
}
if _, err = rw.Write(item.Value); err != nil {
return err
}
if _, err := rw.Write(crlf); err != nil {
return err
}
if err := rw.Flush(); err != nil {
return err
}
line, err := rw.ReadSlice('\n')
if err != nil {
return err
}
switch {
case bytes.Equal(line, resultStored):
return nil
case bytes.Equal(line, resultNotStored):
return ErrNotStored
case bytes.Equal(line, resultExists):
return ErrCASConflict
case bytes.Equal(line, resultNotFound):
return ErrCacheMiss
}
return fmt.Errorf("memcache: unexpected response line from %q: %q", verb, string(line))
}
func writeReadLine(rw *bufio.ReadWriter, format string, args ...interface{}) ([]byte, error) {
_, err := fmt.Fprintf(rw, format, args...)
if err != nil {
return nil, err
}
if err := rw.Flush(); err != nil {
return nil, err
}
line, err := rw.ReadSlice('\n')
return line, err
}
func writeExpectf(rw *bufio.ReadWriter, expect []byte, format string, args ...interface{}) error {
line, err := writeReadLine(rw, format, args...)
if err != nil {
return err
}
switch {
case bytes.Equal(line, resultOk):
return nil
case bytes.Equal(line, expect):
return nil
case bytes.Equal(line, resultNotStored):
return ErrNotStored
case bytes.Equal(line, resultExists):
return ErrCASConflict
case bytes.Equal(line, resultNotFound):
return ErrCacheMiss
}
return fmt.Errorf("memcache: unexpected response line: %q", string(line))
}
// Delete deletes the item with the provided key. The error ErrCacheMiss is
// returned if the item didn't already exist in the cache.
func (c *Conn) Delete(key string) error {
return writeExpectf(c.rw, resultDeleted, "delete %s\r\n", key)
}
// Increment atomically increments key by delta. The return value is
// the new value after being incremented or an error. If the value
// didn't exist in memcached the error is ErrCacheMiss. The value in
// memcached must be an decimal number, or an error will be returned.
// On 64-bit overflow, the new value wraps around.
func (c *Conn) Increment(key string, delta uint64) (newValue uint64, err error) {
return c.incrDecr("incr", key, delta)
}
// Decrement atomically decrements key by delta. The return value is
// the new value after being decremented or an error. If the value
// didn't exist in memcached the error is ErrCacheMiss. The value in
// memcached must be an decimal number, or an error will be returned.
// On underflow, the new value is capped at zero and does not wrap
// around.
func (c *Conn) Decrement(key string, delta uint64) (newValue uint64, err error) {
return c.incrDecr("decr", key, delta)
}
func (c *Conn) incrDecr(verb, key string, delta uint64) (uint64, error) {
var val uint64
line, err := writeReadLine(c.rw, "%s %s %d\r\n", verb, key, delta)
if err != nil {
return val, err
}
switch {
case bytes.Equal(line, resultNotFound):
return val, ErrCacheMiss
case bytes.HasPrefix(line, resultClientErrorPrefix):
errMsg := line[len(resultClientErrorPrefix) : len(line)-2]
return val, errors.New("memcache: client error: " + string(errMsg))
}
val, err = strconv.ParseUint(string(line[:len(line)-2]), 10, 64)
return val, err
}
// Touch updates the expiry for the given key. The seconds parameter is either
// a Unix timestamp or, if seconds is less than 1 month, the number of seconds
// into the future at which time the item will expire. ErrCacheMiss is returned if the
// key is not in the cache. The key must be at most 250 bytes in length.
func (c *Conn) Touch(key string, seconds int32) (err error) {
return writeExpectf(c.rw, resultTouched, "touch %s %d\r\n", key, seconds)
}
// FlushAll clear all item
func (c *Conn) FlushAll() error {
return writeExpectf(c.rw, resultOk, "flush_all\r\n")
}
func (c *Conn) ping() error {
return writeExpectf(c.rw, resultError, "ping\r\n")
}
// IsResumableErr returns true if err is only a protocol-level cache error.
// This is used to determine whether or not a server connection should
// be re-used or not. If an error occurs, by default we don't reuse the
// connection, unless it was just a cache error.
func IsResumableErr(err error) bool {
switch err {
case ErrCacheMiss, ErrCASConflict, ErrNotStored, ErrMalformedKey:
return true
case nil:
return true
}
return false
}
func (c *Conn) metaCmd(cmd, key string, flags []metaFlag, data []byte) (mr MetaResult, err error) {
if !legalKey(key) {
err = ErrMalformedKey
return
}
withPayload := data != nil
if withPayload {
_, err = fmt.Fprintf(c.rw, "%s %s %d %s\r\n", cmd, key, len(data), buildMetaFlags(flags))
} else {
_, err = fmt.Fprintf(c.rw, "%s %s %s\r\n", cmd, key, buildMetaFlags(flags))
}
if err != nil {
return
}
if withPayload {
if _, err = c.rw.Write(data); err != nil {
return
}
if _, err = c.rw.Write(crlf); err != nil {
return
}
}
if err = c.rw.Flush(); err != nil {
return
}
mr, err = parseMetaResponse(c.rw.Reader)
return
}
func legalKey(key string) bool {
if l := len(key); l > 250 || l == 0 {
return false
}
for i := 0; i < len(key); i++ {
if key[i] <= ' ' || key[i] == 0x7f {
return false
}
}
return true
}
func parseMetaResponse(r *bufio.Reader) (mr MetaResult, err error) {
statusLineRaw, err := r.ReadSlice('\n')
if err != nil {
return
}
status := strings.Fields(string(statusLineRaw))
code, size, withValue, status := status[0], 0, false, status[1:]
switch code {
case "MN":
mr.isNoOp = true
case "VA":
size, err = strconv.Atoi(status[0])
status, withValue = status[1:], true
case "NS":
err = ErrNotStored
case "EX":
err = ErrCASConflict
case "EN", "NF":
err = ErrCacheMiss
case "HD":
default:
err = fmt.Errorf("memcache: unexpected line in response: %q", statusLineRaw)
}
if mr.isNoOp || err != nil {
return
}
if mr, err = obtainMetaFlagsResults(status); err != nil {
return
}
if withValue {
mr.Value = make([]byte, size+len(crlf))
if _, err = io.ReadFull(r, mr.Value); err != nil {
return
}
mr.Value = mr.Value[:size]
}
return
}