Skip to content

Commit

Permalink
fix: Make LeakyBucketRequest disposable and dispose its SemaphoreSlim
Browse files Browse the repository at this point in the history
  • Loading branch information
nozzlegear committed May 10, 2024
1 parent a5b7154 commit dbca0b5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async Task WaitForAvailableAsync(int requestCost, CancellationToken cance
if (requestCost > MaximumAvailable)
throw new ShopifyException($"Requested query cost of {requestCost} is larger than maximum available {MaximumAvailable}");

var r = new LeakyBucketRequest(requestCost, cancellationToken);
using var r = new LeakyBucketRequest(requestCost, cancellationToken);

lock (_lock)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using System;
using System.Threading;

namespace ShopifySharp.Infrastructure.Policies.LeakyBucketPolicy;

internal sealed class LeakyBucketRequest(int cost, CancellationToken cancellationToken)
internal sealed class LeakyBucketRequest(int cost, CancellationToken cancellationToken) : IDisposable
{
public readonly int Cost = cost;
public readonly SemaphoreSlim Semaphore = new(0, 1);
public readonly CancellationToken CancellationToken = cancellationToken;

public void Dispose()
{
Semaphore?.Dispose();
}
}

0 comments on commit dbca0b5

Please sign in to comment.