diff --git a/modules/caddyhttp/idletimeout.go b/modules/caddyhttp/idletimeout.go index ce1d0b4b6..c3967940f 100644 --- a/modules/caddyhttp/idletimeout.go +++ b/modules/caddyhttp/idletimeout.go @@ -18,6 +18,8 @@ import ( "context" "errors" "io" + "math" + "math/bits" "net/http" "os" "sync" @@ -64,8 +66,11 @@ type IdleDeadline struct { func (d *IdleDeadline) next() (deadline time.Time) { if d.MinRate > 0 { - credit := time.Duration(d.transferred) * time.Second / time.Duration(d.MinRate) - deadline = d.Start.Add(d.Timeout + credit) + allowance := d.Timeout + rateCredit(d.transferred, d.MinRate) + if allowance < d.Timeout { // the sum overflowed + allowance = math.MaxInt64 + } + deadline = d.Start.Add(allowance) } else { deadline = time.Now().Add(d.Timeout) } @@ -76,6 +81,25 @@ func (d *IdleDeadline) next() (deadline time.Time) { return } +// rateCredit returns how long transferring n bytes may take at rate +// bytes/second, saturating instead of overflowing: n * time.Second exceeds +// int64 once n passes about 9.2 GB, which would turn the credit negative +// and abort large transfers that easily sustain the rate. +func rateCredit(n, rate int64) time.Duration { + if n <= 0 { + return 0 + } + hi, lo := bits.Mul64(uint64(n), uint64(time.Second)) + if hi >= uint64(rate) { + return math.MaxInt64 + } + q, _ := bits.Div64(hi, lo, uint64(rate)) + if q > math.MaxInt64 { + return math.MaxInt64 + } + return time.Duration(q) +} + // idleOverride holds what restore needs to undo an override. type idleOverride struct { prev IdleDeadline diff --git a/modules/caddyhttp/idletimeout_test.go b/modules/caddyhttp/idletimeout_test.go index 857ef6e60..af96381fb 100644 --- a/modules/caddyhttp/idletimeout_test.go +++ b/modules/caddyhttp/idletimeout_test.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "io" + "math" "net/http" "net/http/httptest" "os" @@ -1605,3 +1606,35 @@ func TestIdleTimeoutWriterRestoresHardDeadline(t *testing.T) { }) } } + +func TestIdleDeadlineMinRateLargeTransfer(t *testing.T) { + start := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + const gb = 1_000_000_000 + for _, tc := range []struct { + name string + transferred int64 + minRate int64 + want time.Duration // allowance past start, excluding Timeout + }{ + {"small", 5 * 1024 * 1024, 1024 * 1024, 5 * time.Second}, + {"just below overflow", 9 * gb, gb, 9 * time.Second}, + {"10 GB at 1 MB/s", 10 * gb, 1_000_000, 10_000 * time.Second}, + {"100 GB at 1 GB/s", 100 * gb, gb, 100 * time.Second}, + {"1 TB at 1 B/s", 1000 * gb, 1, math.MaxInt64}, + {"max int64 bytes", math.MaxInt64, 1, math.MaxInt64}, + } { + t.Run(tc.name, func(t *testing.T) { + d := IdleDeadline{Start: start, Timeout: time.Minute, MinRate: tc.minRate, transferred: tc.transferred} + got := d.next() + if tc.want == math.MaxInt64 { + if !got.After(start.Add(100 * 365 * 24 * time.Hour)) { + t.Fatalf("deadline = %v, want far in the future", got) + } + return + } + if want := start.Add(time.Minute + tc.want); !got.Equal(want) { + t.Fatalf("deadline = %v, want %v", got, want) + } + }) + } +}