API Rate Limit HTTP Handler with HttpClientFactory

sebnilsson profile image Seb Nilsson Originally published at sebnilsson.com on ・2 min read

Most APIs have a Rate Limit of some sort. For example, GitHub has a limit of 5000 requests per hour. This can be handled, as a consumer of the API, by limiting your use by timing your requests to the API or through caching of the results.

What about when an API limits your requests per second? This is probably something you would want to handle somewhere central in your code and not spread out everywhere where you make an HTTP call to the API.


For me, the solution was to add aOutgoing request middlewareto the setup of theHttpClientFactory.

With this, I can just configure the startup services to use this RateLimitHttpMessageHandler-class with the HttpClientFactory:

    .AddHttpClient<IApi, Api>()
    .AddHttpMessageHandler(() =>
        new RateLimitHttpMessageHandler(
            limitCount: 5,
            limitTime: TimeSpan.FromSeconds(1)))

This ensures that wherever I use the class IApi, through dependency injection, it will limit the calls to the API to only 5 calls per second.

The simplified version of code for the RateLimitHttpMessageHandler:

public class RateLimitHttpMessageHandler : DelegatingHandler
    private readonly List<DateTimeOffset> _callLog =
        new List<DateTimeOffset>();
    private readonly TimeSpan _limitTime;
    private readonly int _limitCount;

    public RateLimitHttpMessageHandler(int limitCount, TimeSpan limitTime)
        _limitCount = limitCount;
        _limitTime = limitTime;

    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request,
        CancellationToken cancellationToken)
        var now = DateTimeOffset.UtcNow;

        lock (_callLog)

            while (_callLog.Count > _limitCount)

        await LimitDelay(now);

        return await base.SendAsync(request, cancellationToken);

    private async Task LimitDelay(DateTimeOffset now)
        if (_callLog.Count < _limitCount)

        var limit = now.Add(-_limitTime);

        var lastCall = DateTimeOffset.MinValue;
        var shouldLock = false;

        lock (_callLog)
            lastCall = _callLog.FirstOrDefault();
            shouldLock = _callLog.Count(x => x >= limit) >= _limitCount;

        var delayTime = shouldLock && (lastCall > DateTimeOffset.MinValue)
            ? (limit - lastCall)
            : TimeSpan.Zero;

        if (delayTime > TimeSpan.Zero)
            await Task.Delay(delayTime);

Posted on by:

sebnilsson profile

Seb Nilsson


Professional problem-solver, developing great ideas into solutions with value. Focus on Open Source, .NET, JavaScript/TypeScript, Azure, Web, DevOps, Agile & Leadership.


markdown guide