Skip to content

Commit

Permalink
vfs/errorfs: apply limit only when nonzero
Browse files Browse the repository at this point in the history
  • Loading branch information
jbowens committed Jul 10, 2024
1 parent 3b1077d commit e727ab7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions vfs/errorfs/latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit e727ab7

Please sign in to comment.