-
Notifications
You must be signed in to change notification settings - Fork 1
/
memdconn.go
171 lines (146 loc) · 3.92 KB
/
memdconn.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
package gocouchbaseio
import (
"crypto/tls"
"encoding/binary"
"io"
"net"
)
// The data for a request that can be queued with a memdqueueconn,
// and can potentially be rerouted to multiple servers due to
// configuration changes.
type memdRequest struct {
Magic CommandMagic
Opcode CommandCode
Datatype uint8
Vbucket uint16
Opaque uint32
Cas uint64
Key []byte
Extras []byte
Value []byte
}
// The data returned from the server in relation to an executed
// request.
type memdResponse struct {
Magic CommandMagic
Opcode CommandCode
Datatype uint8
Status StatusCode
Opaque uint32
Cas uint64
Key []byte
Extras []byte
Value []byte
}
type memdDialer interface {
Dial(address string) (io.ReadWriteCloser, error)
}
type memdConn struct {
conn io.ReadWriteCloser
recvBuf []byte
}
func DialMemdConn(address string, tlsConfig *tls.Config) (*memdConn, error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return nil, err
}
tcpConn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
return nil, err
}
tcpConn.SetNoDelay(false)
var conn io.ReadWriteCloser
if tlsConfig == nil {
conn = tcpConn
} else {
tlsConn := tls.Client(tcpConn, &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
return nil, err
}
conn = tlsConn
}
return &memdConn{
conn: conn,
}, nil
}
func (s *memdConn) Close() error {
return s.conn.Close()
}
func (s *memdConn) WritePacket(req *memdRequest) error {
extLen := len(req.Extras)
keyLen := len(req.Key)
valLen := len(req.Value)
// Go appears to do some clever things in regards to writing data
// to the kernel for network dispatch. Having a write buffer
// per-server that is re-used actually hinders performance...
// For now, we will simply create a new buffer and let it be GC'd.
buffer := make([]byte, 24+keyLen+extLen+valLen)
buffer[0] = uint8(req.Magic)
buffer[1] = uint8(req.Opcode)
binary.BigEndian.PutUint16(buffer[2:], uint16(keyLen))
buffer[4] = byte(extLen)
buffer[5] = req.Datatype
binary.BigEndian.PutUint16(buffer[6:], uint16(req.Vbucket))
binary.BigEndian.PutUint32(buffer[8:], uint32(len(buffer)-24))
binary.BigEndian.PutUint32(buffer[12:], req.Opaque)
binary.BigEndian.PutUint64(buffer[16:], req.Cas)
copy(buffer[24:], req.Extras)
copy(buffer[24+extLen:], req.Key)
copy(buffer[24+extLen+keyLen:], req.Value)
_, err := s.conn.Write(buffer)
return err
}
func (s *memdConn) readBuffered(n int) ([]byte, error) {
// Make sure our buffer is big enough to hold all our data
if len(s.recvBuf) < n {
neededSize := 4096
if neededSize < n {
neededSize = n
}
newBuf := make([]byte, neededSize)
copy(newBuf[0:], s.recvBuf[0:])
s.recvBuf = newBuf[0:len(s.recvBuf)]
}
// Loop till we encounter an error or have enough data...
for {
// Check if we already have enough data buffered
if n <= len(s.recvBuf) {
buf := s.recvBuf[0:n]
s.recvBuf = s.recvBuf[n:]
return buf, nil
}
// Read data up to the capacity
recvTgt := s.recvBuf[len(s.recvBuf):cap(s.recvBuf)]
n, err := s.conn.Read(recvTgt)
if n <= 0 {
return nil, err
}
// Update the len of our slice to encompass our new data
s.recvBuf = s.recvBuf[:len(s.recvBuf)+n]
}
}
func (s *memdConn) ReadPacket(resp *memdResponse) error {
hdrBuf, err := s.readBuffered(24)
if err != nil {
return err
}
bodyLen := int(binary.BigEndian.Uint32(hdrBuf[8:]))
bodyBuf, err := s.readBuffered(bodyLen)
if err != nil {
return err
}
keyLen := int(binary.BigEndian.Uint16(hdrBuf[2:]))
extLen := int(hdrBuf[4])
resp.Magic = CommandMagic(hdrBuf[0])
resp.Opcode = CommandCode(hdrBuf[1])
resp.Datatype = hdrBuf[5]
resp.Status = StatusCode(binary.BigEndian.Uint16(hdrBuf[6:]))
resp.Opaque = binary.BigEndian.Uint32(hdrBuf[12:])
resp.Cas = binary.BigEndian.Uint64(hdrBuf[16:])
resp.Extras = bodyBuf[:extLen]
resp.Key = bodyBuf[extLen : extLen+keyLen]
resp.Value = bodyBuf[extLen+keyLen:]
return nil
}