Skip to content

Commit

Permalink
Merge pull request #955 from clement911/master
Browse files Browse the repository at this point in the history
More robust validation when setting leaky bucket state.
  • Loading branch information
clement911 authored Nov 13, 2023
2 parents 4cda5e5 + ba9bdf8 commit 25e4cbe
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ internal LeakyBucket(int maximumAvailable, int restoreRatePerSecond, Func<DateTi

public void SetState(int maximumAvailable, int restoreRatePerSecond, double currentlyAvailable)
{
if (maximumAvailable <= 0 || currentlyAvailable < 0 || restoreRatePerSecond <= 0 || currentlyAvailable > maximumAvailable)
throw new ArgumentOutOfRangeException();
if (maximumAvailable <= 0)
throw new ArgumentOutOfRangeException($"{nameof(maximumAvailable)} ({maximumAvailable}) must be greater than zero");

if (currentlyAvailable < 0)
throw new ArgumentOutOfRangeException($"{nameof(currentlyAvailable)} ({currentlyAvailable}) must be positive or zero.");

if (restoreRatePerSecond <= 0)
throw new ArgumentOutOfRangeException($"{nameof(restoreRatePerSecond)} ({restoreRatePerSecond}) must be greater than zero");

if (currentlyAvailable > maximumAvailable)
throw new ArgumentOutOfRangeException($"{nameof(currentlyAvailable)} ({currentlyAvailable}) must not be greater than {nameof(maximumAvailable)} ({maximumAvailable})");

lock (_lock)
{
Expand Down

0 comments on commit 25e4cbe

Please sign in to comment.