Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Changes after .NET 5 migrating
  • Loading branch information
KedBein authored Nov 5, 2021
1 parent b8b5285 commit 5241aa3
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,25 @@ The `BrakePedal.Http` package provides an `HttpThrottlePolicy` for use in an HTT

The `HttpThrottlePolicy` expects a throttle key that's built from values in a request object like IP, endpoint, and other HTTP values.

#### Using as a handler
#### Using as a middleware

First define the throttle key we want to use for this policy:

// HttpRequestKey is part of the library and contains helpful values like IP address.
public class IpRequestKey : HttpRequestKey
{
public override void Initialize(HttpRequestMessage request)
public override void Initialize(HttpRequest request)
{
base.Initialize(request);
string forwardedFor = "X-Forwarded-For";
if (request.Headers.Contains(forwardedFor))

string forwardedFor = "X-Forwarded-For";
if (request.Headers.ContainsKey(forwardedFor))
{
// Use the forwarded IP address if sitting behind a load balancer
string ip = request.Headers.GetValues(forwardedFor).First().Trim();

// The base class HttpRequestKey gets the IP from other sources.
var ip = request.Headers[forwardedFor]
.First()
.Split(',')
.FirstOrDefault()?
.Trim();
ClientIp = IPAddress.Parse(ip);
}
}
Expand All @@ -161,16 +162,15 @@ The `IpRequestKey` defined above uses the IP of the request to track the number
PerSecond = 50 // Only allow 50 requests per second per IP
};

Create a delegating handler using the policy above and add it to the request pipeline:
Create a middleware or filter using the policy above and add it to the request pipeline:

public static class WebApiConfig
public class Startup
{
public static void Register(HttpConfiguration config)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Other code truncated
var throttleHandler = new HttpThrottleHandler(apiRequestPolicy);
config.MessageHandlers.Add(throttleHandler);
app.UseMiddleware<ThrottleMiddleware>();
// Other code truncated
}
Expand Down

0 comments on commit 5241aa3

Please sign in to comment.