Skip to content

Commit 31dc471

Browse files
authored
grpclb: simplify stringifying of IPv6 with net.JoinHostPort (#8503)
This PR simplifies IP address handling in `lbBalancer.processServerList`. From [net.JoinHostPort](https://pkg.go.dev/net#JoinHostPort): > JoinHostPort combines host and port into a network address of the form "host:port". If host contains a colon, as found in literal IPv6 addresses, then JoinHostPort returns "[host]:port". RELEASE NOTES: none
1 parent 57b69b4 commit 31dc471

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

balancer/grpclb/grpclb_remote_balancer.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,8 @@ func (lb *lbBalancer) processServerList(l *lbpb.ServerList) {
8282
}
8383

8484
md := metadata.Pairs(lbTokenKey, s.LoadBalanceToken)
85-
ip := net.IP(s.IpAddress)
86-
ipStr := ip.String()
87-
if ip.To4() == nil {
88-
// Add square brackets to ipv6 addresses, otherwise net.Dial() and
89-
// net.SplitHostPort() will return too many colons error.
90-
ipStr = fmt.Sprintf("[%s]", ipStr)
91-
}
92-
addr := imetadata.Set(resolver.Address{Addr: fmt.Sprintf("%s:%d", ipStr, s.Port)}, md)
85+
ipStr := net.IP(s.IpAddress).String()
86+
addr := imetadata.Set(resolver.Address{Addr: net.JoinHostPort(ipStr, fmt.Sprintf("%d", s.Port))}, md)
9387
if lb.logger.V(2) {
9488
lb.logger.Infof("Server list entry:|%d|, ipStr:|%s|, port:|%d|, load balancer token:|%v|", i, ipStr, s.Port, s.LoadBalanceToken)
9589
}

0 commit comments

Comments
 (0)