Skip to content

Add LocalAddr to peer.Peer (#6577) #6702

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions balancer/leastrequest/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,18 @@ func checkRoundRobinRPCs(ctx context.Context, client testgrpc.TestServiceClient,
gotAddrCount = make(map[string]int)
// Perform 3 iterations.
var iterations [][]string
var localIterations [][]string
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert this file. We don't need to add these assertions here.

for i := 0; i < 3; i++ {
iteration := make([]string, len(addrs))
localIteration := make([]string, len(addrs))
for c := 0; c < len(addrs); c++ {
var peer peer.Peer
client.EmptyCall(ctx, &testpb.Empty{}, grpc.Peer(&peer))
iteration[c] = peer.Addr.String()
localIteration[c] = peer.LocalAddr.String()
}
iterations = append(iterations, iteration)
localIterations = append(localIterations, localIteration)
}
// Ensure the the first iteration contains all addresses in addrs.
for _, addr := range iterations[0] {
Expand All @@ -181,6 +185,10 @@ func checkRoundRobinRPCs(ctx context.Context, client testgrpc.TestServiceClient,
if !cmp.Equal(iterations[0], iterations[1]) || !cmp.Equal(iterations[0], iterations[2]) {
continue
}
// Ensure all three iterations contain the same local addresses.
if !cmp.Equal(localIterations[0], localIterations[1]) || !cmp.Equal(localIterations[0], localIterations[2]) {
continue
}
return nil
}
return fmt.Errorf("timeout when waiting for roundrobin distribution of RPCs across addresses: %v; got: %v", addrs, gotAddrCount)
Expand Down
5 changes: 3 additions & 2 deletions internal/transport/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,9 @@ func (t *http2Client) newStream(ctx context.Context, callHdr *CallHdr) *Stream {

func (t *http2Client) getPeer() *peer.Peer {
return &peer.Peer{
Addr: t.remoteAddr,
AuthInfo: t.authInfo, // Can be nil
Addr: t.remoteAddr,
AuthInfo: t.authInfo, // Can be nil
LocalAddr: t.localAddr,
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/transport/http2_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1440,8 +1440,9 @@ func (t *http2Server) getOutFlowWindow() int64 {

func (t *http2Server) getPeer() *peer.Peer {
return &peer.Peer{
Addr: t.remoteAddr,
AuthInfo: t.authInfo, // Can be nil
Addr: t.remoteAddr,
AuthInfo: t.authInfo, // Can be nil
LocalAddr: t.localAddr,
}
}

Expand Down
2 changes: 2 additions & 0 deletions peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Peer struct {
// AuthInfo is the authentication information of the transport.
// It is nil if there is no transport security being used.
AuthInfo credentials.AuthInfo
// LocalAddr is the peer local address.
LocalAddr net.Addr
}

type peerKey struct{}
Expand Down
10 changes: 10 additions & 0 deletions test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2382,6 +2382,11 @@ func testPeerClientSide(t *testing.T, e env) {
if pp != sp {
t.Fatalf("peer.Addr = localhost:%v, want localhost:%v", pp, sp)
}

pla := peer.LocalAddr.String()
if _, _, err := net.SplitHostPort(pla); err != nil {
t.Fatalf("Failed to parse local address from peer.")
}
Comment on lines +2386 to +2389
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please assert the actual local addr here? rather than just checking it split host port. Same in other test. (Listener, Conn or hardcoded)

}

// TestPeerNegative tests that if call fails setting peer
Expand Down Expand Up @@ -2460,6 +2465,11 @@ func testPeerFailedRPC(t *testing.T, e env) {
if pp != sp {
t.Fatalf("peer.Addr = localhost:%v, want localhost:%v", pp, sp)
}

pla := peer.LocalAddr.String()
if _, _, err := net.SplitHostPort(pla); err != nil {
t.Fatalf("Failed to parse local address from peer.")
}
}
}

Expand Down