Skip to content

Commit 2c586db

Browse files
committed
feat: pass fullMethodName to GetConnection
This provides a way to make decision on passing requests based on the method name. Signed-off-by: Andrey Smirnov <[email protected]>
1 parent 6dfa2cc commit 2c586db

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/talos-systems/grpc-proxy
22

3-
go 1.18
3+
go 1.19
44

55
require (
66
github.com/golang/protobuf v1.5.2

proxy/director.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Backend interface {
2727
// The context returned from this function should be the context for the *outgoing* (to backend) call. In case you want
2828
// to forward any Metadata between the inbound request and outbound requests, you should do it manually. However, you
2929
// *must* propagate the cancel function (`context.WithCancel`) of the inbound context to the one returned.
30-
GetConnection(ctx context.Context) (context.Context, *grpc.ClientConn, error)
30+
GetConnection(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error)
3131

3232
// AppendInfo is called to enhance response from the backend with additional data.
3333
//
@@ -71,7 +71,7 @@ func (sb *SingleBackend) String() string {
7171
}
7272

7373
// GetConnection returns a grpc connection to the backend.
74-
func (sb *SingleBackend) GetConnection(ctx context.Context) (context.Context, *grpc.ClientConn, error) {
74+
func (sb *SingleBackend) GetConnection(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
7575
return sb.GetConn(ctx)
7676
}
7777

@@ -88,11 +88,11 @@ func (sb *SingleBackend) BuildError(streaming bool, err error) ([]byte, error) {
8888
// StreamDirector returns a list of Backend objects to forward the call to.
8989
//
9090
// There are two proxying modes:
91-
// 1. one to one: StreamDirector returns a single Backend object - proxying is done verbatim, Backend.AppendInfo might
92-
// be used to enhance response with source information (or it might be skipped).
93-
// 2. one to many: StreamDirector returns more than one Backend object - for unary calls responses from Backend objects
94-
// are aggregated by concatenating protobuf responses (requires top-level `repeated` protobuf definition) and errors
95-
// are wrapped as responses via BuildError. Responses are potentially enhanced via AppendInfo.
91+
// 1. one to one: StreamDirector returns a single Backend object - proxying is done verbatim, Backend.AppendInfo might
92+
// be used to enhance response with source information (or it might be skipped).
93+
// 2. one to many: StreamDirector returns more than one Backend object - for unary calls responses from Backend objects
94+
// are aggregated by concatenating protobuf responses (requires top-level `repeated` protobuf definition) and errors
95+
// are wrapped as responses via BuildError. Responses are potentially enhanced via AppendInfo.
9696
//
9797
// The presence of the `Context` allows for rich filtering, e.g. based on Metadata (headers).
9898
// If no handling is meant to be done, a `codes.NotImplemented` gRPC error should be returned.

proxy/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s *handler) handler(srv interface{}, serverStream grpc.ServerStream) error
6363

6464
// We require that the backend's returned context inherits from the serverStream.Context().
6565
var outgoingCtx context.Context
66-
outgoingCtx, backendConnections[i].backendConn, backendConnections[i].connError = backends[i].GetConnection(clientCtx)
66+
outgoingCtx, backendConnections[i].backendConn, backendConnections[i].connError = backends[i].GetConnection(clientCtx, fullMethodName)
6767

6868
if backendConnections[i].connError != nil {
6969
continue

proxy/handler_one2many_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (b *assertingBackend) String() string {
152152
return fmt.Sprintf("backend%d", b.i)
153153
}
154154

155-
func (b *assertingBackend) GetConnection(ctx context.Context) (context.Context, *grpc.ClientConn, error) {
155+
func (b *assertingBackend) GetConnection(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
156156
md, _ := metadata.FromIncomingContext(ctx)
157157
// Explicitly copy the metadata, otherwise the tests will fail.
158158
outCtx := metadata.NewOutgoingContext(ctx, md.Copy())

0 commit comments

Comments
 (0)