-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathfreeport.go
97 lines (90 loc) · 4.13 KB
/
freeport.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// The MIT License
//
// Copyright (c) 2022 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package testsuite
import (
"fmt"
"net"
"runtime"
)
// Copied and adapted from
// https://github.com/temporalio/cli/blob/350cb2f9dca55e5063b39ffbdaa2739fdeab4399/temporalcli/devserver/freeport.go
// Returns a TCP port that is available to listen on, for the given (local) host.
//
// This works by binding a new TCP socket on port 0, which requests the OS to
// allocate a free port. There is no strict guarantee that the port will remain
// available after this function returns, but it should be safe to assume that
// a given port will not be allocated again to any process on this machine
// within a few seconds.
//
// On Unix-based systems, binding to the port returned by this function requires
// setting the `SO_REUSEADDR` socket option (Go already does that by default,
// but other languages may not); otherwise, the OS may fail with a message such
// as "address already in use". Windows default behavior is already appropriate
// in this regard; on that platform, `SO_REUSEADDR` has a different meaning and
// should not be set (setting it may have unpredictable consequences).
func getFreePort(host string) (string, int, error) {
l, err := net.Listen("tcp", host+":0")
if err != nil {
return "", 0, fmt.Errorf("failed to assign a free port: %w", err)
}
defer func() { _ = l.Close() }()
port := l.Addr().(*net.TCPAddr).Port
// On Linux and some BSD variants, ephemeral ports are randomized, and may
// consequently repeat within a short time frame after the listenning end
// has been closed. To avoid this, we make a connection to the port, then
// close that connection from the server's side (this is very important),
// which puts the connection in TIME_WAIT state for some time (by default,
// 60s on Linux). While it remains in that state, the OS will not reallocate
// that port number for bind(:0) syscalls, yet we are not prevented from
// explicitly binding to it (thanks to SO_REUSEADDR).
//
// On macOS and Windows, the above technique is not necessary, as the OS
// allocates ephemeral ports sequentially, meaning a port number will only
// be reused after the entire range has been exhausted. Quite the opposite,
// given that these OSes use a significantly smaller range for ephemeral
// ports, making an extra connection just to reserve a port might actually
// be harmful (by hastening ephemeral port exhaustion).
if runtime.GOOS != "darwin" && runtime.GOOS != "windows" {
r, err := net.DialTCP("tcp", nil, l.Addr().(*net.TCPAddr))
if err != nil {
return "", 0, fmt.Errorf("failed to assign a free port: %w", err)
}
c, err := l.Accept()
if err != nil {
return "", 0, fmt.Errorf("failed to assign a free port: %w", err)
}
// Closing the socket from the server side
_ = c.Close()
defer func() { _ = r.Close() }()
}
return host, port, nil
}
func getFreeHostPort() (string, error) {
host, port, err := getFreePort("127.0.0.1")
if err != nil {
host, port, err = getFreePort("[::1]")
if err != nil {
return "", err
}
}
return fmt.Sprintf("%v:%v", host, port), nil
}