-
Notifications
You must be signed in to change notification settings - Fork 36
/
conn.go
303 lines (257 loc) · 8.35 KB
/
conn.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
294
295
296
297
298
299
300
301
302
303
package gorqlite
/*
this file contains some high-level Connection-oriented stuff
*/
/* *****************************************************************
imports
* *****************************************************************/
import (
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
nurl "net/url"
)
const (
defaultTimeout = 10
defaultDisableClusterDiscovery = false
)
var (
// ErrClosed indicates that client connection was closed
ErrClosed = errors.New("gorqlite: connection is closed")
traceOut io.Writer
)
// defaults to false. This is used in trace() to quickly
// return if tracing is off, so that we don't do a perhaps
// expensive Sprintf() call only to send it to Discard
var wantsTrace bool
/* *****************************************************************
type: Connection
* *****************************************************************/
// Connection provides the connection abstraction.
// Note that since rqlite is stateless, there really is no "connection".
// However, this type holds information such as the current leader, peers,
// connection string to build URLs, etc.
//
// Connections are assigned a "connection ID" which is a pseudo-UUID
// for connection identification in trace output only. This helps
// sort out what's going on if you have multiple connections going
// at once. It's generated using a non-standards-or-anything-else-compliant
// function that uses crypto/rand to generate 16 random bytes.
//
// Note that the Connection objection holds info on all peers, gathered
// at time of Open() from the node specified.
type Connection struct {
cluster rqliteCluster
// name type default
username string // username or ""
password string // username or ""
consistencyLevel consistencyLevel // WEAK
disableClusterDiscovery bool // false unless user states otherwise
wantsHTTPS bool // false unless connection URL is https
wantsTransactions bool // true unless user states otherwise
wantsQueueing bool // perform queued writes
// variables below this line need to be initialized in Open()
hasBeenClosed bool // false
ID string // generated in init()
client http.Client
}
// Close will mark the connection as closed. It is safe to be called
// multiple times.
func (conn *Connection) Close() {
conn.hasBeenClosed = true
trace("%s: %s", conn.ID, "closing connection")
}
// ConsistencyLevel tells the current consistency level
func (conn *Connection) ConsistencyLevel() (string, error) {
if conn.hasBeenClosed {
return "", ErrClosed
}
return consistencyLevelToString[conn.consistencyLevel], nil
}
// Leader tells the current leader of the cluster
func (conn *Connection) Leader() (string, error) {
if conn.hasBeenClosed {
return "", ErrClosed
}
if conn.disableClusterDiscovery {
return string(conn.cluster.leader), nil
}
trace("%s: Leader(), calling updateClusterInfo()", conn.ID)
err := conn.updateClusterInfo()
if err != nil {
trace("%s: Leader() got error from updateClusterInfo(): %s", conn.ID, err.Error())
return "", err
} else {
trace("%s: Leader(), updateClusterInfo() OK", conn.ID)
}
return string(conn.cluster.leader), nil
}
// Peers tells the current peers of the cluster
func (conn *Connection) Peers() ([]string, error) {
if conn.hasBeenClosed {
var ans []string
return ans, ErrClosed
}
plist := make([]string, 0)
if conn.disableClusterDiscovery {
for _, p := range conn.cluster.peerList {
plist = append(plist, string(p))
}
return plist, nil
}
trace("%s: Peers(), calling updateClusterInfo()", conn.ID)
err := conn.updateClusterInfo()
if err != nil {
trace("%s: Peers() got error from updateClusterInfo(): %s", conn.ID, err.Error())
return plist, err
} else {
trace("%s: Peers(), updateClusterInfo() OK", conn.ID)
}
if conn.cluster.leader != "" {
plist = append(plist, string(conn.cluster.leader))
}
for _, p := range conn.cluster.otherPeers {
plist = append(plist, string(p))
}
return plist, nil
}
func (conn *Connection) SetConsistencyLevel(levelDesired consistencyLevel) error {
if conn.hasBeenClosed {
return ErrClosed
}
if levelDesired < ConsistencyLevelNone || levelDesired > ConsistencyLevelStrong {
return fmt.Errorf("unknown consistency level: %d", levelDesired)
}
conn.consistencyLevel = levelDesired
return nil
}
func (conn *Connection) SetExecutionWithTransaction(state bool) error {
if conn.hasBeenClosed {
return ErrClosed
}
conn.wantsTransactions = state
return nil
}
// initConnection takes the initial connection URL specified by
// the user, and parses it into a peer. This peer is assumed to
// be the leader. The next thing Open() does is updateClusterInfo()
// so the truth will be revealed soon enough.
//
// initConnection() does not talk to rqlite. It only parses the
// connection URL and prepares the new connection for work.
//
// URL format:
//
// http[s]://${USER}:${PASSWORD}@${HOSTNAME}:${PORT}/db?[OPTIONS]
//
// Examples:
//
// https://mary:secret2@localhost:4001/db
// https://mary:[email protected]:4001/db?level=none
// https://mary:[email protected]:4001/db?level=weak
// https://mary:secret2@localhost:2265/db?level=strong
//
// to use default connection to localhost:4001 with no auth:
//
// http://
// https://
//
// guaranteed map fields - will be set to "" if not specified
//
// field name default if not specified
//
// username ""
// password ""
// hostname "localhost"
// port "4001"
// consistencyLevel "weak"
func (conn *Connection) initConnection(url string) error {
// do some sanity checks. You know users.
if len(url) < 7 {
return errors.New("url specified is impossibly short")
}
if !strings.HasPrefix(url, "http") {
return errors.New("url does not start with 'http'")
}
u, err := nurl.Parse(url)
if err != nil {
return err
}
trace("%s: net.url.Parse() OK", conn.ID)
if u.Scheme == "https" {
conn.wantsHTTPS = true
}
// specs say Username() is always populated even if empty
if u.User == nil {
conn.username = ""
conn.password = ""
} else {
// guaranteed, but could be empty which is ok
conn.username = u.User.Username()
// not guaranteed, so test if set
pass, isset := u.User.Password()
if isset {
conn.password = pass
} else {
conn.password = ""
}
}
if u.Host == "" {
conn.cluster.leader = "localhost:4001"
} else {
conn.cluster.leader = peer(u.Host)
}
conn.cluster.peerList = []peer{conn.cluster.leader}
// at the moment, the only allowed query is "level=" with
// the desired consistency level
// default
conn.consistencyLevel = ConsistencyLevelWeak
// parse query params
query := u.Query()
if query.Get("level") != "" {
cl, err := ParseConsistencyLevel(query.Get("level"))
if err != nil {
return fmt.Errorf("invalid consistency level: %s %w", query.Get("level"), err)
}
conn.consistencyLevel = cl
}
conn.disableClusterDiscovery = defaultDisableClusterDiscovery
if query.Get("disableClusterDiscovery") != "" {
dpd, err := strconv.ParseBool(query.Get("disableClusterDiscovery"))
if err != nil {
return errors.New("invalid disableClusterDiscovery value: " + err.Error())
}
conn.disableClusterDiscovery = dpd
}
timeout := defaultTimeout
if query.Get("timeout") != "" {
customTimeout, err := strconv.Atoi(query.Get("timeout"))
if err != nil {
return errors.New("invalid timeout specified: " + err.Error())
}
timeout = customTimeout
}
// Default transaction state
conn.wantsTransactions = true
// Initialize http client for connection
conn.client = http.Client{
Timeout: time.Second * time.Duration(timeout),
}
trace("%s: parseDefaultPeer() is done:", conn.ID)
if conn.wantsHTTPS {
trace("%s: %s -> %s", conn.ID, "wants https?", "yes")
} else {
trace("%s: %s -> %s", conn.ID, "wants https?", "no")
}
trace("%s: %s -> %s", conn.ID, "username", conn.username)
trace("%s: %s -> %s", conn.ID, "password", conn.password)
trace("%s: %s -> %s", conn.ID, "host", conn.cluster.leader)
trace("%s: %s -> %s", conn.ID, "consistencyLevel", consistencyLevelToString[conn.consistencyLevel])
trace("%s: %s -> %s", conn.ID, "wantsTransaction", conn.wantsTransactions)
conn.cluster.conn = conn
return nil
}