@@ -10,6 +10,7 @@ import (
10
10
11
11
"github.com/cockroachdb/cockroach/pkg/roachpb"
12
12
"github.com/cockroachdb/cockroach/pkg/settings"
13
+ "github.com/cockroachdb/cockroach/pkg/settings/cluster"
13
14
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
14
15
"github.com/cockroachdb/cockroach/pkg/util/envutil"
15
16
"github.com/cockroachdb/errors"
@@ -56,3 +57,62 @@ type NodeDialerNoBreaker interface {
56
57
DialNoBreaker (context.Context , roachpb.NodeID , ConnectionClass ) (_ * grpc.ClientConn , err error )
57
58
DRPCDialNoBreaker (context.Context , roachpb.NodeID , ConnectionClass ) (_ drpc.Conn , err error )
58
59
}
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
+ }
0 commit comments