-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -141,6 +142,7 @@ type callInfo struct { | |
headerMD metadata.MD | ||
trailerMD metadata.MD | ||
traceInfo traceInfo // in trace.go | ||
peer peer.Peer | ||
} | ||
|
||
var defaultCallInfo = callInfo{failFast: true} | ||
|
@@ -183,6 +185,13 @@ func Trailer(md *metadata.MD) CallOption { | |
}) | ||
} | ||
|
||
// Peer returns a CallOption that retrieves peer information. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use net.SplitHostPort? |
||
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() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this above traceInfo?