Merge pull request #10175 from dcantah/swap-to-newclient-grpc

Integration: Change to grpc.NewClient
This commit is contained in:
Maksym Pavlenko 2024-05-08 22:26:08 +00:00 committed by GitHub
commit a8e921ad06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 21 additions and 14 deletions

View File

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

View File

@ -33,7 +33,6 @@ limitations under the License.
package remote
import (
"context"
"errors"
"fmt"
"time"
@ -62,10 +61,7 @@ func NewImageService(endpoint string, connectionTimeout time.Duration) (internal
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.WithContextDialer(dialer),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)),

View File

@ -70,10 +70,8 @@ func NewRuntimeService(endpoint string, connectionTimeout time.Duration) (intern
if err != nil {
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.WithContextDialer(dialer),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)),

View File

@ -36,6 +36,7 @@ package util
import (
"context"
"errors"
"fmt"
"net"
"net/url"
@ -99,10 +100,16 @@ func GetAddressAndDialer(endpoint string) (string, func(ctx context.Context, add
return "", nil, err
}
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) {

View File

@ -34,6 +34,7 @@ package util
import (
"context"
"errors"
"fmt"
"net"
"net/url"
@ -75,6 +76,13 @@ func GetAddressAndDialer(endpoint string) (string, func(ctx context.Context, add
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 {
return addr, tcpDial, nil
}
@ -83,7 +91,7 @@ func GetAddressAndDialer(endpoint string) (string, func(ctx context.Context, add
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) {