Skip to content
Merged
Changes from all 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
23 changes: 12 additions & 11 deletions src/Throttlr.Api.RateLimit/Middleware/RateLimitingMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using RateLimit.Throttlr.Core;

namespace RateLimit.Throttlr.Middleware
{
Expand All @@ -16,6 +12,11 @@
private readonly RequestDelegate _next;
private readonly IRateLimiter _rateLimiter;

private const string RetryAfter = "Retry-After";
private const string RateLimitLimit = "X-RateLimit-Limit";
private const string RateLimitRemaining = "X-RateLimit-Remaining";
private const string RateLimitReset = "X-RateLimit-Reset";

public RateLimitingMiddleware(RequestDelegate next, IRateLimiter rateLimiter)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
Expand All @@ -33,19 +34,19 @@
context.Response.StatusCode = StatusCodes.Status429TooManyRequests;

// Add standard rate limit headers
context.Response.Headers["Retry-After"] = ((int)result.RetryAfter.Value.TotalSeconds).ToString();
context.Response.Headers["X-RateLimit-Limit"] = _rateLimiter.GetLimit().ToString();
context.Response.Headers["X-RateLimit-Remaining"] = result.Remaining.ToString();
context.Response.Headers["X-RateLimit-Reset"] = result.Reset?.ToUnixTimeSeconds().ToString();
context.Response.Headers[RetryAfter] = ((int)result.RetryAfter.Value.TotalSeconds).ToString();

Check warning on line 37 in src/Throttlr.Api.RateLimit/Middleware/RateLimitingMiddleware.cs

View workflow job for this annotation

GitHub Actions / demo-build

Nullable value type may be null.

Check warning on line 37 in src/Throttlr.Api.RateLimit/Middleware/RateLimitingMiddleware.cs

View workflow job for this annotation

GitHub Actions / demo-build

Nullable value type may be null.

Check warning on line 37 in src/Throttlr.Api.RateLimit/Middleware/RateLimitingMiddleware.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Nullable value type may be null.

Check warning on line 37 in src/Throttlr.Api.RateLimit/Middleware/RateLimitingMiddleware.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Nullable value type may be null.

Check warning on line 37 in src/Throttlr.Api.RateLimit/Middleware/RateLimitingMiddleware.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Nullable value type may be null.

Check warning on line 37 in src/Throttlr.Api.RateLimit/Middleware/RateLimitingMiddleware.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Nullable value type may be null.

Check warning on line 37 in src/Throttlr.Api.RateLimit/Middleware/RateLimitingMiddleware.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Nullable value type may be null.

Check warning on line 37 in src/Throttlr.Api.RateLimit/Middleware/RateLimitingMiddleware.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Nullable value type may be null.
context.Response.Headers[RateLimitLimit] = _rateLimiter.GetLimit().ToString();
context.Response.Headers[RateLimitRemaining] = result.Remaining.ToString();
context.Response.Headers[RateLimitReset] = result.Reset?.ToUnixTimeSeconds().ToString();

await context.Response.WriteAsync("Too Many Requests");
return;
}

// Attach useful headers on success
context.Response.Headers["X-RateLimit-Limit"] = _rateLimiter.GetLimit().ToString();
context.Response.Headers["X-RateLimit-Remaining"] = result.Remaining.ToString();
context.Response.Headers["X-RateLimit-Reset"] = result.Reset?.ToUnixTimeSeconds().ToString();
context.Response.Headers[RateLimitLimit] = _rateLimiter.GetLimit().ToString();
context.Response.Headers[RateLimitRemaining] = result.Remaining.ToString();
context.Response.Headers[RateLimitReset] = result.Reset?.ToUnixTimeSeconds().ToString();

await _next(context);
}
Expand Down
Loading