forked from rethinkdb/rethinkdb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
293 lines (246 loc) · 7.19 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
283
284
285
286
287
288
289
290
291
292
293
package gorethink
import (
"bufio"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"net"
"sync"
"time"
"gopkg.in/fatih/pool.v2"
p "github.com/dancannon/gorethink/ql2"
)
type Response struct {
Token int64
Type p.Response_ResponseType `json:"t"`
Responses []interface{} `json:"r"`
Backtrace []interface{} `json:"b"`
Profile interface{} `json:"p"`
}
type Conn interface {
SendQuery(s *Session, q *p.Query, t Term, opts map[string]interface{}, async bool) (*Cursor, error)
ReadResponse(s *Session, token int64) (*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
}
// Dial closes the previous connection and attempts to connect again.
func Dial(s *Session) pool.Factory {
return func() (net.Conn, 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_JSON); 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 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) (*Response, error) {
for {
// Read the 8-byte token of the query the response corresponds to.
var responseToken int64
if err := binary.Read(c, binary.LittleEndian, &responseToken); err != nil {
return nil, RqlConnectionError{err.Error()}
}
// Read the length of the JSON-encoded response as a 4-byte
// little-endian-encoded integer.
var messageLength uint32
if err := binary.Read(c, binary.LittleEndian, &messageLength); err != nil {
return nil, RqlConnectionError{err.Error()}
}
// Read the JSON encoding of the Response itself.
b := make([]byte, messageLength)
if _, err := io.ReadFull(c, b); err != nil {
return nil, RqlDriverError{err.Error()}
}
// Decode the response
var response = new(Response)
response.Token = responseToken
err := json.Unmarshal(b, response)
if err != nil {
return nil, RqlDriverError{err.Error()}
}
if responseToken == 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 Query, opts map[string]interface{}, async bool) (*Cursor, error) {
var err error
// Build query
b, err := json.Marshal(q.build())
if err != nil {
return nil, RqlDriverError{"Error building query"}
}
// Set timeout
if s.timeout == 0 {
c.SetDeadline(time.Time{})
} else {
c.SetDeadline(time.Now().Add(s.timeout))
}
// Send a unique 8-byte token
if err = binary.Write(c, binary.LittleEndian, q.Token); err != nil {
return nil, RqlConnectionError{err.Error()}
}
// Send the length of the JSON-encoded query as a 4-byte
// little-endian-encoded integer.
if err = binary.Write(c, binary.LittleEndian, uint32(len(b))); err != nil {
return nil, RqlConnectionError{err.Error()}
}
// Send the JSON encoding of the query itself.
if err = binary.Write(c, binary.BigEndian, b); err != nil {
return nil, RqlConnectionError{err.Error()}
}
// Return immediately if the noreply option was set
if noreply, ok := opts["noreply"]; (ok && noreply.(bool)) || async {
return nil, nil
}
// Get response
response, err := c.ReadResponse(s, q.Token)
if err != nil {
return nil, err
}
err = checkErrorResponse(response, q.Term)
if err != nil {
return nil, err
}
// De-construct datum and return a cursor
switch response.Type {
case p.Response_SUCCESS_PARTIAL, p.Response_SUCCESS_SEQUENCE, p.Response_SUCCESS_FEED:
cursor := &Cursor{
session: s,
conn: c,
query: q,
term: *q.Term,
opts: opts,
profile: response.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.Responses) < 1 {
value = []interface{}{}
} else {
var v interface{}
v, err = recursivelyConvertPseudotype(response.Responses[0], opts)
if err != nil {
return nil, err
}
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: *q.Term,
opts: opts,
profile: response.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.Type)}
}
}
func (c *Connection) Close() error {
err := c.NoreplyWait()
if err != nil {
return err
}
return c.Conn.Close()
}
// noreplyWaitQuery sends the NOREPLY_WAIT query to the server.
func (c *Connection) NoreplyWait() error {
q := Query{
Type: p.Query_NOREPLY_WAIT,
Token: c.s.nextToken(),
}
_, err := c.SendQuery(c.s, q, map[string]interface{}{}, false)
if err != nil {
return err
}
return nil
}
func checkErrorResponse(response *Response, t *Term) error {
switch response.Type {
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
}