forked from rethinkdb/rethinkdb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
151 lines (131 loc) · 3.79 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
package gorethink
import (
"bufio"
"code.google.com/p/goprotobuf/proto"
"encoding/binary"
"fmt"
p "github.com/dancannon/gorethink/ql2"
"io"
"net"
"time"
)
type Conn interface {
SendQuery(s *Session, q *p.Query, t RqlTerm, opts map[string]interface{}) (*ResultRows, 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
}
// Reconnect 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, err
}
if err := binary.Write(conn, binary.LittleEndian, p.VersionDummy_V0_2); err != nil {
return nil, err
}
// authorization key
if err := binary.Write(conn, binary.LittleEndian, uint32(len(s.authkey))); err != nil {
return nil, err
}
if err := binary.Write(conn, binary.BigEndian, []byte(s.authkey)); err != nil {
return nil, err
}
// read server response to authorization key (terminated by NUL)
reader := bufio.NewReader(conn)
line, err := reader.ReadBytes('\x00')
if err != nil {
return nil, err
}
// 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{conn}, nil
}
func (c *Connection) SendQuery(s *Session, q *p.Query, t RqlTerm, opts map[string]interface{}) (*ResultRows, 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, err
}
if err = binary.Write(c, binary.LittleEndian, uint32(len(data))); err != nil {
return nil, err
}
if err = binary.Write(c, binary.BigEndian, data); err != nil {
return nil, err
}
// Return immediately if the noreply option was set
if noreply, ok := opts["noreply"]; ok && noreply.(bool) {
return nil, nil
}
// Read response
var messageLength uint32
if err := binary.Read(c, binary.LittleEndian, &messageLength); err != nil {
return nil, err
}
buffer := make([]byte, messageLength)
_, err = io.ReadFull(c, buffer)
if err != nil {
return nil, err
}
r := &p.Response{}
err = proto.Unmarshal(buffer, r)
if err != nil {
return nil, err
}
// Ensure that this is the response we were expecting
if q.GetToken() != r.GetToken() {
return nil, RqlDriverError{"Unexpected response received."}
}
// De-construct datum and return the result
switch r.GetType() {
case p.Response_CLIENT_ERROR:
return nil, RqlClientError{rqlResponseError{r, t}}
case p.Response_COMPILE_ERROR:
return nil, RqlCompileError{rqlResponseError{r, t}}
case p.Response_RUNTIME_ERROR:
return nil, RqlRuntimeError{rqlResponseError{r, t}}
case p.Response_SUCCESS_PARTIAL, p.Response_SUCCESS_SEQUENCE:
return &ResultRows{
session: s,
query: q,
term: t,
opts: opts,
buffer: r.GetResponse(),
end: len(r.GetResponse()),
token: q.GetToken(),
responseType: r.GetType(),
}, nil
case p.Response_SUCCESS_ATOM:
return &ResultRows{
session: s,
query: q,
term: t,
opts: opts,
buffer: r.GetResponse(),
end: len(r.GetResponse()),
token: q.GetToken(),
responseType: r.GetType(),
}, nil
default:
return nil, RqlDriverError{fmt.Sprintf("Unexpected response type received: %s", r.GetType())}
}
}