Skip to content

Commit 86f9885

Browse files
committed
v0.0.4
1 parent df3d17d commit 86f9885

66 files changed

Lines changed: 525 additions & 752 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
<RepositoryUrl>https://github.com/managedcode/Orleans.RateLimiting</RepositoryUrl>
1818
<PackageProjectUrl>https://github.com/managedcode/Orleans.RateLimiting</PackageProjectUrl>
1919
<Product>Managed Code - Orleans RateLimiting</Product>
20-
<Version>0.0.3</Version>
21-
<PackageVersion>0.0.3</PackageVersion>
20+
<Version>0.0.4</Version>
21+
<PackageVersion>0.0.4</PackageVersion>
2222

2323
</PropertyGroup>
2424
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">

ManagedCode.Orleans.RateLimiting.Client/Attributes/AnonymousIpRateLimiterAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Attributes;
55
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
66
public class AnonymousIpRateLimiterAttribute : Attribute, IRateLimiterAttribute
77
{
8-
public string ConfigurationName { get; }
9-
108
public AnonymousIpRateLimiterAttribute(string configurationName)
119
{
1210
ConfigurationName = configurationName;
1311
}
12+
13+
public string ConfigurationName { get; }
1414
}

ManagedCode.Orleans.RateLimiting.Client/Attributes/AuthorizedIpRateLimiterAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Attributes;
55
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
66
public class AuthorizedIpRateLimiterAttribute : Attribute, IRateLimiterAttribute
77
{
8-
public string ConfigurationName { get; }
9-
108
public AuthorizedIpRateLimiterAttribute(string configurationName)
119
{
1210
ConfigurationName = configurationName;
1311
}
12+
13+
public string ConfigurationName { get; }
1414
}

ManagedCode.Orleans.RateLimiting.Client/Attributes/InRoleIpRateLimiterAttribute.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Attributes;
55
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
66
public class InRoleIpRateLimiterAttribute : Attribute, IRateLimiterAttribute
77
{
8-
public string ConfigurationName { get; }
9-
public string Role { get; }
10-
118
public InRoleIpRateLimiterAttribute(string configurationName, string role)
129
{
1310
ConfigurationName = configurationName;
1411
Role = role;
1512
}
13+
14+
public string Role { get; }
15+
public string ConfigurationName { get; }
1616
}

ManagedCode.Orleans.RateLimiting.Client/Attributes/IpRateLimiterAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Attributes;
55
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
66
public class IpRateLimiterAttribute : Attribute, IRateLimiterAttribute
77
{
8-
public string ConfigurationName { get; }
9-
108
public IpRateLimiterAttribute(string configurationName)
119
{
1210
ConfigurationName = configurationName;
1311
}
12+
13+
public string ConfigurationName { get; }
1414
}

