Skip to content

Commit

Permalink
fix ratelimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillKurdyukov committed Aug 14, 2024
1 parent 9ac96c4 commit fb664a1
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions slo/src/Internal/SloContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public async Task Run(RunConfig runConfig)

var errorsGauge = metricFactory.CreateGauge("errors", "amount of errors", new[] { "class", "in" });

var writeLimiter = Policy.RateLimit(runConfig.WriteRps, TimeSpan.FromSeconds(1));
var readLimiter = Policy.RateLimit(runConfig.ReadRps, TimeSpan.FromSeconds(1));
var writeLimiter = Policy.RateLimit(runConfig.WriteRps, TimeSpan.FromSeconds(1), runConfig.WriteRps);
var readLimiter = Policy.RateLimit(runConfig.ReadRps, TimeSpan.FromSeconds(1), runConfig.ReadRps);

var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(runConfig.ShutdownTime));
Expand All @@ -152,7 +152,7 @@ Task ShootingTask(RateLimitPolicy rateLimitPolicy, string shootingName,
var tasks = new List<Task>();
long activeTasks = 0;
while (!cancellationTokenSource.Token.IsCancellationRequested)
{
try
Expand All @@ -161,7 +161,7 @@ Task ShootingTask(RateLimitPolicy rateLimitPolicy, string shootingName,
{
// ReSharper disable once AccessToModifiedClosure
Interlocked.Increment(ref activeTasks);
var sw = Stopwatch.StartNew();
var (attempts, statusCode) = await action(client, runConfig, errorsGauge);
string label;
Expand All @@ -184,11 +184,12 @@ Task ShootingTask(RateLimitPolicy rateLimitPolicy, string shootingName,
latencySummary.WithLabels(label).Observe(sw.ElapsedMilliseconds);
}));
}
catch (RateLimitRejectedException)
catch (RateLimitRejectedException e)
{
_logger.LogInformation("Waiting {ShootingName} task, count active tasks: {}", shootingName, Interlocked.Read(ref activeTasks));
_logger.LogInformation(e, "Waiting {ShootingName} task, count active tasks: {}", shootingName,
Interlocked.Read(ref activeTasks));
await Task.Delay(990, cancellationTokenSource.Token);
await Task.Delay(e.RetryAfter, cancellationTokenSource.Token);
}
}
Expand Down

0 comments on commit fb664a1

Please sign in to comment.