/
tcp_port.go
61 lines (54 loc) · 1.2 KB
/
tcp_port.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package netx
import (
"context"
"fmt"
"net"
"strconv"
"sync"
"time"
"github.com/powerman/must"
)
//nolint:gochecknoglobals // By design.
var (
usedTCPPort = make(map[int]int)
usedTCPPortMu sync.Mutex
)
// UnusedTCPPort returns random unique unused TCP port at host.
func UnusedTCPPort(host string) (port int) {
var portStr string
ln, err := net.Listen("tcp", host+":0")
if err == nil {
err = ln.Close()
}
if err == nil {
_, portStr, err = net.SplitHostPort(ln.Addr().String())
}
if err == nil {
port, err = strconv.Atoi(portStr)
}
must.NoErr(err)
usedTCPPortMu.Lock()
used := usedTCPPort[port]
usedTCPPort[port]++
usedTCPPortMu.Unlock()
if used > 0 {
const maxRecursion = 3
if used > maxRecursion {
panic(fmt.Sprintf("same TCP port returned multiple times: %d", port))
}
return UnusedTCPPort(host)
}
return port
}
// WaitTCPPort tries to connect to addr until success or ctx.Done.
func WaitTCPPort(ctx context.Context, addr fmt.Stringer) error {
const delay = time.Second / 20
var dialer net.Dialer
for ; ctx.Err() == nil; time.Sleep(delay) {
conn, err := dialer.DialContext(ctx, "tcp", addr.String())
if err == nil {
return conn.Close()
}
}
return ctx.Err()
}