@@ -4,25 +4,20 @@ import (
44 "context"
55 "crypto/tls"
66 "crypto/x509"
7- "errors"
87 "fmt"
98 "runtime"
109 "time"
1110
1211 "github.com/cenkalti/backoff/v4"
1312 log "github.com/sirupsen/logrus"
1413 "google.golang.org/grpc"
15- "google.golang.org/grpc/connectivity"
1614 "google.golang.org/grpc/credentials"
1715 "google.golang.org/grpc/credentials/insecure"
1816 "google.golang.org/grpc/keepalive"
1917
2018 "github.com/netbirdio/netbird/util/embeddedroots"
2119)
2220
23- // ErrConnectionShutdown indicates that the connection entered shutdown state before becoming ready
24- var ErrConnectionShutdown = errors .New ("connection shutdown before ready" )
25-
2621// Backoff returns a backoff configuration for gRPC calls
2722func Backoff (ctx context.Context ) backoff.BackOff {
2823 b := backoff .NewExponentialBackOff ()
@@ -31,26 +26,6 @@ func Backoff(ctx context.Context) backoff.BackOff {
3126 return backoff .WithContext (b , ctx )
3227}
3328
34- // waitForConnectionReady blocks until the connection becomes ready or fails.
35- // Returns an error if the connection times out, is cancelled, or enters shutdown state.
36- func waitForConnectionReady (ctx context.Context , conn * grpc.ClientConn ) error {
37- conn .Connect ()
38-
39- state := conn .GetState ()
40- for state != connectivity .Ready && state != connectivity .Shutdown {
41- if ! conn .WaitForStateChange (ctx , state ) {
42- return fmt .Errorf ("wait state change from %s: %w" , state , ctx .Err ())
43- }
44- state = conn .GetState ()
45- }
46-
47- if state == connectivity .Shutdown {
48- return ErrConnectionShutdown
49- }
50-
51- return nil
52- }
53-
5429// CreateConnection creates a gRPC client connection with the appropriate transport options.
5530// The component parameter specifies the WebSocket proxy component path (e.g., "/management", "/signal").
5631func CreateConnection (ctx context.Context , addr string , tlsEnabled bool , component string ) (* grpc.ClientConn , error ) {
@@ -68,25 +43,22 @@ func CreateConnection(ctx context.Context, addr string, tlsEnabled bool, compone
6843 }))
6944 }
7045
71- conn , err := grpc .NewClient (
46+ connCtx , cancel := context .WithTimeout (ctx , 30 * time .Second )
47+ defer cancel ()
48+
49+ conn , err := grpc .DialContext (
50+ connCtx ,
7251 addr ,
7352 transportOption ,
7453 WithCustomDialer (tlsEnabled , component ),
54+ grpc .WithBlock (),
7555 grpc .WithKeepaliveParams (keepalive.ClientParameters {
7656 Time : 30 * time .Second ,
7757 Timeout : 10 * time .Second ,
7858 }),
7959 )
8060 if err != nil {
81- return nil , fmt .Errorf ("new client: %w" , err )
82- }
83-
84- ctx , cancel := context .WithTimeout (ctx , 30 * time .Second )
85- defer cancel ()
86-
87- if err := waitForConnectionReady (ctx , conn ); err != nil {
88- _ = conn .Close ()
89- return nil , err
61+ return nil , fmt .Errorf ("dial context: %w" , err )
9062 }
9163
9264 return conn , nil
0 commit comments