Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTTP Server on random available port in Go

HTTP Server on random available port in Go

Imagine you want to start an HTTP server without specifying the port. For example, a temporary server for testing. How do you do that?

The answer is straightforward. Specify the port number to 0 like:

l, err := net.Listen("tcp", ":0")

It’s an easy rule provided by the net package, but do you understand how Go binds a random port allocation. I’ll give you a clear understanding of what Go does inside the net package.

The presentation contains the following topics.

net, net/http packages
File descriptor
System calls
I think this presentation will give audiences not only a knowledge of Go, but also one of system calls.

Kazuki Higashiguchi

March 24, 2022

More Decks by Kazuki Higashiguchi

Other Decks in Technology


  1. About Me Kazuki Higashiguchi Backend Engineer at Autify. No-code AI-powered

    software testing automation platform Follow @hgsgtk on Twitter
  2. Autify for Web / for Mobile We are taking demo

    requests https://autify.com/ Autify for Web Autify for Mobile
  3. Available ports HTTP server on a random available port 50123

    60012 45342 ????? go run main.go No port specified HTTP server ????? Port: ????? Bind & Listen
  4. net.Listen "If the port in the address parameter is empty

    or "0", as in "" or "[::1]:0", a port number is automatically chosen” https://pkg.go.dev/net#Listen
  5. 3. net.DefaultResolver -> net.LookupPort LookupPort looks up the port for

    the given network e.g. LookupPort(“”, “0”) -> returned port: 0 net/ipsock.go https://cs.opensource.google/go/go/+/refs/tags/go1.18:src/net/ipsock.go;l=260;drc=refs%2Ftags%2Fgo1.18
  6. socket type: SOCK_STREAM Network type Socket type Description “tcp” (Transmission

    Control Protocol) SOCK_STREAM Stream-oriented Sequenced, reliable, two-way, connection-base byte streams. “unix” (Unix domain sockets) “udp” (User Datagram Protocol) SOCK_DGRAM Datagram-oriented. Connectionless, unreliable messages “unixgram” “unixpacket” SOCK_SEQPACKET Datagram-oriented. Sequenced, reliable, two-way, connection-base byte streams.
  7. bind, listen, getsockname Bind Listen Getsockname Assign the socket address

    to the socket referred to by the file descriptor Mark the socket as a passive socket Return the current socket address
  8. Bind to an ephemeral port Bind Ephemeral port (Dynamic port)

    HTTP server Port: 54563 Bind Choose one from ephemeral ports
  9. Specification of bind OS When a port number is zero

    Port range Linux Attempt to bind to an ephemeral port (Basically) 49152 - 65535 Windows Assign a unique port from the dynamic client port range (On Windows Vista and later) 49152 - 65535 (Windows Server 2003 and earlier) 1025 - 5000 IANA defines the port range is 49152 - 65535
  10. Key consideration 1. Confirm the “bind” specification 2. Check your

    infrastructure can use an ephemeral port 3. Check the range of ephemeral port