use net.JoinHostPort instead of fmt.Sprintf (#2791)

This commit is contained in:
fatedier
2022-02-09 15:19:35 +08:00
committed by GitHub
parent b2311e55e7
commit 6194273615
17 changed files with 61 additions and 49 deletions

View File

@@ -3,6 +3,7 @@ package port
import (
"fmt"
"net"
"strconv"
"sync"
"k8s.io/apimachinery/pkg/util/sets"
@@ -57,7 +58,7 @@ func (pa *Allocator) GetByName(portName string) int {
return 0
}
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
l, err := net.Listen("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)))
if err != nil {
// Maybe not controlled by us, mark it used.
pa.used.Insert(port)
@@ -65,7 +66,7 @@ func (pa *Allocator) GetByName(portName string) int {
}
l.Close()
udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%d", port))
udpAddr, err := net.ResolveUDPAddr("udp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)))
if err != nil {
continue
}