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

Questions for Rate Limiting #910

Closed
markjiang7m2 opened this issue Jun 3, 2019 · 5 comments
Closed

Questions for Rate Limiting #910

markjiang7m2 opened this issue Jun 3, 2019 · 5 comments
Labels
bug Identified as a potential bug Rate Limiting Ocelot feature: Rate Limiting

Comments

@markjiang7m2
Copy link

Expected Behavior

I setup a demo for Ocelot Rate Limiting function. Period: 1m, PeriodTimespan: 10, Limit: 5. As per the Documentation for Rate Limiting, The count of request should be limitted as 5 in 1 min, and then after 10 seconds, it will works again.

Actual Behavior

I setup a console project for sending the request. It will sleep 1s after got a response. Below is the output:

2019/6/3 13:33:31
OK, This is from localhost:8001, path: /api/ocelot/5
2019/6/3 13:33:32
OK, This is from localhost:8001, path: /api/ocelot/5
2019/6/3 13:33:33
OK, This is from localhost:8001, path: /api/ocelot/5
2019/6/3 13:33:34
OK, This is from localhost:8001, path: /api/ocelot/5
2019/6/3 13:33:35
OK, This is from localhost:8001, path: /api/ocelot/5
2019/6/3 13:33:36
TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.
2019/6/3 13:33:37
TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.
2019/6/3 13:33:38
TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.
2019/6/3 13:33:39
TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.
2019/6/3 13:33:40
TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.
2019/6/3 13:33:41
OK, This is from localhost:8001, path: /api/ocelot/5
2019/6/3 13:33:43
OK, This is from localhost:8001, path: /api/ocelot/5
2019/6/3 13:33:44
OK, This is from localhost:8001, path: /api/ocelot/5
2019/6/3 13:33:45
OK, This is from localhost:8001, path: /api/ocelot/5
2019/6/3 13:33:46
OK, This is from localhost:8001, path: /api/ocelot/5
2019/6/3 13:33:47
TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.
2019/6/3 13:33:48
TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.
2019/6/3 13:33:49
TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.
2019/6/3 13:33:50
TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.
2019/6/3 13:33:51
TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.

It is 5 successes, 5 rejects as period.
Then I change the RateLimitOptions as Period: 1m, PeriodTimespan: 10, Limit: 6, and I got 6 successes, 4 rejects as period.

My Questions

  1. Does the time of last reject = The time of the 1st success + PeriodTimespan?
  2. After the PeriodTimespan, request will works again, but in a Period(1min here), the count of successful requests is more than the limit(5 times here). What is the logic between Period and PeriodTimespan? Is there anything wrong with my setting?

Steps to Reproduce the Problem

Api service:

[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
	var result = await Task.Run(() =>
	{
		return $"This is from {HttpContext.Request.Host.Value}, path: {HttpContext.Request.Path}";
	});
	return Ok(result);
}

Ocelot.json:

{
  "DownstreamPathTemplate": "/api/ocelot/{postId}",
  "DownstreamScheme": "http",
  "DownstreamHostAndPorts": [
	{
	  "Host": "localhost",
	  "Port": 8001
	}
  ],
  "UpstreamPathTemplate": "/ocelot/ratelimit/{postId}",
  "UpstreamHttpMethod": [ "Get" ],
  "Priority": 2,
  "RateLimitOptions": {
	"ClientWhiteList": [
	  "markadmin"
	],
	"EnableRateLimiting": true,
	"Period": "1m",
	"PeriodTimespan": 10,
	"Limit": 5
  }
}

Console project:

public static async Task Main(string[] args)
{
	using (var client = new HttpClient())
	{
		for (var i = 0; i < 100; i++)
		{
			Console.WriteLine(DateTime.Now);

			var result = await client.GetAsync("http://localhost:4727/ocelot/ratelimit/5");
			Console.WriteLine($"{result.StatusCode}, {result.Content.ReadAsStringAsync().Result}");
			System.Threading.Thread.Sleep(1000);
		}
		Console.Read();
	}
}

Specifications

  • Version:13.5.1
  • Platform:.net core 2.2.0
  • Subsystem:
@markjiang7m2
Copy link
Author

One more question. I am trying to use the LoadBalancer in this demo, so I change the reroute as below.

{
  "DownstreamPathTemplate": "/api/ocelot/{postId}",
  "DownstreamScheme": "http",
  "DownstreamHostAndPorts": [
	{
	  "Host": "localhost",
	  "Port": 8001
	},
        {
	  "Host": "localhost",
	  "Port": 8002
	}
  ],
  "UpstreamPathTemplate": "/ocelot/ratelimit/{postId}",
  "UpstreamHttpMethod": [ "Get" ],
  "Priority": 2,
  "RateLimitOptions": {
	"ClientWhiteList": [
	  "markadmin"
	],
	"EnableRateLimiting": true,
	"Period": "1m",
	"PeriodTimespan": 10,
	"Limit": 5
  },
    "LoadBalancerOptions": {
    "Type": "RoundRobin"
    }
}

As the document say that
RoundRobin - loops through available services and sends requests.

I do the test as below.
Firstly, both downstream services are running correctly, and the results are correct. Then I try stopping the 8001 service, Ocelot will still transfer request to 8001 service, so I get a half bad request results. Is it correct?

@ahmedanwar100
Copy link

@markjiang7m2, can you please tell me from where you are getting markadmin in RateLimitOptions:ClientWhiteList ?

@raman-m raman-m added the Rate Limiting Ocelot feature: Rate Limiting label Jan 21, 2024
@raman-m
Copy link
Member

raman-m commented Jan 21, 2024

@markjiang7m2 on Jun 3, 2019

Hi Mark!
Thanks for bug reporting!

Current Rate Limiting issues
We have #1590 which seems is very similar to your user scenario. I would say #1590 is the duplicate of this issue. Please confirm that fact!

So, I'll keep this issue open, to let it be fixed together with #1590...

@raman-m
Copy link
Member

raman-m commented Jan 21, 2024

@markjiang7m2 commented on Jun 5, 2019

Regarding Load Balancer and RoundRobin...
This is already known bug #1513 which we accepted. Seems your case is related to #1041 also.

@raman-m raman-m added the bug Identified as a potential bug label Jan 21, 2024
@raman-m
Copy link
Member

raman-m commented Jun 15, 2024

Rate limiting bug was fixed by #1592
Bug fix is available in versions 23.3.0+

@raman-m raman-m closed this as completed Jun 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Identified as a potential bug Rate Limiting Ocelot feature: Rate Limiting
Projects
None yet
Development

No branches or pull requests

3 participants