Skip to content

Commit 9f60831

Browse files
authored
Merge pull request kubernetes-csi#29 from lpabon/connection
More reliable connections
2 parents 29defa1 + 1138d3d commit 9f60831

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

utils/grpcutil.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ limitations under the License.
1717
package utils
1818

1919
import (
20+
"context"
21+
"fmt"
2022
"net"
2123
"net/url"
2224
"time"
2325

2426
"google.golang.org/grpc"
27+
"google.golang.org/grpc/connectivity"
2528
)
2629

2730
// Connect address by grpc
@@ -38,5 +41,19 @@ func Connect(address string) (*grpc.ClientConn, error) {
3841
}))
3942
}
4043

41-
return grpc.Dial(address, dialOptions...)
44+
conn, err := grpc.Dial(address, dialOptions...)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
50+
defer cancel()
51+
for {
52+
if !conn.WaitForStateChange(ctx, conn.GetState()) {
53+
return conn, fmt.Errorf("Connection timed out")
54+
}
55+
if conn.GetState() == connectivity.Ready {
56+
return conn, nil
57+
}
58+
}
4259
}

0 commit comments

Comments
 (0)