HallPass 0.6.2
dotnet add package HallPass --version 0.6.2
NuGet\Install-Package HallPass -Version 0.6.2
<PackageReference Include="HallPass" Version="0.6.2" />
paket add HallPass --version 0.6.2
#r "nuget: HallPass, 0.6.2"
// Install HallPass as a Cake Addin
#addin nuget:?package=HallPass&version=0.6.2
// Install HallPass as a Cake Tool
#tool nuget:?package=HallPass&version=0.6.2
HallPass - PRE-RELEASE
Client-side rate limiter library for .NET to help client-side developers respect the rate limits of the APIs that they consume.
Installation
Nuget Package Manager
Install-Package HallPass
dotnet CLI
dotnet add package HallPass
Usage
Configuration
Using a HallPass-specific HttpClient
using HallPass;
...
// Register HallPass and hook into IHttpClientFactory.CreateHallPassClient() extension method
builder.Services.AddHallPass(config =>
{
// throttle all requests matching a uri pattern
config.UseLeakyBucket(
uriPattern: "api.foo.com/users",
rate: 100,
frequency: TimeSpan.FromMinutes(15),
capacity: 100);
// can also use a Func<HttpRequestMessage, bool> to resolve whether to throttle or not
config.UseLeakyBucket(
httpRequestMessage => httpRequestMessage.RequestUri.ToString().Contains("api.foo.com/posts"),
1000,
TimeSpan.FromMinutes(1),
1000);
});
Using the default HttpClient
using HallPass;
...
// Register HallPass and hook into IHttpClientFactory.CreateClient()
builder.Services.AddHallPass(config =>
{
// use the default HttpClient, not just the HallPass specific named client
config.UseDefaultHttpClient = true;
// throttle all requests matching a uri pattern
config.UseLeakyBucket(
uriPattern: "api.foo.com/users",
rate: 100,
frequency: TimeSpan.FromMinutes(15),
capacity: 100);
// can also use a Func<HttpRequestMessage, bool> to resolve whether to throttle or not
config.UseLeakyBucket(
httpRequestMessage => httpRequestMessage.RequestUri.ToString().Contains("api.foo.com/posts"),
1000,
TimeSpan.FromMinutes(1),
1000);
});
Usage - Throttle a single call
It's highly recommended to use the built-in .NET IHttpClientFactory
to obtain instances of HttpClient
when using HallPass. Under the hood, HallPass adds an additional DelegateHandler
to the named HallPass HttpClient
, which handles the throttling action per configured endpoint.
using HallPass;
...
class MyService
{
private readonly IHttpClientFactory _httpClientFactory;
public MyService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<FooUser> GetFooUserAsync(string userId, CancellationToken token = default)
{
// HallPass extension method
HttpClient httpClient = _httpClientFactory.CreateHallPassClient();
...
// block to keep rate within 100 / 15 minutes
await httpClient.GetAsync($"https://api.foo.com/users/{userId}", token);
}
}
Usage - Throttle a bunch of calls in a loop
HallPass works for synchronous loops (within async methods) by simply awaiting until it has permission to proceed based on the configuration.
var userIds = Enumerable.Range(1, 500);
foreach (var userId in userIds)
{
// block to keep rate within 100 / 15 minutes
await httpClient.GetAsync($"https://api.foo.com/users/{userId}", token);
}
Usage - Throttle a bunch of calls concurrently
HallPass is also thread-safe, working as expected for concurrent bunches of requests. In this case, it's generally better to allow IHttpClientFactory
to manage instances of HttpClient
per Task
.
var tasks = Enumerable
.Range(1, 500)
.Select(userId => Task.Run(async () =>
{
// usually it's better to get a client per Task, allowing IHttpClientFactory to manage the concurrency complexity
var httpClient = _httpClientFactory.CreateHallPassClient();
// block to keep rate within 100 / 15 minutes
await httpClient.GetAsync($"https://api.foo.com/users/{userId}", token);
}))
.ToList();
await Task.WhenAll(tasks);
COMING SOON
- Throttle a Bunch of Calls Across Distributed Systems - (demo is available at hallpass.dev)
- More Bucket Types (Fixed Window, Sliding Log, Sliding Window, Query Cost, Concurrency Limit)
COMING SOON: Throttle a Bunch of Calls Across Distributed Systems
If demand is there, we can consider modifying HallPass to throttle calls across distributed systems. If you have multiple instances of an application running at once, but need to respect a single external API rate limit, or if you have multiple different applications running but still need to respect a single external API rate limit between all instances and applications, you would be able to do so with minimal code changes.
Configuration
builder.Services.AddHallPass(config =>
{
// remote buckets, for coordinating clusters of services
config
.UseLeakyBucket("api.bar.com/statuses", 50, TimeSpan.FromMinutes(1), 50)
// client id and secret provided when registering an app in your HallPass dashboard
// 'key' is essential, acting as the shared token used to group/coordinate multiple instances
.ForMultipleInstances("my-client-id", "my-client-secret", key: "api.bar.com/statuses");
});
Usage
/* this line will wait, if necessary, so the frequency remains within 50 requests
per 1 minute ACROSS ALL DISTRIBUTED INSTANCES for the given HallPass client_id,
as defined in the configuration */
await httpClient.GetAsync($"https://api.bar.com/statuses");
Distributed Throttling Notes
This would be a paid service, and the HallPass API itself will be rate-limited (with basic throttling and retries handled via the SDK code). We're still finalizing the pricing model, but hope to have a free tier available to demo soon!
HallPass will take care of registering individual instances, "fairly" dolling out permissions, and tracking the global rate limit for your account/app and its usage on our servers.
HallPass will never know what endpoints you're calling, because the actual API call is still handled locally within each application. All that HallPass receives is an encrypted unique ID representing each scoped throttle group, and the bucket type used for that key.
API calls to the HallPass service will be limited. Broadly, the SDK will request and store bunches of HallPasses that can be used locally. When the local cache runs out, it will reach out to the API again for a refill.
COMING SOON: More Bucket Types
Please let us know which bucket types would be valuable to have. Query Cost will likely either need significant configuration on the side of the user, or will need to be tailored to individual APIs.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. |
-
net6.0
- LazyCache (>= 2.4.0)
- lazycache.aspnetcore (>= 2.4.0)
- Microsoft.Extensions.Http (>= 6.0.0)
- microsoft.extensions.logging (>= 6.0.0)
- System.Text.Json (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.6.2 | 135 | 5/1/2023 |
0.6.1 | 373 | 8/18/2022 |
0.6.0 | 379 | 8/18/2022 |
0.5.5 | 360 | 8/17/2022 |
0.5.4 | 360 | 8/17/2022 |
0.5.3 | 351 | 8/17/2022 |
0.5.2 | 352 | 8/15/2022 |
0.5.1 | 398 | 8/15/2022 |
0.5.0 | 388 | 8/1/2022 |
0.4.1 | 372 | 7/29/2022 |
0.4.0 | 376 | 7/29/2022 |
0.3.0 | 383 | 7/28/2022 |
0.2.7 | 379 | 7/28/2022 |
0.2.6 | 381 | 7/28/2022 |
0.2.4 | 380 | 7/27/2022 |
0.2.3 | 377 | 7/21/2022 |
0.2.2 | 383 | 7/18/2022 |
0.2.1 | 360 | 7/15/2022 |
0.2.0 | 386 | 7/14/2022 |
0.1.3 | 398 | 7/7/2022 |
0.1.2 | 381 | 7/5/2022 |
0.1.1 | 390 | 6/8/2022 |
0.1.0 | 360 | 6/7/2022 |
0.0.18 | 376 | 6/7/2022 |
0.0.17 | 372 | 6/7/2022 |
0.0.16 | 352 | 6/6/2022 |
0.0.15 | 376 | 6/6/2022 |
0.0.14 | 377 | 6/6/2022 |
0.0.13 | 374 | 6/6/2022 |
0.0.12 | 382 | 6/6/2022 |
0.0.11 | 360 | 6/4/2022 |
0.0.10 | 397 | 6/3/2022 |
0.0.9 | 377 | 5/31/2022 |
0.0.8 | 369 | 5/31/2022 |
0.0.7 | 390 | 5/31/2022 |
0.0.6 | 386 | 5/31/2022 |
0.0.5 | 365 | 5/31/2022 |
0.0.4 | 387 | 5/31/2022 |
0.0.3 | 367 | 5/31/2022 |
0.0.2 | 382 | 5/31/2022 |
0.0.1 | 382 | 5/11/2022 |