Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed class ConcurrencyLimiter : RateLimiter
private object Lock => _queue;

/// <inheritdoc />
public override TimeSpan? IdleDuration => RateLimiterHelper.GetElapsedTime(_idleSince);
public override TimeSpan? IdleDuration => Stopwatch.GetElapsedTime(_idleSince);
Comment thread
WeihanLi marked this conversation as resolved.
Outdated
/// <summary>
/// Initializes the <see cref="ConcurrencyLimiter"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public sealed class FixedWindowRateLimiter : ReplenishingRateLimiter

/// <summary>
/// Function to calculate elapsed time from a given tick value.
/// Defaults to <see cref="RateLimiterHelper.GetElapsedTime(long?)"/>.
/// Defaults to the <c>Stopwatch.GetElapsedTime(long?)</c> extension which returns <see langword="null"/> when the timestamp is <see langword="null"/>.
/// In tests, this field can be reassigned via reflection to inject custom time behavior without modifying the public API.
/// </summary>
private readonly Func<long?, TimeSpan?> _getElapsedTime = RateLimiterHelper.GetElapsedTime;
private readonly Func<long?, TimeSpan?> _getElapsedTime = Stopwatch.GetElapsedTime;
Comment thread
WeihanLi marked this conversation as resolved.
Outdated
Comment thread
WeihanLi marked this conversation as resolved.
Outdated

private readonly Timer? _renewTimer;
private readonly FixedWindowRateLimiterOptions _options;
Expand All @@ -39,7 +39,7 @@ public sealed class FixedWindowRateLimiter : ReplenishingRateLimiter
private static readonly RateLimitLease FailedLease = new FixedWindowLease(false, null);

/// <inheritdoc />
public override TimeSpan? IdleDuration => RateLimiterHelper.GetElapsedTime(_idleSince);
public override TimeSpan? IdleDuration => Stopwatch.GetElapsedTime(_idleSince);
Comment thread
WeihanLi marked this conversation as resolved.
Outdated

/// <inheritdoc />
public override bool IsAutoReplenishing => _options.AutoReplenishment;
Expand Down Expand Up @@ -301,7 +301,7 @@ private void ReplenishInternal(long nowTicks)
return;
}

if (RateLimiterHelper.GetElapsedTime(_lastReplenishmentTick, nowTicks) < _options.Window && !_options.AutoReplenishment)
if (Stopwatch.GetElapsedTime(_lastReplenishmentTick, nowTicks) < _options.Window && !_options.AutoReplenishment)
Comment thread
WeihanLi marked this conversation as resolved.
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ namespace System.Threading.RateLimiting
{
internal static class RateLimiterHelper
{
public static TimeSpan? GetElapsedTime(long? startTimestamp)
extension(Stopwatch)
{
if (startTimestamp is null)
public static TimeSpan? GetElapsedTime(long? startTimestamp)
Comment thread
WeihanLi marked this conversation as resolved.
Outdated
Comment thread
WeihanLi marked this conversation as resolved.
Outdated
Comment thread
WeihanLi marked this conversation as resolved.
Outdated
Comment thread
WeihanLi marked this conversation as resolved.
Outdated
{
return null;
}

return Stopwatch.GetElapsedTime(startTimestamp.Value);
}
if (startTimestamp is null)
{
return null;
}

public static TimeSpan GetElapsedTime(long startTimestamp, long endTimestamp)
{
return Stopwatch.GetElapsedTime(startTimestamp, endTimestamp);
return Stopwatch.GetElapsedTime(startTimestamp.Value);
Comment thread
WeihanLi marked this conversation as resolved.
Outdated
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public sealed class SlidingWindowRateLimiter : ReplenishingRateLimiter
private static readonly RateLimitLease FailedLease = new SlidingWindowLease(false, null);

/// <inheritdoc />
public override TimeSpan? IdleDuration => RateLimiterHelper.GetElapsedTime(_idleSince);
public override TimeSpan? IdleDuration => Stopwatch.GetElapsedTime(_idleSince);
Comment thread
WeihanLi marked this conversation as resolved.
Outdated

/// <inheritdoc />
public override bool IsAutoReplenishing => _options.AutoReplenishment;
Expand Down Expand Up @@ -293,7 +293,7 @@ private void ReplenishInternal(long nowTicks)
return;
}

if (RateLimiterHelper.GetElapsedTime(_lastReplenishmentTick, nowTicks) < ReplenishmentPeriod && !_options.AutoReplenishment)
if (Stopwatch.GetElapsedTime(_lastReplenishmentTick, nowTicks) < ReplenishmentPeriod && !_options.AutoReplenishment)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class TokenBucketRateLimiter : ReplenishingRateLimiter
private static readonly RateLimitLease FailedLease = new TokenBucketLease(false, null);

/// <inheritdoc />
public override TimeSpan? IdleDuration => RateLimiterHelper.GetElapsedTime(_idleSince);
public override TimeSpan? IdleDuration => Stopwatch.GetElapsedTime(_idleSince);
Comment thread
WeihanLi marked this conversation as resolved.
Outdated

/// <inheritdoc />
public override bool IsAutoReplenishing => _options.AutoReplenishment;
Expand Down Expand Up @@ -308,7 +308,7 @@ private void ReplenishInternal(long nowTicks)
}
else
{
add = _fillRate * RateLimiterHelper.GetElapsedTime(_lastReplenishmentTick, nowTicks).Ticks;
add = _fillRate * Stopwatch.GetElapsedTime(_lastReplenishmentTick, nowTicks).Ticks;
}

_tokenCount = Math.Min(_options.TokenLimit, _tokenCount + add);
Expand Down
Loading