Questions around http server closeIdleConns

95 views
Skip to first unread message

Danny Shemesh

unread,
Oct 10, 2024, 2:20:11 PM10/10/24
to golang-nuts
Hello everyone, I came across a behavior while debugging an http server graceful shutdown which made wonder:

1. As a mitigation to this issue: https://github.com/golang/go/issues/22682
We've added the following check to `closeIdleConns`:
```
+ // Issue 22682: treat StateNew connections as if
+ // they're idle if we haven't read the first request's
+ // header in over 5 seconds.
+ if st == StateNew && unixSec < time.Now().Unix()-5 {
+ st = StateIdle
+ }
```

I was wondering if it would've made sense to consider `ReadHeaderTimeout` here when set, fall-backing to the default of 5 seconds; maybe taking the max of them.


2. I was wondering if it would've made sense to allow configuring a pre-close idle duration on the server, or a pre-close hook, that would allow closing only connections that had been idle for at least some time.

I'm dealing w/ a scenario where this would've been beneficial - 
There appears to be a small race during a version rollout and graceful termination of an http1 server,
where actively used connections in a burst period toggle between active / idle - and just before an idle conn is closed, it's re-used by a client, which then receives EOF and doesn't retry the operation, as the operation is non idempotent (say, http POST) -
this subsequently leads to some unfortunate side effects on the client side.

Having the ability to fine tune the closing of idle connections could allow narrowing this race window, if say, a user configures the server to only close connections that are idle for at least 5 seconds, or until the ctx is canceled;

I believe I can implement both above elegantly in `closeIdleConns`, and was wondering if this is something you'd consider.


Appreciate your time on the manner,
Danny
Reply all
Reply to author
Forward
0 new messages