-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
149 lines (133 loc) · 3.13 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
package memcached
import (
"bufio"
"io"
"net"
"strings"
"sync"
"time"
"github.com/pkg/errors"
)
type conn struct {
cl *Client
mu sync.Mutex
net.Conn
buffered bufio.ReadWriter
isAlive bool
createdAt time.Time
}
// Error
var (
ErrMemcachedClosed = errors.New("memcached is closed")
ErrDeadNode = errors.New("dead node")
ErrConnect = errors.New("connect error")
ErrBadConn = errors.New("bad conn")
ErrNotFound = errors.New("not found")
)
func (c *conn) reset() error {
c.mu.Lock()
defer c.mu.Unlock()
if wb := c.buffered.Writer.Buffered(); wb > 0 {
if err := c.flush(); err != nil {
return errors.Wrap(err, "Failed flush")
}
}
if rb := c.buffered.Reader.Buffered(); rb > 0 {
if _, err := c.buffered.Discard(rb); err != nil {
return errors.Wrap(err, "Failed Discard")
}
}
return c.setDeadline()
}
func (c *conn) close() error {
if !c.isAlive {
return nil
}
if err := c.Conn.Close(); err != nil {
return err
}
c.isAlive = false
return nil
}
func (c *conn) expired(timeout time.Duration) bool {
if timeout <= 0 {
return false
}
return c.createdAt.Add(timeout).Before(time.Now())
}
func (c *conn) setDeadline() error {
if c.Conn == nil {
return nil
}
if err := c.SetDeadline(time.Now().Add(c.cl.pollTimeout)); err != nil {
return errors.Wrap(err, "Failed SetDeadline")
}
return nil
}
func (c *conn) writestrings(strs ...string) error {
for _, s := range strs {
if err := c.writestring(s); err != nil {
return errors.Wrap(err, "Failed writestring")
}
}
return nil
}
func (c *conn) writestring(s string) error {
if _, err := c.buffered.WriteString(s); err != nil {
return errors.Wrap(err, "Failed buffered.WriteString")
}
return nil
}
func (c *conn) write(b []byte) error {
if _, err := c.buffered.Write(b); err != nil {
return errors.Wrap(err, "Failed buffered,Write")
}
return nil
}
func (c *conn) flush() error {
if err := c.buffered.Flush(); err != nil {
return errors.Wrapf(ErrBadConn, "Failed buffered.Flush: %+v", err)
}
return nil
}
func (c *conn) readline() (string, error) {
if err := c.flush(); err != nil {
return "", errors.Wrap(err, "Failed flush")
}
l, err := c.buffered.ReadSlice('\n')
if err != nil {
return "", errors.Wrapf(ErrBadConn, "Failed bufferd.ReadSlice: %+v", err)
}
if err = handleError(string(l)); err != nil {
return "", err
}
return strings.Replace(string(l), "\r\n", "", -1), nil
}
func (c *conn) read(count int) ([]byte, error) {
if err := c.flush(); err != nil {
return []byte{}, errors.Wrap(err, "Failed flush")
}
b := make([]byte, count)
if err := c.SetDeadline(time.Now().Add(c.cl.pollTimeout)); err != nil {
return []byte{}, errors.Wrap(err, "Failed SetDeadLine")
}
if _, err := io.ReadFull(c.buffered, b); err != nil {
return b, errors.Wrapf(ErrBadConn, "Failed ReadFull: %+v", err)
}
if err := handleError(string(b)); err != nil {
return []byte{}, err
}
return b, nil
}
func handleError(s string) error {
if s == "ERROR" {
return ErrNonexistentCommand
}
if strings.HasPrefix(s, "CLIENT_ERROR") {
return ErrClient
}
if strings.HasPrefix(s, "SERVER_ERROR") {
return ErrServer
}
return nil
}