forked from temporalio/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get-free-port: prevent duplicate ports on Linux (temporalio#564)
- Loading branch information
Showing
7 changed files
with
252 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,95 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2022 Temporal Technologies Inc. All rights reserved. | ||
// | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Copyright (c) 2021 Datadog, Inc. | ||
// | ||
// 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 devserver | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"runtime" | ||
) | ||
|
||
// Modified from https://github.com/phayes/freeport/blob/95f893ade6f232a5f1511d61735d89b1ae2df543/freeport.go | ||
|
||
func MustGetFreePort() int { | ||
p := NewPortProvider() | ||
defer p.Close() | ||
return p.MustGetFreePort() | ||
} | ||
|
||
func NewPortProvider() *PortProvider { | ||
return &PortProvider{} | ||
} | ||
|
||
type PortProvider struct { | ||
listeners []*net.TCPListener | ||
} | ||
|
||
// GetFreePort asks the kernel for a free open port that is ready to use. | ||
func (p *PortProvider) GetFreePort() (int, error) { | ||
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0") | ||
// 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) (int, error) { | ||
l, err := net.Listen("tcp", host+":0") | ||
if err != nil { | ||
if addr, err = net.ResolveTCPAddr("tcp6", "[::1]:0"); err != nil { | ||
panic(fmt.Sprintf("temporal: failed to get free port: %v", err)) | ||
} | ||
return 0, fmt.Errorf("failed to assign a free port: %v", err) | ||
} | ||
defer l.Close() | ||
port := l.Addr().(*net.TCPAddr).Port | ||
|
||
l, err := net.ListenTCP("tcp", addr) | ||
if err != nil { | ||
return 0, err | ||
// 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: %v", err) | ||
} | ||
c, err := l.Accept() | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to assign a free port: %v", err) | ||
} | ||
// Closing the socket from the server side | ||
c.Close() | ||
defer r.Close() | ||
} | ||
|
||
p.listeners = append(p.listeners, l) | ||
|
||
return l.Addr().(*net.TCPAddr).Port, nil | ||
return port, nil | ||
} | ||
|
||
func (p *PortProvider) MustGetFreePort() int { | ||
port, err := p.GetFreePort() | ||
// Returns a TCP port that is available to listen on, for the given (local) | ||
// host; panics if no port is available. | ||
// | ||
// 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 MustGetFreePort(host string) int { | ||
port, err := GetFreePort(host) | ||
if err != nil { | ||
panic(err) | ||
panic(fmt.Errorf("failed assigning ephemeral port: %w", err)) | ||
} | ||
return port | ||
} | ||
|
||
func (p *PortProvider) Close() error { | ||
for _, l := range p.listeners { | ||
if err := l.Close(); err != nil { | ||
return err | ||
} | ||
// Asserts that the given TCP port is available to listen on, for the given | ||
// (local) host; return an error if it is not. | ||
func CheckPortFree(host string, port int) error { | ||
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port)) | ||
if err != nil { | ||
return err | ||
} | ||
l.Close() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package devserver_test | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"testing" | ||
|
||
"github.com/temporalio/cli/temporalcli/devserver" | ||
) | ||
|
||
func TestFreePort_NoDouble(t *testing.T) { | ||
host := "127.0.0.1" | ||
portSet := make(map[int]bool) | ||
|
||
for i := 0; i < 2000; i++ { | ||
p, err := devserver.GetFreePort(host) | ||
if err != nil { | ||
t.Fatalf("Error: %s", err) | ||
break | ||
} | ||
|
||
if _, exists := portSet[p]; exists { | ||
t.Fatalf("Port %d has been assigned more than once", p) | ||
} | ||
|
||
// Add port to the set | ||
portSet[p] = true | ||
} | ||
} | ||
|
||
func TestFreePort_CanBindImmediatelySameProcess(t *testing.T) { | ||
host := "127.0.0.1" | ||
|
||
for i := 0; i < 500; i++ { | ||
p, err := devserver.GetFreePort(host) | ||
if err != nil { | ||
t.Fatalf("Error: %s", err) | ||
break | ||
} | ||
err = tryListenAndDialOn(host, p) | ||
if err != nil { | ||
t.Fatalf("Error: %s", err) | ||
break | ||
} | ||
} | ||
} | ||
|
||
// This function is used as part of unit tests, to ensure that the port | ||
func tryListenAndDialOn(host string, port int) error { | ||
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port)) | ||
if err != nil { | ||
return err | ||
} | ||
defer l.Close() | ||
|
||
r, err := net.DialTCP("tcp", nil, l.Addr().(*net.TCPAddr)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer r.Close() | ||
|
||
c, err := l.Accept() | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer c.Close() | ||
|
||
return nil | ||
} |
Oops, something went wrong.