Integration: Change to grpc.NewClient

Fun times: In grpc 1.63 grpc.Dial and a few of the options we use (WithBlock) are
deprecated in favor of the no-IO variant NewClient. The uses in the integration tests
should be easy to swap however as they don't use WithBlock anyways, so that's what this
change aims to do. This also removes some context.WithTimeout's as I don't see anywhere
the context is actually used in Dial if you don't also specify WithBlock (and it's
especially not used now with NewClient as it doesn't even take in a context).

Signed-off-by: Danny Canter <danny@dcantah.dev>
This commit is contained in:
Danny Canter 2024-05-06 02:17:35 -07:00
parent a26c686ea2
commit 9ecfac7f6a
5 changed files with 21 additions and 14 deletions

View File

@ -656,9 +656,7 @@ func RawRuntimeClient() (runtime.RuntimeServiceClient, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get dialer: %w", err) return nil, fmt.Errorf("failed to get dialer: %w", err)
} }
ctx, cancel := context.WithTimeout(context.Background(), timeout) conn, err := grpc.NewClient(addr,
defer cancel()
conn, err := grpc.DialContext(ctx, addr,
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(dialer), grpc.WithContextDialer(dialer),
) )

View File

@ -33,7 +33,6 @@ limitations under the License.
package remote package remote
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"time" "time"
@ -62,10 +61,7 @@ func NewImageService(endpoint string, connectionTimeout time.Duration) (internal
return nil, err return nil, err
} }
ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout) conn, err := grpc.NewClient(addr,
defer cancel()
conn, err := grpc.DialContext(ctx, addr,
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(dialer), grpc.WithContextDialer(dialer),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)),

View File

@ -70,10 +70,8 @@ func NewRuntimeService(endpoint string, connectionTimeout time.Duration) (intern
if err != nil { if err != nil {
return nil, err return nil, err
} }
ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout)
defer cancel()
conn, err := grpc.DialContext(ctx, addr, conn, err := grpc.NewClient(addr,
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(dialer), grpc.WithContextDialer(dialer),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)),

View File

@ -36,6 +36,7 @@ package util
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net" "net"
"net/url" "net/url"
@ -99,10 +100,16 @@ func GetAddressAndDialer(endpoint string) (string, func(ctx context.Context, add
return "", nil, err return "", nil, err
} }
if protocol != unixProtocol { if protocol != unixProtocol {
return "", nil, fmt.Errorf("only support unix socket endpoint") return "", nil, errors.New("only support unix socket endpoint")
} }
return addr, dial, nil // Use passthrough as the scheme so it allows us to use our custom dialer:
//
// "grpc.Dial uses "passthrough" as the default name resolver for backward compatibility while grpc.NewClient
// uses "dns" as its default name resolver. This subtle difference is important to legacy systems that also
// specified a custom dialer and expected it to receive the target string directly."
// https://github.com/grpc/grpc-go/blob/master/Documentation/anti-patterns.md#the-wrong-way-grpcdial
return fmt.Sprintf("passthrough:///%s", addr), dial, nil
} }
func dial(ctx context.Context, addr string) (net.Conn, error) { func dial(ctx context.Context, addr string) (net.Conn, error) {

View File

@ -34,6 +34,7 @@ package util
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net" "net"
"net/url" "net/url"
@ -75,6 +76,13 @@ func GetAddressAndDialer(endpoint string) (string, func(ctx context.Context, add
return "", nil, err return "", nil, err
} }
// Use passthrough as the scheme so it allows us to use our custom dialer:
//
// "grpc.Dial uses "passthrough" as the default name resolver for backward compatibility while grpc.NewClient
// uses "dns" as its default name resolver. This subtle difference is important to legacy systems that also
// specified a custom dialer and expected it to receive the target string directly."
// https://github.com/grpc/grpc-go/blob/master/Documentation/anti-patterns.md#the-wrong-way-grpcdial
addr = fmt.Sprintf("passthrough:///%s", addr)
if protocol == tcpProtocol { if protocol == tcpProtocol {
return addr, tcpDial, nil return addr, tcpDial, nil
} }
@ -83,7 +91,7 @@ func GetAddressAndDialer(endpoint string) (string, func(ctx context.Context, add
return addr, npipeDial, nil return addr, npipeDial, nil
} }
return "", nil, fmt.Errorf("only support tcp and npipe endpoint") return "", nil, errors.New("only support tcp and npipe endpoint")
} }
func tcpDial(ctx context.Context, addr string) (net.Conn, error) { func tcpDial(ctx context.Context, addr string) (net.Conn, error) {