fix(http): honor configured request header deadlines

Signed-off-by: Feng Ruohang <rh@vonng.com>
(cherry picked from commit 0d48d32d7e038ae1ea5966f3d7e0cb86780a6311)
Signed-off-by: Feng Ruohang <rh@vonng.com>
This commit is contained in:
Feng Ruohang
2026-09-16 00:17:41 +08:00
parent aea3882c95
commit 055030ea53
74 changed files with 4386 additions and 3 deletions
+20 -2
View File
@@ -34,6 +34,8 @@ type DeadlineConn struct {
net.Conn
readDeadline time.Duration // sets the read deadline on a connection.
readSetAt time.Time
readExplicit time.Time // last deadline requested by the caller.
readDeadlineStrict bool // idle renewal must not extend readExplicit.
writeDeadline time.Duration // sets the write deadline on a connection.
writeSetAt time.Time
abortReads, abortWrites atomic.Bool // A deadline was set to indicate caller wanted the conn to time out.
@@ -59,17 +61,31 @@ func (c *DeadlineConn) setReadDeadline() {
c.mu.Lock()
defer c.mu.Unlock()
if c.abortReads.Load() {
if c.abortReads.Load() || c.infReads.Load() {
return
}
now := time.Now()
if now.Sub(c.readSetAt) > updateInterval {
c.Conn.SetReadDeadline(now.Add(c.readDeadline + updateInterval))
deadline := now.Add(c.readDeadline + updateInterval)
if c.readDeadlineStrict && !c.readExplicit.IsZero() && c.readExplicit.Before(deadline) {
deadline = c.readExplicit
}
c.Conn.SetReadDeadline(deadline)
c.readSetAt = now
}
}
// SetReadDeadlineStrict controls whether idle renewal may extend a deadline set
// by SetReadDeadline or SetDeadline. The default is false. Explicit zero and
// past deadlines retain their disable/cancel semantics in either mode.
func (c *DeadlineConn) SetReadDeadlineStrict(strict bool) {
c.mu.Lock()
defer c.mu.Unlock()
c.readDeadlineStrict = strict
c.readSetAt = time.Time{}
}
func (c *DeadlineConn) setWriteDeadline() {
// Do not set a Write deadline, if upstream wants to cancel all reads.
if c.writeDeadline <= 0 || c.abortWrites.Load() || c.infWrites.Load() {
@@ -115,6 +131,7 @@ func (c *DeadlineConn) SetDeadline(t time.Time) error {
defer c.mu.Unlock()
c.readSetAt = time.Time{}
c.readExplicit = t
c.writeSetAt = time.Time{}
c.abortReads.Store(!t.IsZero() && time.Until(t) < 0)
c.abortWrites.Store(!t.IsZero() && time.Until(t) < 0)
@@ -132,6 +149,7 @@ func (c *DeadlineConn) SetReadDeadline(t time.Time) error {
c.abortReads.Store(!t.IsZero() && time.Until(t) < 0)
c.infReads.Store(t.IsZero())
c.readSetAt = time.Time{}
c.readExplicit = t
return c.Conn.SetReadDeadline(t)
}