forked from rethinkdb/rethinkdb-go
/
connection.go
282 lines (238 loc) · 6.68 KB
/
connection.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
package gorethink
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"net"
"sync"
"time"
"code.google.com/p/goprotobuf/proto"
p "github.com/dancannon/gorethink/ql2"
)
type Conn interface {
SendQuery(s *Session, q *p.Query, t Term, opts map[string]interface{}, async bool) (*Cursor, error)
ReadResponse(s *Session, token int64) (*p.Response, error)
Close() error
}
// connection is a connection to a rethinkdb database
type Connection struct {
// embed the net.Conn type, so that we can effectively define new methods on
// it (interfaces do not allow that)
net.Conn
s *Session
sync.Mutex
closed bool
}
// Dial closes the previous connection and attempts to connect again.
func Dial(s *Session) (*Connection, error) {
conn, err := net.Dial("tcp", s.address)
if err != nil {
return nil, RqlConnectionError{err.Error()}
}
// Send the protocol version to the server as a 4-byte little-endian-encoded integer
if err := binary.Write(conn, binary.LittleEndian, p.VersionDummy_V0_3); err != nil {
return nil, RqlConnectionError{err.Error()}
}
// Send the length of the auth key to the server as a 4-byte little-endian-encoded integer
if err := binary.Write(conn, binary.LittleEndian, uint32(len(s.authkey))); err != nil {
return nil, RqlConnectionError{err.Error()}
}
// Send the auth key as an ASCII string
// If there is no auth key, skip this step
if s.authkey != "" {
if _, err := io.WriteString(conn, s.authkey); err != nil {
return nil, RqlConnectionError{err.Error()}
}
}
// Send the protocol type as a 4-byte little-endian-encoded integer
if err := binary.Write(conn, binary.LittleEndian, p.VersionDummy_PROTOBUF); err != nil {
return nil, RqlConnectionError{err.Error()}
}
// read server response to authorization key (terminated by NUL)
reader := bufio.NewReader(conn)
line, err := reader.ReadBytes('\x00')
if err != nil {
if err == io.EOF {
return nil, fmt.Errorf("Unexpected EOF: %s", string(line))
}
return nil, RqlDriverError{err.Error()}
}
// convert to string and remove trailing NUL byte
response := string(line[:len(line)-1])
if response != "SUCCESS" {
// we failed authorization or something else terrible happened
return nil, RqlDriverError{fmt.Sprintf("Server dropped connection with message: \"%s\"", response)}
}
return &Connection{
s: s,
Conn: conn,
}, nil
}
func TestOnBorrow(c *Connection, t time.Time) error {
c.SetReadDeadline(t)
data := make([]byte, 1)
if _, err := c.Read(data); err != nil {
e, ok := err.(net.Error)
if err != nil && !(ok && e.Timeout()) {
return err
}
}
c.SetReadDeadline(time.Time{})
return nil
}
func (c *Connection) ReadResponse(s *Session, token int64) (*p.Response, error) {
for {
var messageLength uint32
if err := binary.Read(c, binary.LittleEndian, &messageLength); err != nil {
c.Close()
return nil, RqlConnectionError{err.Error()}
}
buffer := make([]byte, messageLength)
if _, err := io.ReadFull(c, buffer); err != nil {
c.Close()
return nil, RqlDriverError{err.Error()}
}
response := &p.Response{}
if err := proto.Unmarshal(buffer, response); err != nil {
return nil, RqlDriverError{err.Error()}
}
if response.GetToken() == token {
return response, nil
} else if cursor, ok := s.checkCache(token); ok {
// Handle batch response
s.handleBatchResponse(cursor, response)
} else {
return nil, RqlDriverError{"Unexpected response received"}
}
}
}
func (c *Connection) SendQuery(s *Session, q *p.Query, t Term, opts map[string]interface{}, async bool) (*Cursor, error) {
var data []byte
var err error
// Ensure that the connection is not closed
if s.closed {
return nil, RqlDriverError{"Connection is closed"}
}
// Set timeout
if s.timeout == 0 {
c.SetDeadline(time.Time{})
} else {
c.SetDeadline(time.Now().Add(s.timeout))
}
// Send query
if data, err = proto.Marshal(q); err != nil {
return nil, RqlDriverError{err.Error()}
}
if err = binary.Write(c, binary.LittleEndian, uint32(len(data))); err != nil {
c.Close()
return nil, RqlConnectionError{err.Error()}
}
if err = binary.Write(c, binary.BigEndian, data); err != nil {
c.Close()
return nil, RqlConnectionError{err.Error()}
}
// Return immediately if the noreply option was set
if noreply, ok := opts["noreply"]; ok && noreply.(bool) {
c.Close()
return nil, nil
} else if async {
return nil, nil
}
// Get response
response, err := c.ReadResponse(s, *q.Token)
if err != nil {
return nil, err
}
err = checkErrorResponse(response, t)
if err != nil {
return nil, err
}
// De-construct the profile datum if it exists
var profile interface{}
if response.GetProfile() != nil {
var err error
profile, err = deconstructDatum(response.GetProfile(), opts)
if err != nil {
return nil, RqlDriverError{err.Error()}
}
}
// De-construct datum and return a cursor
switch response.GetType() {
case p.Response_SUCCESS_PARTIAL, p.Response_SUCCESS_SEQUENCE, p.Response_SUCCESS_FEED:
cursor := &Cursor{
session: s,
conn: c,
query: q,
term: t,
opts: opts,
profile: profile,
}
s.setCache(*q.Token, cursor)
cursor.extend(response)
return cursor, nil
case p.Response_SUCCESS_ATOM:
var value []interface{}
var err error
if len(response.GetResponse()) < 1 {
value = []interface{}{}
} else if response.GetResponse()[0].GetType() == p.Datum_R_ARRAY {
value, err = deconstructDatums(response.GetResponse()[0].GetRArray(), opts)
if err != nil {
return nil, err
}
} else {
var v interface{}
v, err = deconstructDatum(response.GetResponse()[0], opts)
if err != nil {
return nil, RqlDriverError{err.Error()}
}
if sv, ok := v.([]interface{}); ok {
value = sv
} else if v == nil {
value = []interface{}{nil}
} else {
value = []interface{}{v}
}
}
cursor := &Cursor{
session: s,
conn: c,
query: q,
term: t,
opts: opts,
profile: profile,
buffer: value,
finished: true,
}
return cursor, nil
case p.Response_WAIT_COMPLETE:
return nil, nil
default:
return nil, RqlDriverError{fmt.Sprintf("Unexpected response type received: %s", response.GetType())}
}
}
func (c *Connection) Close() error {
err := c.s.noreplyWaitQuery()
if err != nil {
return err
}
return c.CloseNoWait()
}
func (c *Connection) CloseNoWait() error {
c.Lock()
c.closed = true
c.Unlock()
return c.Conn.Close()
}
func checkErrorResponse(response *p.Response, t Term) error {
switch response.GetType() {
case p.Response_CLIENT_ERROR:
return RqlClientError{rqlResponseError{response, t}}
case p.Response_COMPILE_ERROR:
return RqlCompileError{rqlResponseError{response, t}}
case p.Response_RUNTIME_ERROR:
return RqlRuntimeError{rqlResponseError{response, t}}
}
return nil
}