ManagedCode.Orleans.RateLimiting.Client/Extensions/ApplicationBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static IApplicationBuilder UseOrleansIpRateLimiting(this IApplicationBuil
1010
applicationBuilder.UseMiddleware<OrleansIpRateLimitingMiddleware>();
1111
return applicationBuilder;
1212
}
13-
13+
1414
public static IApplicationBuilder UseOrleansUserRateLimiting(this IApplicationBuilder applicationBuilder)
1515
{
1616
applicationBuilder.UseMiddleware<OrleansUserRateLimitingMiddleware>();

ManagedCode.Orleans.RateLimiting.Client/Extensions/ClientBuilderExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using Microsoft.Extensions.DependencyInjection;
31
using Orleans.Hosting;
4-
using Orleans.Runtime;
52

63
namespace ManagedCode.Orleans.RateLimiting.Client.Extensions;
74

ManagedCode.Orleans.RateLimiting.Client/Extensions/HttpRequestExtensions.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ public static class HttpRequestExtensions
99
{
1010
public static string GetClientIpAddress(this HttpRequest request)
1111
{
12-
return GetClientIpAddress(request, new []
12+
return GetClientIpAddress(request, new[]
1313
{
1414
"X-Real-IP",
1515
"X-Forwarded-For",
1616
"REMOTE_ADDR"
1717
});
1818
}
19-
19+
2020
public static string GetClientIpAddress(this HttpRequest request, string[] headers)
2121
{
2222
string? ip = null;
23-
23+
2424
foreach (var header in headers)
2525
{
2626
ip = GetHeaderValueAs(request, header);
27-
if(!string.IsNullOrEmpty(ip))
27+
if (!string.IsNullOrEmpty(ip))
2828
break;
2929
}
3030

@@ -33,14 +33,14 @@ public static string GetClientIpAddress(this HttpRequest request, string[] heade
3333

3434
return ip ?? string.Empty;
3535
}
36-
36+
3737
private static string GetHeaderValueAs(HttpRequest request, string headerName)
3838
{
3939
StringValues values;
4040

4141
if (request.Headers?.TryGetValue(headerName, out values) ?? false)
4242
{
43-
string rawValues = values.ToString(); // writes out as Csv when there are multiple.
43+
var rawValues = values.ToString(); // writes out as Csv when there are multiple.
4444

4545
if (!string.IsNullOrWhiteSpace(rawValues))
4646
{
@@ -51,15 +51,12 @@ private static string GetHeaderValueAs(HttpRequest request, string headerName)
5151

5252
return string.Empty;
5353
}
54-
54+
5555
private static IEnumerable<string> SplitCsv(string? csvList)
5656
{
5757
if (string.IsNullOrWhiteSpace(csvList))
5858
return Enumerable.Empty<string>();
5959

60-
return csvList
61-
.TrimEnd(',')
62-
.Split(',')
63-
.Select(s => s.Trim());
60+
return csvList.TrimEnd(',').Split(',').Select(s => s.Trim());
6461
}
6562
}

ManagedCode.Orleans.RateLimiting.Client/ManagedCode.Orleans.RateLimiting.Client.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818

1919
<ItemGroup>
2020

21-
<PackageReference Include="ManagedCode.Communication" Version="2.0.22" />
21+
<PackageReference Include="ManagedCode.Communication" Version="2.0.22"/>
2222

23-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
24-
<PackageReference Include="Microsoft.Orleans.Client" Version="7.1.1" />
25-
<PackageReference Include="System.Threading.RateLimiting" Version="7.0.0" />
23+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0"/>
24+
<PackageReference Include="Microsoft.Orleans.Client" Version="7.1.1"/>
25+
<PackageReference Include="System.Threading.RateLimiting" Version="7.0.0"/>
2626

2727
</ItemGroup>
2828

2929
<ItemGroup>
30-
<ProjectReference Include="..\ManagedCode.Orleans.RateLimiting.Core\ManagedCode.Orleans.RateLimiting.Core.csproj" />
30+
<ProjectReference Include="..\ManagedCode.Orleans.RateLimiting.Core\ManagedCode.Orleans.RateLimiting.Core.csproj"/>
3131
</ItemGroup>
3232

3333
</Project>

ManagedCode.Orleans.RateLimiting.Client/Middlewares/OrleansBaseRateLimitingMiddleware.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.Net;
54
using System.Reflection;
65
using System.Threading.Tasks;
76
using ManagedCode.Communication;
87
using ManagedCode.Orleans.RateLimiting.Client.Attributes;
9-
using ManagedCode.Orleans.RateLimiting.Client.Extensions;
108
using ManagedCode.Orleans.RateLimiting.Core.Extensions;
119
using ManagedCode.Orleans.RateLimiting.Core.Models;
1210
using ManagedCode.Orleans.RateLimiting.Core.Models.Holders;
13-
using Microsoft.AspNetCore.Authorization;
1411
using Microsoft.AspNetCore.Http;
15-
using Microsoft.AspNetCore.Http.Extensions;
1612
using Microsoft.AspNetCore.Mvc.Controllers;
1713
using Microsoft.Extensions.DependencyInjection;
1814
using Microsoft.Extensions.Logging;
@@ -22,9 +18,9 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Middlewares;
2218

2319
public abstract class OrleansBaseRateLimitingMiddleware
2420
{
21+
private readonly IClusterClient _client;
2522
private readonly ILogger _logger;
2623
private readonly RequestDelegate _next;
27-
private readonly IClusterClient _client;
2824
private readonly IServiceProvider _services;
2925

3026
protected OrleansBaseRateLimitingMiddleware(ILogger logger, RequestDelegate next, IClusterClient client, IServiceProvider services)
@@ -35,13 +31,14 @@ protected OrleansBaseRateLimitingMiddleware(ILogger logger, RequestDelegate next
3531
_services = services;
3632
}
3733

38-
protected abstract void AddLimiters(HttpContext httpContext, GroupLimiterHolder holder);
34+
protected abstract void AddLimiters(HttpContext httpContext, GroupLimiterHolder holder);
35+
3936
public async Task Invoke(HttpContext httpContext)
4037
{
4138
await using var holder = new GroupLimiterHolder();
42-
39+
4340
AddLimiters(httpContext, holder);
44-
41+
4542
// throw too many requests if any of the limiters is null code 429
4643
var error = await holder.AcquireAsync();
4744
if (error is null)
@@ -52,49 +49,49 @@ public async Task Invoke(HttpContext httpContext)
5249
{
5350
httpContext.Response.Clear();
5451
httpContext.Response.StatusCode = (int)HttpStatusCode.TooManyRequests;
55-
await httpContext.Response.WriteAsJsonAsync(Result.Fail(HttpStatusCode.TooManyRequests,error.ToException()));
52+
await httpContext.Response.WriteAsJsonAsync(Result.Fail(HttpStatusCode.TooManyRequests, error.ToException()));
5653
}
5754
}
58-
55+
5956
protected (T attribute, string? postfix)? TryGetAttribute<T>(HttpContext httpContext) where T : Attribute, IRateLimiterAttribute
6057
{
6158
var endpoint = httpContext.GetEndpoint();
62-
63-
if(endpoint is null)
59+
60+
if (endpoint is null)
6461
return null;
65-
62+
6663
// first try to get attribute from endpoint,
6764
var attribute = endpoint.Metadata.GetMetadata<T>();
68-
string postfix = endpoint.ToString()!;
69-
65+
var postfix = endpoint.ToString()!;
66+
7067
if (attribute is null)
7168
{
7269
// then try to get attribute from controller
7370
var controllerType = endpoint.Metadata.GetMetadata<ControllerActionDescriptor>()?.ControllerTypeInfo;
7471

7572
if (controllerType != null)
7673
{
77-
attribute = controllerType.GetCustomAttribute<T>(inherit: true);
74+
attribute = controllerType.GetCustomAttribute<T>(true);
7875
postfix = controllerType.ToString();
7976
}
8077
}
8178

8279
if (attribute is null)
8380
return null;
84-
81+
8582
return (attribute, postfix);
8683
}
87-
84+
8885
protected ILimiterHolder? TryGetLimiterHolder(HttpContext httpContext, string key, string configurationName)
8986
{
9087
var limiter = _client.GetRateLimiterByConfig(key, configurationName, _services.GetService<IEnumerable<RateLimiterConfig>>());
91-
92-
if(limiter is null)
88+
89+
if (limiter is null)
9390
_logger.LogError($"Configuration {configurationName} not found for RateLimiter");
9491

9592
return limiter;
9693
}
97-
94+
9895
protected string CreateKey(params string[] parts)
9996
{
10097
return string.Join(":", parts);

0 commit comments

Comments
 (0)