micro/util/net/net.go

79 lines
1.5 KiB
Go
Raw Normal View History

2019-05-31 01:52:10 +03:00
package net
import (
"errors"
"fmt"
"net"
"strconv"
"strings"
)
// HostPort format addr and port suitable for dial
func HostPort(addr string, port interface{}) string {
host := addr
if strings.Count(addr, ":") > 0 {
host = fmt.Sprintf("[%s]", addr)
}
// TODO check for NATS case
if v, ok := port.(string); ok {
if v == "" {
return fmt.Sprintf("%s", host)
}
}
return fmt.Sprintf("%s:%v", host, port)
}
2019-05-31 01:52:10 +03:00
// Listen takes addr:portmin-portmax and binds to the first available port
// Example: Listen("localhost:5000-6000", fn)
func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, error) {
if strings.Count(addr, ":") == 1 && strings.Count(addr, "-") == 0 {
2019-05-31 01:52:10 +03:00
return fn(addr)
}
// host:port || host:min-max
host, ports, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
2019-05-31 01:52:10 +03:00
// try to extract port range
prange := strings.Split(ports, "-")
2019-05-31 01:52:10 +03:00
// single port
if len(prange) < 2 {
2019-05-31 01:52:10 +03:00
return fn(addr)
}
// we have a port range
// extract min port
min, err := strconv.Atoi(prange[0])
2019-05-31 01:52:10 +03:00
if err != nil {
return nil, errors.New("unable to extract port range")
}
// extract max port
max, err := strconv.Atoi(prange[1])
2019-05-31 01:52:10 +03:00
if err != nil {
return nil, errors.New("unable to extract port range")
}
// range the ports
for port := min; port <= max; port++ {
// try bind to host:port
ln, err := fn(HostPort(host, port))
2019-05-31 01:52:10 +03:00
if err == nil {
return ln, nil
}
// hit max port
if port == max {
return nil, err
}
}
// why are we here?
return nil, fmt.Errorf("unable to bind to %s", addr)
}