Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added calloption to retrieve peer information #1066

Merged
merged 5 commits into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"golang.org/x/net/context"
"golang.org/x/net/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/stats"
"google.golang.org/grpc/transport"
)
Expand Down Expand Up @@ -85,6 +86,9 @@ func recvResponse(ctx context.Context, dopts dialOptions, t transport.ClientTran
dopts.copts.StatsHandler.HandleRPC(ctx, inPayload)
}
c.trailerMD = stream.Trailer()
if peer, ok := peer.FromContext(stream.Context()); ok {
c.peer = *peer
}
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions rpc_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/stats"
"google.golang.org/grpc/transport"
)
Expand Down Expand Up @@ -141,6 +142,7 @@ type callInfo struct {
headerMD metadata.MD
trailerMD metadata.MD
traceInfo traceInfo // in trace.go
peer peer.Peer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this above traceInfo?

}

var defaultCallInfo = callInfo{failFast: true}
Expand Down Expand Up @@ -183,6 +185,13 @@ func Trailer(md *metadata.MD) CallOption {
})
}

// Peer returns a CallOption that retrieves peer information.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "for a unary RPC" at the end so it's consistent with Header and Trailer?

func Peer(peer *peer.Peer) CallOption {
return afterCall(func(c *callInfo) {
*peer = c.peer
})
}

// FailFast configures the action to take when an RPC is attempted on broken
// connections or unreachable servers. If failfast is true, the RPC will fail
// immediately. Otherwise, the RPC client will block the call until a
Expand Down
32 changes: 32 additions & 0 deletions test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,38 @@ func testMetadataUnaryRPC(t *testing.T, e env) {
}
}

func TestPeerClientSide(t *testing.T) {
defer leakCheck(t)()
for _, e := range listTestEnv() {
testPeerClientSide(t, e)
}
}

func testPeerClientSide(t *testing.T, e env) {
te := newTest(t, e)
te.userAgent = testAppUA
te.startServer(&testServer{security: e.security})
defer te.tearDown()
tc := testpb.NewTestServiceClient(te.clientConn())
peer := new(peer.Peer)
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.Peer(peer)); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add grpc.FailFast(false)?

t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
}
pa := extractPort(peer.Addr.String())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sa := extractPort(te.srvAddr)
if pa != sa {
t.Fatalf("peer.Addr = localhost:%v, want localhost:%v", pa, sa)
}
}

func extractPort(addr string) string {
sp := strings.Split(addr, ":")
if len(sp) < 2 {
return ""
}
return sp[1]
}

func TestMultipleSetTrailerUnaryRPC(t *testing.T) {
defer leakCheck(t)()
for _, e := range listTestEnv() {
Expand Down