Skip to content

Commit

Permalink
add header metadata to PickOptions (grpc#2376)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley authored Oct 12, 2018
1 parent 1ca9df5 commit 5b2c343
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions balancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ type PickOptions struct {
// FullMethodName is the method name that NewClientStream() is called
// with. The canonical format is /service/Method.
FullMethodName string
// Header contains the metadata from the RPC's client header.
Header metadata.MD
}

// DoneInfo contains additional information for done.
Expand Down
3 changes: 3 additions & 0 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"google.golang.org/grpc/internal/channelz"
"google.golang.org/grpc/internal/transport"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/resolver"
_ "google.golang.org/grpc/resolver/dns" // To register dns resolver.
_ "google.golang.org/grpc/resolver/passthrough" // To register passthrough resolver.
Expand Down Expand Up @@ -716,8 +717,10 @@ func (cc *ClientConn) GetMethodConfig(method string) MethodConfig {
}

func (cc *ClientConn) getTransport(ctx context.Context, failfast bool, method string) (transport.ClientTransport, func(balancer.DoneInfo), error) {
hdr, _ := metadata.FromOutgoingContext(ctx)
t, done, err := cc.blockingpicker.pick(ctx, failfast, balancer.PickOptions{
FullMethodName: method,
Header: hdr,
})
if err != nil {
return nil, nil, toRPCErr(err)
Expand Down
19 changes: 16 additions & 3 deletions test/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/internal/leakcheck"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/resolver"
testpb "google.golang.org/grpc/test/grpc_testing"
"google.golang.org/grpc/testdata"
Expand All @@ -46,6 +47,7 @@ type testBalancer struct {
sc balancer.SubConn

newSubConnOptions balancer.NewSubConnOptions
pickOptions []balancer.PickOptions
doneInfo []balancer.DoneInfo
}

Expand Down Expand Up @@ -102,6 +104,7 @@ type picker struct {
}

func (p *picker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
p.bal.pickOptions = append(p.bal.pickOptions, opts)
if p.err != nil {
return nil, nil, p.err
}
Expand Down Expand Up @@ -137,14 +140,14 @@ func TestCredsBundleFromBalancer(t *testing.T) {
}
}

func TestDoneInfo(t *testing.T) {
func TestPickAndDone(t *testing.T) {
defer leakcheck.Check(t)
for _, e := range listTestEnv() {
testDoneInfo(t, e)
testPickAndDone(t, e)
}
}

func testDoneInfo(t *testing.T, e env) {
func testPickAndDone(t *testing.T, e env) {
te := newTest(t, e)
b := &testBalancer{}
balancer.Register(b)
Expand All @@ -164,10 +167,20 @@ func testDoneInfo(t *testing.T, e env) {
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); !reflect.DeepEqual(err, wantErr) {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %v", err, wantErr)
}
md := metadata.Pairs("testMDKey", "testMDVal")
ctx = metadata.NewOutgoingContext(ctx, md)
if _, err := tc.UnaryCall(ctx, &testpb.SimpleRequest{}); err != nil {
t.Fatalf("TestService.UnaryCall(%v, _, _, _) = _, %v; want _, <nil>", ctx, err)
}

poWant := []balancer.PickOptions{
{FullMethodName: "/grpc.testing.TestService/EmptyCall"},
{FullMethodName: "/grpc.testing.TestService/UnaryCall", Header: md},
}
if !reflect.DeepEqual(b.pickOptions, poWant) {
t.Fatalf("b.pickOptions = %v; want %v", b.pickOptions, poWant)
}

if len(b.doneInfo) < 1 || !reflect.DeepEqual(b.doneInfo[0].Err, wantErr) {
t.Fatalf("b.doneInfo = %v; want b.doneInfo[0].Err = %v", b.doneInfo, wantErr)
}
Expand Down

0 comments on commit 5b2c343

Please sign in to comment.