Shailesh Saxena
Shailesh Saxena

Reputation: 3512

How to limit 10 requests per second for 3 urls(including all 3) from Spring application

I am using a third party service to get RSS feed and 2 other similar services through 3 url calls.

The service has a restriction of max 10 requests per second for all 3 urls. Means if there is no call for first url, remaining 2 urls can be called 10 times in total in a second. and if there are 3 calls for 2nd url then only 7 calls allowed for remaining 2 urls in a second. I am using Tomcat 7.

Couldn't find a way to get it done in spring. If anyone can give an idea?

Thanks in advance.

Upvotes: 0

Views: 1147

Answers (1)

StanislavL
StanislavL

Reputation: 57421

I would introduce a CallLimiter spring bean - singleton. The bean should have a Queue - last 10 call time.

The bean can be added to all the 3 places where you need to call the remote API.

Before call check first time in the queue. If it's bigger than 1 second you can call the remote API and add current call time in the end of the queue. If it is less than 1 second just call Thread.currentThread().wait(50); (actually the wait time could be any you like) and repeat.

Upvotes: 2

Related Questions