Skip to content

Commit d529a24

Browse files
committed
Changed Connect to return the reason for connections
1 parent 2834301 commit d529a24

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## Unreleased
66

7+
### Changed
8+
- Changed `Connect` to return the reason for connections failing (instead of just "no connections were made when creating the session")
9+
710
### Fixed
811
- Fixed queries not being retried when using `Query()`, queries are now retried if the request failed due to a bad connection.
912
- Fixed `Cursor` methods panicking if using a nil cursor, please note that you should still always check if your queries return an error.

cluster.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ func NewCluster(hosts []Host, opts *ConnectOpts) (*Cluster, error) {
3939
opts: opts,
4040
}
4141

42-
//Check that hosts in the ClusterConfig is not empty
43-
c.connectNodes(c.getSeeds())
42+
// Attempt to connect to each host and discover any additional hosts if host
43+
// discovery is enabled
44+
if err := c.connectNodes(c.getSeeds()); err != nil {
45+
return nil, err
46+
}
47+
4448
if !c.IsConnected() {
4549
return nil, ErrNoConnectionsStarted
4650
}
@@ -206,17 +210,20 @@ func (c *Cluster) listenForNodeChanges() error {
206210
return err
207211
}
208212

209-
func (c *Cluster) connectNodes(hosts []Host) {
213+
func (c *Cluster) connectNodes(hosts []Host) error {
210214
// Add existing nodes to map
211215
nodeSet := map[string]*Node{}
212216
for _, node := range c.GetNodes() {
213217
nodeSet[node.ID] = node
214218
}
215219

220+
var attemptErr error
221+
216222
// Attempt to connect to each seed host
217223
for _, host := range hosts {
218224
conn, err := NewConnection(host.String(), c.opts)
219225
if err != nil {
226+
attemptErr = err
220227
Log.Warnf("Error creating connection: %s", err.Error())
221228
continue
222229
}
@@ -235,13 +242,15 @@ func (c *Cluster) connectNodes(hosts []Host) {
235242

236243
_, cursor, err := conn.Query(q)
237244
if err != nil {
245+
attemptErr = err
238246
Log.Warnf("Error fetching cluster status: %s", err)
239247
continue
240248
}
241249

242250
var results []nodeStatus
243251
err = cursor.All(&results)
244252
if err != nil {
253+
attemptErr = err
245254
continue
246255
}
247256

@@ -256,12 +265,14 @@ func (c *Cluster) connectNodes(hosts []Host) {
256265
nodeSet[node.ID] = node
257266
}
258267
} else {
268+
attemptErr = err
259269
Log.Warnf("Error connecting to node: %s", err)
260270
}
261271
}
262272
} else {
263273
svrRsp, err := conn.Server()
264274
if err != nil {
275+
attemptErr = err
265276
Log.Warnf("Error fetching server ID: %s", err)
266277
continue
267278
}
@@ -273,20 +284,30 @@ func (c *Cluster) connectNodes(hosts []Host) {
273284
"id": node.ID,
274285
"host": node.Host.String(),
275286
}).Debug("Connected to node")
287+
276288
nodeSet[node.ID] = node
277289
}
278290
} else {
291+
attemptErr = err
279292
Log.Warnf("Error connecting to node: %s", err)
280293
}
281294
}
282295
}
283296

297+
// If no nodes were contactable then return the last error, this does not
298+
// include driver errors such as if there was an issue building the
299+
// query
300+
if len(nodeSet) == 0 {
301+
return attemptErr
302+
}
303+
284304
nodes := []*Node{}
285305
for _, node := range nodeSet {
286306
nodes = append(nodes, node)
287307
}
288-
289308
c.setNodes(nodes)
309+
310+
return nil
290311
}
291312

292313
func (c *Cluster) connectNodeWithStatus(s nodeStatus) (*Node, error) {

session_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ func (s *RethinkSuite) TestSessionConnectError(c *test.C) {
8585
Address: "nonexistanturl",
8686
Timeout: time.Second,
8787
})
88+
8889
c.Assert(err, test.NotNil)
90+
c.Assert(err, test.FitsTypeOf, RQLConnectionError{})
8991
}
9092

9193
func (s *RethinkSuite) TestSessionClose(c *test.C) {

0 commit comments

Comments
 (0)