Skip to content

Commit 7ad583b

Browse files
Test Userclaude
andcommitted
grpclb, channelz: replace net.IP with netip.Addr
Replace legacy net.IP usage with the modern net/netip package: - grpclb: use netip.AddrFromSlice instead of net.IP cast for parsing server list IP addresses, with proper error handling for invalid IPs. - channelz: use netip.AddrFromSlice and net.TCPAddr.AddrPort() for converting addresses in the channelz protoconv layer. Contributes to #8884. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c1a9239 commit 7ad583b

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

balancer/grpclb/grpclb_remote_balancer.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"io"
2525
"net"
26+
"net/netip"
2627
"sync"
2728
"time"
2829

@@ -82,7 +83,14 @@ func (lb *lbBalancer) processServerList(l *lbpb.ServerList) {
8283
}
8384

8485
md := metadata.Pairs(lbTokenKey, s.LoadBalanceToken)
85-
ipStr := net.IP(s.IpAddress).String()
86+
ip, ok := netip.AddrFromSlice(s.IpAddress)
87+
if !ok {
88+
if lb.logger.V(2) {
89+
lb.logger.Infof("Server list entry:|%d|, failed to parse IP address: %x", i, s.IpAddress)
90+
}
91+
continue
92+
}
93+
ipStr := ip.String()
8694
addr := imetadata.Set(resolver.Address{Addr: net.JoinHostPort(ipStr, fmt.Sprintf("%d", s.Port))}, md)
8795
if lb.logger.V(2) {
8896
lb.logger.Infof("Server list entry:|%d|, ipStr:|%s|, port:|%d|, load balancer token:|%v|", i, ipStr, s.Port, s.LoadBalanceToken)

channelz/internal/protoconv/socket.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package protoconv
2020

2121
import (
2222
"net"
23+
"net/netip"
2324
"time"
2425

2526
"google.golang.org/grpc/codes"
@@ -62,13 +63,18 @@ func addrToProto(a net.Addr) *channelzpb.Address {
6263
// TODO: Address_OtherAddress{}. Need proto def for Value.
6364
case "ip":
6465
// Note zone info is discarded through the conversion.
65-
return &channelzpb.Address{Address: &channelzpb.Address_TcpipAddress{TcpipAddress: &channelzpb.Address_TcpIpAddress{IpAddress: a.(*net.IPAddr).IP}}}
66+
if addr, ok := netip.AddrFromSlice(a.(*net.IPAddr).IP); ok {
67+
return &channelzpb.Address{Address: &channelzpb.Address_TcpipAddress{TcpipAddress: &channelzpb.Address_TcpIpAddress{IpAddress: addr.AsSlice()}}}
68+
}
6669
case "ip+net":
6770
// Note mask info is discarded through the conversion.
68-
return &channelzpb.Address{Address: &channelzpb.Address_TcpipAddress{TcpipAddress: &channelzpb.Address_TcpIpAddress{IpAddress: a.(*net.IPNet).IP}}}
71+
if addr, ok := netip.AddrFromSlice(a.(*net.IPNet).IP); ok {
72+
return &channelzpb.Address{Address: &channelzpb.Address_TcpipAddress{TcpipAddress: &channelzpb.Address_TcpIpAddress{IpAddress: addr.AsSlice()}}}
73+
}
6974
case "tcp":
70-
// Note zone info is discarded through the conversion.
71-
return &channelzpb.Address{Address: &channelzpb.Address_TcpipAddress{TcpipAddress: &channelzpb.Address_TcpIpAddress{IpAddress: a.(*net.TCPAddr).IP, Port: int32(a.(*net.TCPAddr).Port)}}}
75+
tcpAddr := a.(*net.TCPAddr)
76+
addrPort := tcpAddr.AddrPort()
77+
return &channelzpb.Address{Address: &channelzpb.Address_TcpipAddress{TcpipAddress: &channelzpb.Address_TcpIpAddress{IpAddress: addrPort.Addr().AsSlice(), Port: int32(addrPort.Port())}}}
7278
case "unix", "unixgram", "unixpacket":
7379
return &channelzpb.Address{Address: &channelzpb.Address_UdsAddress_{UdsAddress: &channelzpb.Address_UdsAddress{Filename: a.String()}}}
7480
default:

0 commit comments

Comments
 (0)