-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switching to use DI to push the strategy into the middleware
- Loading branch information
Elliot Coyle
committed
Nov 13, 2019
1 parent
62bbfdb
commit 5109c16
Showing
4 changed files
with
55 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 24 additions & 7 deletions
31
KnowYourLimits.AspNetCore/RateLimiterMiddlewareExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,40 @@ | ||
using KnowYourLimits.Identity; | ||
using KnowYourLimits.Strategies; | ||
using KnowYourLimits.Strategies.LeakyBucket; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
// These are all used outside of this lib, and so are 'unused' | ||
// ReSharper disable UnusedMember.Global | ||
|
||
namespace KnowYourLimits.AspNetCore | ||
{ | ||
// ReSharper disable once UnusedMember.Global | ||
public static class RateLimiterMiddlewareExtensions | ||
{ | ||
// ReSharper disable once UnusedMember.Global | ||
public static void UseRateLimiting<TClientIdentity>(this IApplicationBuilder applicationBuilder, | ||
IRateLimitStrategy<TClientIdentity> rateLimitStrategy) | ||
public static void UseRateLimiting<TClientIdentity>(this IApplicationBuilder applicationBuilder) | ||
where TClientIdentity : IClientIdentity, new() | ||
{ | ||
applicationBuilder.UseMiddleware<RateLimiterMiddleware<TClientIdentity>>(); | ||
} | ||
|
||
public static void AddRateLimiting<TClientIdentity>( | ||
this IServiceCollection serviceCollection, | ||
IRateLimitStrategy<TClientIdentity> strategy) | ||
where TClientIdentity : IClientIdentity, new() | ||
{ | ||
if (rateLimitStrategy.IdentityProvider == null) | ||
if (strategy.IdentityProvider == null) | ||
{ | ||
rateLimitStrategy.IdentityProvider = new IpClientIdentityProvider<TClientIdentity>(); | ||
strategy.IdentityProvider = new IpClientIdentityProvider<TClientIdentity>(); | ||
} | ||
|
||
applicationBuilder.UseMiddleware<RateLimiterMiddleware<TClientIdentity>>(rateLimitStrategy); | ||
serviceCollection.AddSingleton(strategy); | ||
} | ||
|
||
public static void AddLeakyBucketRateLimiting( | ||
this IServiceCollection serviceCollection, | ||
LeakyBucketConfiguration config) | ||
{ | ||
serviceCollection.AddRateLimiting(new LeakyBucketRateLimitStrategy(config)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters