Flood of TIME_WAIT Connections from a Go HTTP Client: Unread Response Bodies and the Liveness Probe
Notes from tuning TIME_WAIT connections in a Go HTTP Client.
Business flow

Analysis
Container monitoring showed a large number of short-lived connections between the service and the load balancer in front of the event bus. Time to go back to the code.
The code that sends the request:
1 | func SendToKEvent(ev *KEvent) error { |
The HTTP client code:
1 | var ( |
The code looked fine at first glance, but it reminded me of a problem I had handled before with the Go Elasticsearch client:
TCP state diagram (in Chinese)
Checking the TIME_WAIT section of that post confirmed it: this was exactly the issue:
https://pkg.go.dev/net/http#Response
1 | // The http Client and Transport guarantee that Body is always |
Fixing the code:
1 | func SendToKEvent(ev *KEvent) error { |
After redeploying, the number of TIME_WAIT connections dropped a lot, but a dozen or so were still there:
1 | bash-5.0# netstat -anp |grep TIME |
A couple of things to note here:
- 172.16.3.247 is the pod's IP
- 10.200.76.64 is the IP of the host machine the pod runs on
So there were short-lived connections between the pod and its host. What were those connections doing? Time for a packet capture:

Look closely at frame No. 502:
1 | Frame 502: 177 bytes on wire (1416 bits), 177 bytes captured (1416 bits) |
The Connection header:
Connection: keep-alive— once a page has loaded, the TCP connection used to transfer HTTP data between client and server stays open. If the client hits the same server again, it reuses the connection that is already established.Connection: close— once a request completes, the TCP connection between client and server is closed. The next request from the client has to establish a new TCP connection.
The note on the Connection line says it all: a request carrying Connection: close will be a short-lived connection.
Now let's look at the service's Deployment configuration:
1 | livenessProbe: |
At this point everything adds up: Kubernetes requests the service's liveness endpoint every 10 seconds, each request is a short-lived connection, and the default TIME_WAIT duration is 120s.
So the service should hover at around 11–13 TIME_WAIT connections at all times.
That explained everything else.
Conclusions
- After a Go HTTP client request completes, even if the calling code does not use the response body, you still have to read the body.
- As long as a service has a liveness probe configured, there will be short-lived connections; their number depends on the probe interval.
Further reading
- TCP state diagram (in Chinese)
This article is the English edition of a post originally published in Chinese.