@@ -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
292313func (c * Cluster ) connectNodeWithStatus (s nodeStatus ) (* Node , error ) {
0 commit comments