Skip to main content

Bind to an ephemeral port, force it into the TIME_WAIT state, and unbind it.

Project description

ephemeral-port-reserve

Sometimes you need a networked program to bind to a port that can't be hard-coded. Generally this is when you want to run several of them in parallel; if they all bind to port 8080, only one of them can succeed.

The usual solution is the "port 0 trick". If you bind to port 0, your kernel will find some arbitrary high-numbered port that's unused and bind to that. Afterward you can query the actual port that was bound to if you need to use the port number elsewhere. However, there are cases where the port 0 trick won't work. For example, mysqld takes port 0 to mean "the port configured in my.cnf". Docker can bind your containers to port 0, but uses its own implementation to find a free port which races and fails in the face of parallelism.

ephemeral-port-reserve provides an implementation of the port 0 trick which is reliable and race-free. You can use it like so:

PORT="$(ephemeral-port-reserve)"
docker run -p 127.0.0.1:$PORT:5000 registry:2

ephemeral-port-reserve is a utility to bind to an ephemeral port, force it into the TIME_WAIT state, and unbind it.

This means that further ephemeral port alloctions won't pick this "reserved" port, but subprocesses can still bind to it explicitly, given that they use SO_REUSEADDR. By default on linux you have a grace period of 60 seconds to reuse this port. To check your own particular value:

$ cat /proc/sys/net/ipv4/tcp_fin_timeout
60

NOTE: By default, the port returned is specifically for localhost, aka 127.0.0.1. If you bind instead to 0.0.0.0, you may encounter a port conflict. If you need to bind to a non-localhost IP, you can pass it as the first argument.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ephemeral_port_reserve-1.1.3.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

ephemeral_port_reserve-1.1.3-py2.py3-none-any.whl (4.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file ephemeral_port_reserve-1.1.3.tar.gz.

File metadata

  • Download URL: ephemeral_port_reserve-1.1.3.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.5.2

File hashes

Hashes for ephemeral_port_reserve-1.1.3.tar.gz
Algorithm Hash digest
SHA256 043269a1b29bf9ecc23527b878037bfa1217d346696f09412ff75d89fe334acf
MD5 2bb4f9470c55cbadae36089d627d4b36
BLAKE2b-256 c2afd9ae52916187d811adea4212e96f27ba4fd00dd32df456219ebe0725b783

See more details on using hashes here.

Provenance

File details

Details for the file ephemeral_port_reserve-1.1.3-py2.py3-none-any.whl.

File metadata

  • Download URL: ephemeral_port_reserve-1.1.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.5.2

File hashes

Hashes for ephemeral_port_reserve-1.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b93f52664e5ec0d83c0ffdee3fed889210342fee149b1d0d108fb4d3d4a0bf38
MD5 a5e6fa315741f98b0c2aad470b6b0eef
BLAKE2b-256 2130a1baf66d09c9d15b5a058c07184ea138c26f930008628ce15164b1001de0

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page