Files
rpcx/util/net.go
liaotonglang 6a14ed3000 Optimize GetFreePort(), bypass string manipulation
net.Listen("tcp", "127.0.0.1:0").Addr is a net.TCPAddr, use a type assertion
to get real value and then we have port.

Seems better, a little bit:

	go test -bench="BenchmarkGetFreePort*" ./util

	goos: linux
	goarch: amd64
	pkg: github.com/smallnest/rpcx/util
	cpu: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
	BenchmarkGetFreePort_Old-4        132356              8869 ns/op
	BenchmarkGetFreePort_New-4        139147              8317 ns/op
	PASS
	ok      github.com/smallnest/rpcx/util  2.528s

I notice that GetFreePort() not used in rpcx anymore, is backword compatibility
the reason we must have it?
2022-01-27 16:43:46 +08:00

157 lines
3.0 KiB
Go

package util
import (
"errors"
"fmt"
"net"
"net/url"
"sort"
"strconv"
"strings"
)
// GetFreePort gets a free port.
func GetFreePort() (port int, err error) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return 0, err
}
defer listener.Close()
return listener.Addr().(*net.TCPAddr).Port, nil
}
// ParseRpcxAddress parses rpcx address such as tcp@127.0.0.1:8972 quic@192.168.1.1:9981
func ParseRpcxAddress(addr string) (network string, ip string, port int, err error) {
ati := strings.Index(addr, "@")
if ati <= 0 {
return "", "", 0, fmt.Errorf("invalid rpcx address: %s", addr)
}
network = addr[:ati]
addr = addr[ati+1:]
var portstr string
ip, portstr, err = net.SplitHostPort(addr)
if err != nil {
return "", "", 0, err
}
port, err = strconv.Atoi(portstr)
return network, ip, port, err
}
func ConvertMeta2Map(meta string) map[string]string {
var rt = make(map[string]string)
if meta == "" {
return rt
}
v, err := url.ParseQuery(meta)
if err != nil {
return rt
}
for key := range v {
rt[key] = v.Get(key)
}
return rt
}
func ConvertMap2String(meta map[string]string) string {
var buf strings.Builder
keys := make([]string, 0, len(meta))
for k := range meta {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
vs := meta[k]
keyEscaped := url.QueryEscape(k)
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(keyEscaped)
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(vs))
}
return buf.String()
}
// ExternalIPV4 gets external IPv4 address of this server.
func ExternalIPV4() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network?")
}
// ExternalIPV6 gets external IPv6 address of this server.
func ExternalIPV6() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To16()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network?")
}