forked from couchbase/gocbcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemdconn.go
176 lines (147 loc) · 3.9 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
172
173
174
175
176
package gocbcore
import (
"bufio"
"context"
"crypto/tls"
"io"
"net"
"sync"
"time"
"github.com/couchbase/gocbcore/v10/memd"
)
const defaultReaderBufSize = 20 * 1024 * 1024
type memdConn interface {
LocalAddr() string
RemoteAddr() string
WritePacket(*memd.Packet) error
ReadPacket() (*memd.Packet, int, error)
Close() error
Release()
EnableFeature(feature memd.HelloFeature)
IsFeatureEnabled(feature memd.HelloFeature) bool
}
type wrappedReadWriteCloser struct {
*bufio.Reader
io.Writer
io.Closer
}
// readerBufPools - Map of buffer size to thread safe pool containing packet reader buffers.
var readerBufPools = map[int]*sync.Pool{}
var readerBufPoolsLock sync.Mutex
// acquireReadBuf - Returns a pointer to a read buffer which is ready to be used, ensure the buffer is released using
// the 'releaseWriteBuf' function.
func acquireReadBuf(stream io.Reader, bufSize int) *bufio.Reader {
readerBufPoolsLock.Lock()
bufPool, ok := readerBufPools[bufSize]
if !ok {
bufPool = &sync.Pool{}
readerBufPools[bufSize] = bufPool
}
readerBufPoolsLock.Unlock()
iReader := bufPool.Get()
var reader *bufio.Reader
if iReader == nil {
reader = bufio.NewReaderSize(stream, bufSize)
} else {
var ok bool
reader, ok = iReader.(*bufio.Reader)
if ok {
reader.Reset(stream)
} else {
reader = bufio.NewReaderSize(stream, bufSize)
}
}
return reader
}
// releaseReadBuf - Reset the buffer so that it's clean for the next user (note that this retains the underlying
// storage for future reads) and then return it to the pool.
func releaseReadBuf(buf *bufio.Reader, bufSize int) {
buf.Reset(nil)
readerBufPoolsLock.Lock()
bufPool, ok := readerBufPools[bufSize]
if !ok {
readerBufPoolsLock.Unlock()
logWarnf("Attempted to release a read buffer for a buffer size without a registered pool")
return
}
bufPool.Put(buf)
readerBufPoolsLock.Unlock()
}
type memdConnWrap struct {
localAddr string
remoteAddr string
conn *memd.Conn
baseConn *wrappedReadWriteCloser
bufSize int
}
func (s *memdConnWrap) LocalAddr() string {
return s.localAddr
}
func (s *memdConnWrap) RemoteAddr() string {
return s.remoteAddr
}
func (s *memdConnWrap) WritePacket(pkt *memd.Packet) error {
return s.conn.WritePacket(pkt)
}
func (s *memdConnWrap) ReadPacket() (*memd.Packet, int, error) {
return s.conn.ReadPacket()
}
func (s *memdConnWrap) EnableFeature(feature memd.HelloFeature) {
s.conn.EnableFeature(feature)
}
func (s *memdConnWrap) IsFeatureEnabled(feature memd.HelloFeature) bool {
return s.conn.IsFeatureEnabled(feature)
}
func (s *memdConnWrap) Close() error {
return s.baseConn.Close()
}
// Release is not thread safe and should not be called whilst there are pending calls, such as ReadPacket.
func (s *memdConnWrap) Release() {
if s.baseConn == nil {
logWarnf("Release called on already released connection")
return
}
releaseReadBuf(s.baseConn.Reader, s.bufSize)
s.baseConn = nil
}
func dialMemdConn(ctx context.Context, address string, tlsConfig *tls.Config, deadline time.Time, bufSize uint) (memdConn, error) {
d := net.Dialer{
Deadline: deadline,
}
baseConn, err := d.DialContext(ctx, "tcp", address)
if err != nil {
return nil, err
}
tcpConn, isTCPConn := baseConn.(*net.TCPConn)
if !isTCPConn || tcpConn == nil {
return nil, errCliInternalError
}
err = tcpConn.SetNoDelay(false)
if err != nil {
logWarnf("Failed to disable TCP nodelay (%s)", err)
}
var conn io.ReadWriteCloser = tcpConn
if tlsConfig != nil {
tlsConn := tls.Client(tcpConn, tlsConfig)
err = tlsConn.Handshake()
if err != nil {
return nil, err
}
conn = tlsConn
}
if bufSize == 0 {
bufSize = defaultReaderBufSize
}
c := &wrappedReadWriteCloser{
Reader: acquireReadBuf(conn, int(bufSize)),
Writer: conn,
Closer: conn,
}
return &memdConnWrap{
conn: memd.NewConn(c),
baseConn: c,
localAddr: baseConn.LocalAddr().String(),
remoteAddr: address,
bufSize: int(bufSize),
}, nil
}