HTTP Server on random available port in Go
summary
Shows that calling net.Listen("tcp", ":0") is the practical way to get a random port and that net/http/test uses the same. Explains internals: net.LookupPort returns 0, net/internal creates a socket, then the kernel runs three system calls bind listen getsockname to pick and report the ephemeral port. Notes to check OS ephemeral port ranges (transcript cites Windows Server 2003 as 1025–535000), and infrastructure limits like Fargate and firewall rules before relying on randomly assigned ports.
AI-written summary · about
chapters (10)
- 0:00Introduction
- 1:13Motivation for random port binding
- 2:58HTTP server on random port
- 3:40Using port zero
- 5:03How net.Listen works
- 6:30Network and port resolution
- 8:14System calls and sockets
- 10:23Bind call assigns port
- 11:27OS port range specs
- 12:11Infrastructure compatibility
related talks
47mUnbounded Concurrency in Go
05mLook Ma, No Ports!
31mHow to share IPv4 addresses by partitioning the port space
29mGoing Infinite: Handling 1 Million Websocket Connections in Go
07mFirst Steps Into Web Server Development
18mUnmasking netpoll.go
23mBuilding a net Conn Type From the Ground Up
01h 06mgolang rocks!
