Skip to content

Commit

Permalink
Changed Connect to return the reason for connections
Browse files Browse the repository at this point in the history
  • Loading branch information
dancannon committed May 22, 2016
1 parent 2834301 commit d529a24
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Changed
- Changed `Connect` to return the reason for connections failing (instead of just "no connections were made when creating the session")

### Fixed
- Fixed queries not being retried when using `Query()`, queries are now retried if the request failed due to a bad connection.
- Fixed `Cursor` methods panicking if using a nil cursor, please note that you should still always check if your queries return an error.
Expand Down
29 changes: 25 additions & 4 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ func NewCluster(hosts []Host, opts *ConnectOpts) (*Cluster, error) {
opts: opts,
}

//Check that hosts in the ClusterConfig is not empty
c.connectNodes(c.getSeeds())
// Attempt to connect to each host and discover any additional hosts if host
// discovery is enabled
if err := c.connectNodes(c.getSeeds()); err != nil {
return nil, err
}

if !c.IsConnected() {
return nil, ErrNoConnectionsStarted
}
Expand Down Expand Up @@ -206,17 +210,20 @@ func (c *Cluster) listenForNodeChanges() error {
return err
}

func (c *Cluster) connectNodes(hosts []Host) {
func (c *Cluster) connectNodes(hosts []Host) error {
// Add existing nodes to map
nodeSet := map[string]*Node{}
for _, node := range c.GetNodes() {
nodeSet[node.ID] = node
}

var attemptErr error

// Attempt to connect to each seed host
for _, host := range hosts {
conn, err := NewConnection(host.String(), c.opts)
if err != nil {
attemptErr = err
Log.Warnf("Error creating connection: %s", err.Error())
continue
}
Expand All @@ -235,13 +242,15 @@ func (c *Cluster) connectNodes(hosts []Host) {

_, cursor, err := conn.Query(q)
if err != nil {
attemptErr = err
Log.Warnf("Error fetching cluster status: %s", err)
continue
}

var results []nodeStatus
err = cursor.All(&results)
if err != nil {
attemptErr = err
continue
}

Expand All @@ -256,12 +265,14 @@ func (c *Cluster) connectNodes(hosts []Host) {
nodeSet[node.ID] = node
}
} else {
attemptErr = err
Log.Warnf("Error connecting to node: %s", err)
}
}
} else {
svrRsp, err := conn.Server()
if err != nil {
attemptErr = err
Log.Warnf("Error fetching server ID: %s", err)
continue
}
Expand All @@ -273,20 +284,30 @@ func (c *Cluster) connectNodes(hosts []Host) {
"id": node.ID,
"host": node.Host.String(),
}).Debug("Connected to node")

nodeSet[node.ID] = node
}
} else {
attemptErr = err
Log.Warnf("Error connecting to node: %s", err)
}
}
}

// If no nodes were contactable then return the last error, this does not
// include driver errors such as if there was an issue building the
// query
if len(nodeSet) == 0 {
return attemptErr
}

nodes := []*Node{}
for _, node := range nodeSet {
nodes = append(nodes, node)
}

c.setNodes(nodes)

return nil
}

func (c *Cluster) connectNodeWithStatus(s nodeStatus) (*Node, error) {
Expand Down
2 changes: 2 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ func (s *RethinkSuite) TestSessionConnectError(c *test.C) {
Address: "nonexistanturl",
Timeout: time.Second,
})

c.Assert(err, test.NotNil)
c.Assert(err, test.FitsTypeOf, RQLConnectionError{})
}

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

0 comments on commit d529a24

Please sign in to comment.