-
Notifications
You must be signed in to change notification settings - Fork 29
/
memcache.go
789 lines (719 loc) · 19 KB
/
memcache.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package memcache provides a client for the memcached cache server.
package memcache
import (
"bytes"
"encoding/binary"
"errors"
"io"
"io/ioutil"
"net"
"runtime"
"sync"
"time"
)
// Similar to:
// http://code.google.com/appengine/docs/go/memcache/reference.html
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")
// ErrServer means that a server error occurred.
ErrServerError = errors.New("memcache: server error")
// ErrNoStats means that no statistics were available.
ErrNoStats = errors.New("memcache: no statistics available")
// ErrMalformedKey is returned when an invalid key is used.
// Keys must be at maximum 250 bytes long, ASCII, and not
// contain whitespace or control characters.
ErrMalformedKey = errors.New("malformed: key is too long or contains invalid characters")
// ErrNoServers is returned when no servers are configured or available.
ErrNoServers = errors.New("memcache: no servers configured or available")
// ErrBadMagic is returned when the magic number in a response is not valid.
ErrBadMagic = errors.New("memcache: bad magic number in response")
// ErrBadIncrDec is returned when performing a incr/decr on non-numeric values.
ErrBadIncrDec = errors.New("memcache: incr or decr on non-numeric value")
putUint16 = binary.BigEndian.PutUint16
putUint32 = binary.BigEndian.PutUint32
putUint64 = binary.BigEndian.PutUint64
bUint16 = binary.BigEndian.Uint16
bUint32 = binary.BigEndian.Uint32
bUint64 = binary.BigEndian.Uint64
)
// DefaultTimeout is the default socket read/write timeout.
const DefaultTimeout = time.Duration(100) * time.Millisecond
const (
buffered = 8 // arbitrary buffered channel size, for readability
maxIdleConnsPerAddr = 2 // TODO(bradfitz): make this configurable?
)
type command uint8
const (
cmdGet = iota
cmdSet
cmdAdd
cmdReplace
cmdDelete
cmdIncr
cmdDecr
cmdQuit
cmdFlush
cmdGetQ
cmdNoop
cmdVersion
cmdGetK
cmdGetKQ
cmdAppend
cmdPrepend
cmdStat
cmdSetQ
cmdAddQ
cmdReplaceQ
cmdDeleteQ
cmdIncrementQ
cmdDecrementQ
cmdQuitQ
cmdFlushQ
cmdAppendQ
cmdPrependQ
)
type response uint16
const (
respOk = iota
respKeyNotFound
respKeyExists
respValueTooLarge
respInvalidArgs
respItemNotStored
respInvalidIncrDecr
respWrongVBucket
respAuthErr
respAuthContinue
respUnknownCmd = 0x81
respOOM = 0x82
respNotSupported = 0x83
respInternalErr = 0x85
respBusy = 0x85
respTemporaryErr = 0x86
)
func (r response) asError() error {
switch r {
case respKeyNotFound:
return ErrCacheMiss
case respKeyExists:
return ErrNotStored
case respInvalidIncrDecr:
return ErrBadIncrDec
case respItemNotStored:
return ErrNotStored
}
return r
}
func (r response) Error() string {
switch r {
case respOk:
return "Ok"
case respKeyNotFound:
return "key not found"
case respKeyExists:
return "key already exists"
case respValueTooLarge:
return "value too large"
case respInvalidArgs:
return "invalid arguments"
case respItemNotStored:
return "item not stored"
case respInvalidIncrDecr:
return "incr/decr on non-numeric value"
case respWrongVBucket:
return "wrong vbucket"
case respAuthErr:
return "auth error"
case respAuthContinue:
return "auth continue"
}
return ""
}
const (
reqMagic uint8 = 0x80
respMagic uint8 = 0x81
)
func legalKey(key string) bool {
if len(key) > 250 {
return false
}
for i := 0; i < len(key); i++ {
if key[i] <= ' ' || key[i] > 0x7e {
return false
}
}
return true
}
func poolSize() int {
s := 8
if mp := runtime.GOMAXPROCS(0); mp > s {
s = mp
}
return s
}
// New returns a memcache client using the provided server(s)
// with equal weight. If a server is listed multiple times,
// it gets a proportional amount of weight.
func New(server ...string) (*Client, error) {
servers, err := NewServerList(server...)
if err != nil {
return nil, err
}
return NewFromServers(servers), nil
}
// NewFromServers returns a new Client using the provided Servers.
func NewFromServers(servers Servers) *Client {
return &Client{
timeout: DefaultTimeout,
maxIdlePerAddr: maxIdleConnsPerAddr,
servers: servers,
freeconn: make(map[string]chan *conn),
bufPool: make(chan []byte, poolSize()),
}
}
// Client is a memcache client.
// It is safe for unlocked use by multiple concurrent goroutines.
type Client struct {
timeout time.Duration
maxIdlePerAddr int
servers Servers
mu sync.RWMutex
freeconn map[string]chan *conn
bufPool chan []byte
}
// Timeout returns the socket read/write timeout. By default, it's
// DefaultTimeout.
func (c *Client) Timeout() time.Duration {
return c.timeout
}
// SetTimeout specifies the socket read/write timeout.
// If zero, DefaultTimeout is used. If < 0, there's
// no timeout. This method must be called before any
// connections to the memcached server are opened.
func (c *Client) SetTimeout(timeout time.Duration) {
if timeout == time.Duration(0) {
timeout = DefaultTimeout
}
c.timeout = timeout
}
// MaxIdleConnsPerAddr returns the maximum number of idle
// connections kept per server address.
func (c *Client) MaxIdleConnsPerAddr() int {
return c.maxIdlePerAddr
}
// SetMaxIdleConnsPerAddr changes the maximum number of
// idle connections kept per server. If maxIdle < 0,
// no idle connections are kept. If maxIdle == 0,
// the default number (currently 2) is used.
func (c *Client) SetMaxIdleConnsPerAddr(maxIdle int) {
if maxIdle == 0 {
maxIdle = maxIdleConnsPerAddr
}
c.mu.Lock()
defer c.mu.Unlock()
c.maxIdlePerAddr = maxIdle
if maxIdle > 0 {
freeconn := make(map[string]chan *conn)
for k, v := range c.freeconn {
ch := make(chan *conn, maxIdle)
ChanDone:
for {
select {
case cn := <-v:
select {
case ch <- cn:
default:
cn.nc.Close()
}
default:
freeconn[k] = ch
break ChanDone
}
}
}
c.freeconn = freeconn
} else {
c.closeIdleConns()
c.freeconn = nil
}
}
// Close closes all currently open connections.
func (c *Client) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
c.closeIdleConns()
c.freeconn = nil
c.maxIdlePerAddr = 0
return nil
}
// 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
// Object is the Item's value for use with a Codec.
Object interface{}
// 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
}
// conn is a connection to a server.
type conn struct {
nc net.Conn
addr *Addr
}
// condRelease releases this connection if the error pointed to by err
// is is nil (not an error) or is only a protocol level error (e.g. a
// cache miss). The purpose is to not recycle TCP connections that
// are bad.
func (c *Client) condRelease(cn *conn, err *error) {
switch *err {
case nil, ErrCacheMiss, ErrCASConflict, ErrNotStored, ErrBadIncrDec:
c.putFreeConn(cn)
default:
cn.nc.Close()
}
}
func (c *Client) closeIdleConns() {
for _, v := range c.freeconn {
NextIdle:
for {
select {
case cn := <-v:
cn.nc.Close()
default:
break NextIdle
}
}
}
}
func (c *Client) putFreeConn(cn *conn) {
c.mu.RLock()
freelist := c.freeconn[cn.addr.s]
maxIdle := c.maxIdlePerAddr
c.mu.RUnlock()
if freelist == nil && maxIdle > 0 {
freelist = make(chan *conn, maxIdle)
c.mu.Lock()
c.freeconn[cn.addr.s] = freelist
c.mu.Unlock()
}
select {
case freelist <- cn:
break
default:
cn.nc.Close()
}
}
func (c *Client) getFreeConn(addr *Addr) *conn {
c.mu.RLock()
freelist := c.freeconn[addr.s]
c.mu.RUnlock()
if freelist == nil {
return nil
}
select {
case cn := <-freelist:
return cn
default:
return nil
}
}
// ConnectTimeoutError is the error type used when it takes
// too long to connect to the desired host. This level of
// detail can generally be ignored.
type ConnectTimeoutError struct {
Addr net.Addr
}
func (cte *ConnectTimeoutError) Error() string {
return "memcache: connect timeout to " + cte.Addr.String()
}
func (cte *ConnectTimeoutError) Timeout() bool {
return true
}
func (cte *ConnectTimeoutError) Temporary() bool {
return true
}
func (c *Client) dial(addr *Addr) (net.Conn, error) {
if c.timeout > 0 {
conn, err := net.DialTimeout(addr.n, addr.s, c.timeout)
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Timeout() {
return nil, &ConnectTimeoutError{addr}
}
return nil, err
}
return conn, nil
}
return net.Dial(addr.n, addr.s)
}
func (c *Client) getConn(addr *Addr) (*conn, error) {
cn := c.getFreeConn(addr)
if cn == nil {
nc, err := c.dial(addr)
if err != nil {
return nil, err
}
cn = &conn{
nc: nc,
addr: addr,
}
}
if c.timeout > 0 {
cn.nc.SetDeadline(time.Now().Add(c.timeout))
}
return cn, nil
}
// 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 *Client) Get(key string) (*Item, error) {
cn, err := c.sendCommand(key, cmdGet, nil, 0, nil)
if err != nil {
return nil, err
}
return c.parseItemResponse(key, cn, true)
}
func (c *Client) sendCommand(key string, cmd command, value []byte, casid uint64, extras []byte) (*conn, error) {
addr, err := c.servers.PickServer(key)
if err != nil {
return nil, err
}
cn, err := c.getConn(addr)
if err != nil {
return nil, err
}
err = c.sendConnCommand(cn, key, cmd, value, casid, extras)
if err != nil {
cn.nc.Close()
return nil, err
}
return cn, err
}
func (c *Client) sendConnCommand(cn *conn, key string, cmd command, value []byte, casid uint64, extras []byte) (err error) {
var buf []byte
select {
// 24 is header size
case buf = <-c.bufPool:
buf = buf[:24]
default:
buf = make([]byte, 24, 24+len(key)+len(extras))
// Magic (0)
buf[0] = reqMagic
}
// Command (1)
buf[1] = byte(cmd)
kl := len(key)
el := len(extras)
// Key length (2-3)
putUint16(buf[2:], uint16(kl))
// Extras length (4)
buf[4] = byte(el)
// Data type (5), always zero
// VBucket (6-7), always zero
// Total body length (8-11)
vl := len(value)
bl := uint32(kl + el + vl)
putUint32(buf[8:], bl)
// Opaque (12-15), always zero
// CAS (16-23)
putUint64(buf[16:], casid)
// Extras
if el > 0 {
buf = append(buf, extras...)
}
if kl > 0 {
// Key itself
buf = append(buf, stobs(key)...)
}
if _, err = cn.nc.Write(buf); err != nil {
return err
}
select {
case c.bufPool <- buf:
default:
}
if vl > 0 {
if _, err = cn.nc.Write(value); err != nil {
return err
}
}
return nil
}
func (c *Client) parseResponse(rKey string, cn *conn) ([]byte, []byte, []byte, []byte, error) {
var err error
hdr := make([]byte, 24)
if err = readAtLeast(cn.nc, hdr, 24); err != nil {
return nil, nil, nil, nil, err
}
if hdr[0] != respMagic {
return nil, nil, nil, nil, ErrBadMagic
}
total := int(bUint32(hdr[8:12]))
status := bUint16(hdr[6:8])
if status != respOk {
if _, err = io.CopyN(ioutil.Discard, cn.nc, int64(total)); err != nil {
return nil, nil, nil, nil, err
}
if status == respInvalidArgs && !legalKey(rKey) {
return nil, nil, nil, nil, ErrMalformedKey
}
return nil, nil, nil, nil, response(status).asError()
}
var extras []byte
el := int(hdr[4])
if el > 0 {
extras = make([]byte, el)
if err = readAtLeast(cn.nc, extras, el); err != nil {
return nil, nil, nil, nil, err
}
}
var key []byte
kl := int(bUint16(hdr[2:4]))
if kl > 0 {
key = make([]byte, int(kl))
if err = readAtLeast(cn.nc, key, kl); err != nil {
return nil, nil, nil, nil, err
}
}
var value []byte
vl := total - el - kl
if vl > 0 {
value = make([]byte, vl)
if err = readAtLeast(cn.nc, value, vl); err != nil {
return nil, nil, nil, nil, err
}
}
return hdr, key, extras, value, nil
}
func (c *Client) parseUintResponse(key string, cn *conn) (uint64, error) {
_, _, _, value, err := c.parseResponse(key, cn)
c.condRelease(cn, &err)
if err != nil {
return 0, err
}
return bUint64(value), nil
}
func (c *Client) parseItemResponse(key string, cn *conn, release bool) (*Item, error) {
hdr, k, extras, value, err := c.parseResponse(key, cn)
if release {
c.condRelease(cn, &err)
}
if err != nil {
return nil, err
}
var flags uint32
if len(extras) > 0 {
flags = bUint32(extras)
}
if key == "" && len(k) > 0 {
key = string(k)
}
return &Item{
Key: key,
Value: value,
Flags: flags,
casid: bUint64(hdr[16:24]),
}, 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 *Client) GetMulti(keys []string) (map[string]*Item, error) {
keyMap := make(map[*Addr][]string)
for _, key := range keys {
addr, err := c.servers.PickServer(key)
if err != nil {
return nil, err
}
keyMap[addr] = append(keyMap[addr], key)
}
var chs []chan *Item
for addr, keys := range keyMap {
ch := make(chan *Item)
chs = append(chs, ch)
go func(addr *Addr, keys []string, ch chan *Item) {
defer close(ch)
cn, err := c.getConn(addr)
if err != nil {
return
}
defer c.condRelease(cn, &err)
for _, k := range keys {
if err = c.sendConnCommand(cn, k, cmdGetKQ, nil, 0, nil); err != nil {
return
}
}
if err = c.sendConnCommand(cn, "", cmdNoop, nil, 0, nil); err != nil {
return
}
var item *Item
for {
item, err = c.parseItemResponse("", cn, false)
if item == nil || item.Key == "" {
// Noop response
break
}
ch <- item
}
}(addr, keys, ch)
}
m := make(map[string]*Item)
for _, ch := range chs {
for item := range ch {
m[item.Key] = item
}
}
return m, nil
}
// Set writes the given item, unconditionally.
func (c *Client) Set(item *Item) error {
return c.populateOne(cmdSet, item, 0)
}
// Add writes the given item, if no value already exists for its
// key. ErrNotStored is returned if that condition is not met.
func (c *Client) Add(item *Item) error {
return c.populateOne(cmdAdd, item, 0)
}
// 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 *Client) CompareAndSwap(item *Item) error {
return c.populateOne(cmdSet, item, item.casid)
}
func (c *Client) populateOne(cmd command, item *Item, casid uint64) error {
extras := make([]byte, 8)
putUint32(extras, item.Flags)
putUint32(extras[4:8], uint32(item.Expiration))
cn, err := c.sendCommand(item.Key, cmd, item.Value, casid, extras)
if err != nil {
return err
}
hdr, _, _, _, err := c.parseResponse(item.Key, cn)
if err != nil {
c.condRelease(cn, &err)
return err
}
c.putFreeConn(cn)
item.casid = bUint64(hdr[16:24])
return nil
}
// 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 *Client) Delete(key string) error {
cn, err := c.sendCommand(key, cmdDelete, nil, 0, nil)
if err != nil {
return err
}
_, _, _, _, err = c.parseResponse(key, cn)
c.condRelease(cn, &err)
return err
}
// 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 *Client) Increment(key string, delta uint64) (newValue uint64, err error) {
return c.incrDecr(cmdIncr, 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 *Client) Decrement(key string, delta uint64) (newValue uint64, err error) {
return c.incrDecr(cmdDecr, key, delta)
}
func (c *Client) incrDecr(cmd command, key string, delta uint64) (uint64, error) {
extras := make([]byte, 20)
putUint64(extras, delta)
// Set expiration to 0xfffffff, so the command fails if the key
// does not exist.
for ii := 16; ii < 20; ii++ {
extras[ii] = 0xff
}
cn, err := c.sendCommand(key, cmd, nil, 0, extras)
if err != nil {
return 0, err
}
return c.parseUintResponse(key, cn)
}
// Flush removes all the items in the cache after expiration seconds. If
// expiration is <= 0, it removes all the items right now.
func (c *Client) Flush(expiration int) error {
servers, err := c.servers.Servers()
var failed []*Addr
var errs []error
if err != nil {
return err
}
var extras []byte
if expiration > 0 {
extras = make([]byte, 4)
putUint32(extras, uint32(expiration))
}
for _, addr := range servers {
cn, err := c.getConn(addr)
if err != nil {
failed = append(failed, addr)
errs = append(errs, err)
continue
}
if err = c.sendConnCommand(cn, "", cmdFlush, nil, 0, extras); err == nil {
_, _, _, _, err = c.parseResponse("", cn)
}
if err != nil {
failed = append(failed, addr)
errs = append(errs, err)
}
c.condRelease(cn, &err)
}
if len(failed) > 0 {
var buf bytes.Buffer
buf.WriteString("failed to flush some servers: ")
for ii, addr := range failed {
if ii > 0 {
buf.WriteString(", ")
}
buf.WriteString(addr.String())
buf.WriteString(": ")
buf.WriteString(errs[ii].Error())
}
return errors.New(buf.String())
}
return nil
}