Skip to content

Commit b61418c

Browse files
rpc: add generic dial methods to dial RPC clients
Introduce generic dial methods for creating RPC clients across services. This consolidates code paths, reduces the number of places where the DRPC experimental flag must be enabled, and simplifies adding version guards by limiting them to only a few locations. Epic: CRDB-48935 Informs: None Release note: None
1 parent 5dc1548 commit b61418c

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

pkg/rpc/nodedialer/nodedialer.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,32 @@ func (c *tracingInternalClient) Batch(
430430
}
431431
return c.RestrictedInternalClient.Batch(ctx, ba)
432432
}
433+
434+
// DialRPCClient establishes a connection to a node identified by its ID and
435+
// returns a client for the requested service type. When DRPC is enabled, it
436+
// creates a DRPC client; otherwise, it falls back to a gRPC client.
437+
func DialRPCClient[C any](
438+
nd *Dialer,
439+
ctx context.Context,
440+
nodeID roachpb.NodeID,
441+
class rpcbase.ConnectionClass,
442+
grpcClientFn func(*grpc.ClientConn) C,
443+
drpcClientFn func(drpc.Conn) C,
444+
) (C, error) {
445+
return rpcbase.DialRPCClient(nd, ctx, nodeID, class, grpcClientFn,
446+
drpcClientFn, nd.rpcContext.Settings)
447+
}
448+
449+
// DialRPCClientNoBreaker is like DialRPCClient, but will not check the
450+
// circuit breaker before trying to connect.
451+
func DialRPCClientNoBreaker[C any](
452+
nd *Dialer,
453+
ctx context.Context,
454+
nodeID roachpb.NodeID,
455+
class rpcbase.ConnectionClass,
456+
grpcClientFn func(*grpc.ClientConn) C,
457+
drpcClientFn func(drpc.Conn) C,
458+
) (C, error) {
459+
return rpcbase.DialRPCClientNoBreaker(nd, ctx, nodeID, class, grpcClientFn,
460+
drpcClientFn, nd.rpcContext.Settings)
461+
}

pkg/rpc/rpcbase/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ go_library(
2626
"//pkg/roachpb",
2727
"//pkg/rpc/rpcpb",
2828
"//pkg/settings",
29+
"//pkg/settings/cluster",
2930
"//pkg/util/buildutil",
3031
"//pkg/util/envutil",
3132
"@com_github_cockroachdb_errors//:errors",

pkg/rpc/rpcbase/nodedialer.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/cockroachdb/cockroach/pkg/roachpb"
1212
"github.com/cockroachdb/cockroach/pkg/settings"
13+
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
1314
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
1415
"github.com/cockroachdb/cockroach/pkg/util/envutil"
1516
"github.com/cockroachdb/errors"
@@ -56,3 +57,62 @@ type NodeDialerNoBreaker interface {
5657
DialNoBreaker(context.Context, roachpb.NodeID, ConnectionClass) (_ *grpc.ClientConn, err error)
5758
DRPCDialNoBreaker(context.Context, roachpb.NodeID, ConnectionClass) (_ drpc.Conn, err error)
5859
}
60+
61+
// DialRPCClient establishes a connection to a node identified by its ID and
62+
// returns a client for the requested service type. When DRPC is enabled, it
63+
// creates a DRPC client; otherwise, it falls back to a gRPC client.
64+
func DialRPCClient[C any](
65+
nd NodeDialer,
66+
ctx context.Context,
67+
nodeID roachpb.NodeID,
68+
class ConnectionClass,
69+
grpcClientFn func(*grpc.ClientConn) C,
70+
drpcClientFn func(drpc.Conn) C,
71+
st *cluster.Settings,
72+
) (C, error) {
73+
useDRPC := ExperimentalDRPCEnabled.Get(&st.SV)
74+
75+
var nilC C
76+
if !TODODRPC && !useDRPC {
77+
conn, err := nd.Dial(ctx, nodeID, class)
78+
if err != nil {
79+
return nilC, err
80+
}
81+
return grpcClientFn(conn), nil
82+
}
83+
84+
conn, err := nd.DRPCDial(ctx, nodeID, class)
85+
if err != nil {
86+
return nilC, err
87+
}
88+
return drpcClientFn(conn), nil
89+
}
90+
91+
// DialRPCClientNoBreaker is like DialRPCClient, but will not check the
92+
// circuit breaker before trying to connect.
93+
func DialRPCClientNoBreaker[C any](
94+
nd NodeDialerNoBreaker,
95+
ctx context.Context,
96+
nodeID roachpb.NodeID,
97+
class ConnectionClass,
98+
grpcClientFn func(*grpc.ClientConn) C,
99+
drpcClientFn func(drpc.Conn) C,
100+
st *cluster.Settings,
101+
) (C, error) {
102+
useDRPC := ExperimentalDRPCEnabled.Get(&st.SV)
103+
104+
var nilC C
105+
if !TODODRPC && !useDRPC {
106+
conn, err := nd.DialNoBreaker(ctx, nodeID, class)
107+
if err != nil {
108+
return nilC, err
109+
}
110+
return grpcClientFn(conn), nil
111+
}
112+
113+
conn, err := nd.DRPCDialNoBreaker(ctx, nodeID, class)
114+
if err != nil {
115+
return nilC, err
116+
}
117+
return drpcClientFn(conn), nil
118+
}

pkg/server/testserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func makeTestConfigFromParams(params base.TestServerArgs) Config {
334334
}
335335

336336
if params.DefaultDRPCOption == base.TestDRPCEnabled {
337-
rpc.ExperimentalDRPCEnabled.Override(context.Background(), &st.SV, true)
337+
rpcbase.ExperimentalDRPCEnabled.Override(context.Background(), &st.SV, true)
338338
}
339339

340340
return cfg

pkg/testutils/serverutils/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ go_library(
3131
"//pkg/multitenant/tenantcapabilitiespb",
3232
"//pkg/roachpb",
3333
"//pkg/rpc",
34-
"//pkg/rpc/rpcbase",
3534
"//pkg/security/securitytest",
3635
"//pkg/security/username",
3736
"//pkg/server/decommissioning",

0 commit comments

Comments
 (0)