Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint path involving * (variable) not working correctly #455

Open
martinaedma opened this issue Sep 1, 2023 · 3 comments
Open

Endpoint path involving * (variable) not working correctly #455

martinaedma opened this issue Sep 1, 2023 · 3 comments

Comments

@martinaedma
Copy link

Hi, im using following settings

"IpRateLimitingSettings": {
    "EnableEndpointRateLimiting": true,
    "StackBlockedRequests": false,
    "RealIpHeader": "X-Azure-ClientIP",
    "HttpStatusCode": 429,
    "GeneralRules": [
      {
        "Endpoint": "post:/api/Apartments/*/lock",
        "Period": "60s",
        "Limit": 3
      }
    ]
  }

Endpoint has following definition
[HttpPost("{id:int:min(1)}/lock")]

example request

/api/Apartments/12345/lock

when I hit this 3x I will get rate limited, but then I change variable and request go through.

/api/Apartments/11111/lock

in endpoint definition i thought /*/ between Apartments and lock would take care of this, but it doesnt.
How can I define endpoint path so that variable change does not trick limiter into thinking its a new url.

@martinaedma
Copy link
Author

Hei, I got it fixed when I found some previous discussions about it.
Made custom classes


    public class IpRateLimitConfigurationCustom : RateLimitConfiguration
    {
        public IpRateLimitConfigurationCustom(IOptions<IpRateLimitOptions> ipOptions, IOptions<ClientRateLimitOptions> clientOptions) : base(ipOptions, clientOptions)
        {

        }

        public override ICounterKeyBuilder EndpointCounterKeyBuilder { get; } = new EndpointCounterKeyBuilder();
    }

    public class EndpointCounterKeyBuilder : ICounterKeyBuilder
    {
        public string Build(ClientRequestIdentity requestIdentity, RateLimitRule rule)
        {
            // This will allow to rate limit /api/values/1 and api/values/2 under same counter
            return $"_{rule.Endpoint}";
        }
    }

used api/Apartments/*

wildcard

and injected my custom configuration class, works now

@martinaedma
Copy link
Author

Done

@liguobao
Copy link

liguobao commented Sep 5, 2023

Very good~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants