spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.52k stars 3.32k forks source link

Throw exception when request rate limiter denys request #780

Open tianmingxing opened 5 years ago

tianmingxing commented 5 years ago
  1. When the current limiting mechanism is triggered, the gateway responds to the status code 429 to the client, but I want the client to always accept only the 200 status code. Any error message is prompted by the response data. How can I capture this situation?
  2. I tried to write a class that inherits DefaultErrorAttributes to implement, but found that this exception was not caught at all.
ryanjbaxter commented 5 years ago

The configuration object for the rate limiter filter allows you to configure the status code https://github.com/spring-cloud/spring-cloud-gateway/blob/4ea874a3c6ffb50b59ec05a19847ab8b10037d30/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RequestRateLimiterGatewayFilterFactory.java#L125

tianmingxing commented 5 years ago

In addition to modifying the response code, I also need to define the information returned. I want to be able to return what the client can parse when triggering a current limit, as shown below:

{
  "timestamp": "2019-01-20T19:55:39.814",
  "code": 1008,
  "message": "too many connections."
}
spencergibb commented 5 years ago

Does your error attributes class have the proper order? https://www.baeldung.com/spring-webflux-errors

tianmingxing commented 5 years ago

The code I've written is shown below, and the component startup sequence is the same as in the article.

20190120151439

I want to catch the exception caused by the current limit, but it doesn't seem to be a mistake, so it is not caught. But I need to customize the response content in this case, otherwise the client can't parse the recognition.

ryanjbaxter commented 5 years ago

I dont think the rate request filter is actually returning an exception when there are too many request so I dont think the handler would come into play

https://github.com/spring-cloud/spring-cloud-gateway/blob/4ea874a3c6ffb50b59ec05a19847ab8b10037d30/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RequestRateLimiterGatewayFilterFactory.java#L111

tianmingxing commented 5 years ago
  1. Sorry, I don't understand what you mean.
  2. I have to return what the client can understand. The process of processing the content is the same. It does not need to be handled differently for different situations. This will increase the processing of the response information by the client.
spencergibb commented 5 years ago

It's not currently possible since an exception isn't thrown.

tianmingxing commented 5 years ago

@spencergibb If there is an exception thrown, this problem can be easily handled, so in the face of such a realistic situation, can you provide some good suggestions? Thank you!

tianmingxing commented 5 years ago

Is there more information available on this topic so far?

spencergibb commented 5 years ago

I don't have one other than a custom filter.

spring-projects-issues commented 5 years ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

tianmingxing commented 5 years ago

@spencergibb This problem is universal and it seems to be improved as a feature in the next version. I look forward to your best solution, thank you!

huberchrigu commented 2 years ago

+1 I will have to create my own filter until this is possible.