Pārlūkot izejas kodu

Merge pull request #652 from Zariel/retry-dials

try a few times to connect to a host
Chris Bannister 9 gadi atpakaļ
vecāks
revīzija
2a72f6c4f3
1 mainītis faili ar 13 papildinājumiem un 3 dzēšanām
  1. 13 3
      connectionpool.go

+ 13 - 3
connectionpool.go

@@ -499,16 +499,26 @@ func (pool *hostConnPool) connectMany(count int) {
 }
 
 // create a new connection to the host and add it to the pool
-func (pool *hostConnPool) connect() error {
+func (pool *hostConnPool) connect() (err error) {
+	// TODO: provide a more robust connection retry mechanism, we should also
+	// be able to detect hosts that come up by trying to connect to downed ones.
+	const maxAttempts = 3
 	// try to connect
-	conn, err := pool.session.connect(pool.addr, pool)
+	var conn *Conn
+	for i := 0; i < maxAttempts; i++ {
+		conn, err = pool.session.connect(pool.addr, pool)
+		if err == nil {
+			break
+		}
+	}
+
 	if err != nil {
 		return err
 	}
 
 	if pool.keyspace != "" {
 		// set the keyspace
-		if err := conn.UseKeyspace(pool.keyspace); err != nil {
+		if err = conn.UseKeyspace(pool.keyspace); err != nil {
 			conn.Close()
 			return err
 		}