@@ -25,6 +25,7 @@ import (
25
25
"time"
26
26
27
27
"google.golang.org/grpc"
28
+ "google.golang.org/grpc/connectivity"
28
29
grpccredentials "google.golang.org/grpc/credentials"
29
30
"google.golang.org/grpc/credentials/insecure"
30
31
utilerrors "k8s.io/apimachinery/pkg/util/errors"
@@ -102,11 +103,7 @@ func (s *ServerConfig) NewServer() (*grpc.Server, error) {
102
103
103
104
// DialWithTimeOut will attempt to create a client connection based on the given targets, one at a time, until a client connection is successfully established.
104
105
func (c * ClientConfig ) DialWithTimeOut (paths []string , timeout time.Duration ) (* grpc.ClientConn , error ) {
105
- opts := []grpc.DialOption {
106
- // grpc.WithBlock is deprecated. TODO: Perhaps need to reconsider the approach in a future PR
107
- //nolint:staticcheck
108
- grpc .WithBlock (),
109
- }
106
+ var opts []grpc.DialOption
110
107
111
108
var cred grpccredentials.TransportCredentials
112
109
if c .ServerAuthCAFile == "" && ! c .InsecureSkipServerVerify {
@@ -136,9 +133,7 @@ func (c *ClientConfig) DialWithTimeOut(paths []string, timeout time.Duration) (*
136
133
}
137
134
cred = grpccredentials .NewTLS (config )
138
135
}
139
-
140
136
opts = append (opts , grpc .WithTransportCredentials (cred ))
141
-
142
137
var cc * grpc.ClientConn
143
138
var err error
144
139
var allErrs []error
@@ -147,7 +142,7 @@ func (c *ClientConfig) DialWithTimeOut(paths []string, timeout time.Duration) (*
147
142
if err == nil {
148
143
return cc , nil
149
144
}
150
- allErrs = append (allErrs , err )
145
+ allErrs = append (allErrs , fmt . Errorf ( "dial %s error: %v" , path , err ) )
151
146
}
152
147
153
148
return nil , utilerrors .NewAggregate (allErrs )
@@ -157,12 +152,27 @@ func createGRPCConnection(path string, timeout time.Duration, opts ...grpc.DialO
157
152
ctx , cancel := context .WithTimeout (context .Background (), timeout )
158
153
defer cancel ()
159
154
160
- // grpc.DialContext is deprecated. TODO: Perhaps need to reconsider the approach in a future PR
161
- //nolint:staticcheck
162
- cc , err := grpc .DialContext (ctx , path , opts ... )
155
+ cc , err := grpc .NewClient (path , opts ... )
163
156
if err != nil {
164
- return nil , fmt . Errorf ( "dial %s error: %v" , path , err )
157
+ return nil , err
165
158
}
159
+ defer func () {
160
+ if err != nil {
161
+ cc .Close ()
162
+ }
163
+ }()
166
164
167
- return cc , nil
165
+ // A blocking dial blocks until the clientConn is ready.
166
+ for {
167
+ state := cc .GetState ()
168
+ if state == connectivity .Idle {
169
+ cc .Connect ()
170
+ }
171
+ if state == connectivity .Ready {
172
+ return cc , nil
173
+ }
174
+ if ! cc .WaitForStateChange (ctx , state ) {
175
+ return nil , fmt .Errorf ("timeout waiting for connection to %s, state is %s" , path , state )
176
+ }
177
+ }
168
178
}
0 commit comments