-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
session.go
190 lines (158 loc) · 4.69 KB
/
session.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
package gorethink
import (
"crypto/tls"
"time"
p "github.com/dancannon/gorethink/ql2"
)
// A Session represents a connection to a RethinkDB cluster and should be used
// when executing queries.
type Session struct {
hosts []Host
opts *ConnectOpts
cluster *Cluster
closed bool
}
// ConnectOpts is used to specify optional arguments when connecting to a cluster.
type ConnectOpts struct {
Address string `gorethink:"address,omitempty"`
Addresses []string `gorethink:"addresses,omitempty"`
Database string `gorethink:"database,omitempty"`
AuthKey string `gorethink:"authkey,omitempty"`
Timeout time.Duration `gorethink:"timeout,omitempty"`
TLSConfig *tls.Config `gorethink:"tlsconfig,omitempty"`
MaxIdle int `gorethink:"max_idle,omitempty"`
MaxOpen int `gorethink:"max_open,omitempty"`
// Below options are for cluster discovery, please note there is a high
// probability of these changing as the API is still being worked on.
// DiscoverHosts is used to enable host discovery, when true the driver
// will attempt to discover any new nodes added to the cluster and then
// start sending queries to these new nodes.
DiscoverHosts bool `gorethink:"discover_hosts,omitempty"`
// NodeRefreshInterval is used to determine how often the driver should
// refresh the status of a node.
NodeRefreshInterval time.Duration `gorethink:"node_refresh_interval,omitempty"`
}
func (o *ConnectOpts) toMap() map[string]interface{} {
return optArgsToMap(o)
}
// Connect creates a new database session. To view the available connection
// options see ConnectOpts.
//
// By default maxIdle and maxOpen are set to 1: passing values greater
// than the default (e.g. MaxIdle: "10", MaxOpen: "20") will provide a
// pool of re-usable connections.
//
// Basic connection example:
//
// session, err := r.Connect(r.ConnectOpts{
// Host: "localhost:28015",
// Database: "test",
// AuthKey: "14daak1cad13dj",
// })
//
// Cluster connection example:
//
// session, err := r.Connect(r.ConnectOpts{
// Hosts: []string{"localhost:28015", "localhost:28016"},
// Database: "test",
// AuthKey: "14daak1cad13dj",
// })
func Connect(opts ConnectOpts) (*Session, error) {
var addresses = opts.Addresses
if len(addresses) == 0 {
addresses = []string{opts.Address}
}
hosts := make([]Host, len(addresses))
for i, address := range addresses {
hostname, port := splitAddress(address)
hosts[i] = NewHost(hostname, port)
}
if len(hosts) <= 0 {
return nil, ErrNoHosts
}
// Connect
s := &Session{
hosts: hosts,
opts: &opts,
}
err := s.Reconnect()
if err != nil {
return nil, err
}
return s, nil
}
// CloseOpts allows calls to the Close function to be configured.
type CloseOpts struct {
NoReplyWait bool `gorethink:"noreplyWait,omitempty"`
}
func (o *CloseOpts) toMap() map[string]interface{} {
return optArgsToMap(o)
}
// Reconnect closes and re-opens a session.
func (s *Session) Reconnect(optArgs ...CloseOpts) error {
var err error
if err = s.Close(optArgs...); err != nil {
return err
}
s.cluster, err = NewCluster(s.hosts, s.opts)
if err != nil {
return err
}
s.closed = false
return nil
}
// Close closes the session
func (s *Session) Close(optArgs ...CloseOpts) error {
if s.closed {
return nil
}
if len(optArgs) >= 1 {
if optArgs[0].NoReplyWait {
s.NoReplyWait()
}
}
if s.cluster != nil {
s.cluster.Close()
}
s.cluster = nil
s.closed = true
return nil
}
// SetMaxIdleConns sets the maximum number of connections in the idle
// connection pool.
func (s *Session) SetMaxIdleConns(n int) {
s.opts.MaxIdle = n
s.cluster.SetMaxIdleConns(n)
}
// SetMaxOpenConns sets the maximum number of open connections to the database.
func (s *Session) SetMaxOpenConns(n int) {
s.opts.MaxOpen = n
s.cluster.SetMaxOpenConns(n)
}
// NoReplyWait ensures that previous queries with the noreply flag have been
// processed by the server. Note that this guarantee only applies to queries
// run on the given connection
func (s *Session) NoReplyWait() error {
return s.cluster.Exec(Query{
Type: p.Query_NOREPLY_WAIT,
})
}
// Use changes the default database used
func (s *Session) Use(database string) {
s.opts.Database = database
}
// Query executes a ReQL query using the session to connect to the database
func (s *Session) Query(q Query) (*Cursor, error) {
return s.cluster.Query(q)
}
// Exec executes a ReQL query using the session to connect to the database
func (s *Session) Exec(q Query) error {
return s.cluster.Exec(q)
}
// SetHosts resets the hosts used when connecting to the RethinkDB cluster
func (s *Session) SetHosts(hosts []Host) {
s.hosts = hosts
}
func (s *Session) newQuery(t Term, opts map[string]interface{}) Query {
return newQuery(t, opts, s.opts)
}