From e727ab747b2f3fcdcf92398f1770eb11e7d25bcd Mon Sep 17 00:00:00 2001 From: Jackson Owens Date: Wed, 10 Jul 2024 14:23:35 -0400 Subject: [PATCH] vfs/errorfs: apply limit only when nonzero --- vfs/errorfs/latency.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/vfs/errorfs/latency.go b/vfs/errorfs/latency.go index de033730e7..3a650b872d 100644 --- a/vfs/errorfs/latency.go +++ b/vfs/errorfs/latency.go @@ -59,7 +59,7 @@ type randomLatency struct { // mean is the mean duration injected each operation. mean time.Duration // limit configures a limit on total latency injected over the lifetime of - // the Injector. + // the Injector if nonzero. limit time.Duration // agg is the aggregate latency injected over the lifetime of the Injector. agg atomic.Int64 @@ -85,13 +85,17 @@ func (rl *randomLatency) MaybeError(op Op) error { dur = time.Duration(min(prng.ExpFloat64(), 20.0) * float64(rl.mean)) }) - if v := time.Duration(rl.agg.Add(int64(dur))); v-dur > rl.limit { - // We'd already exceeded the limit before adding dur. Don't inject - // anything. - return nil - } else if v > rl.limit { - // We're about to exceed the limit. Cap the duration. - dur -= v - rl.limit + // Apply a limit on total latency injected over the lifetime of the + // Injector, if one is configured. + if rl.limit > 0 { + if v := time.Duration(rl.agg.Add(int64(dur))); v-dur > rl.limit { + // We'd already exceeded the limit before adding dur. Don't inject + // anything. + return nil + } else if v > rl.limit { + // We're about to exceed the limit. Cap the duration. + dur -= v - rl.limit + } } time.Sleep(dur)