-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcache.go
48 lines (40 loc) · 819 Bytes
/
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
package memcache
import (
"errors"
"sync/atomic"
)
// Client ...
type Client struct {
conns []*clientConn
next uint64
}
// New ...
func New(addr string, numConns int, options ...Option) (*Client, error) {
if numConns <= 0 {
return nil, errors.New("numConns must > 0")
}
conns := make([]*clientConn, 0, numConns)
for i := 0; i < numConns; i++ {
c := newConn(addr, options...)
conns = append(conns, c)
}
return &Client{
conns: conns,
next: 0,
}, nil
}
// Close shut down Client
func (c *Client) Close() error {
var err error
for _, conn := range c.conns {
err = conn.shutdown()
}
for _, conn := range c.conns {
conn.waitCloseCompleted()
}
return err
}
func (c *Client) getNextConn() *clientConn {
next := atomic.AddUint64(&c.next, 1)
return c.conns[next%uint64(len(c.conns))]
}