Answer the question
In order to leave comments, you need to log in
What is the best way to implement the collection of data by the handler for the subsequent request to the api in php?
php nginx apache2
Hello. The task is that requests come to my handler (php script) via http, then I parse the request data and make requests to external api.
Bottom line - the limit of requests to the external api method is 3 requests per second.
As it always happens, unexpectedly, thousands of requests per hour rained down on my service, and it often turns out that there are more than 3 of these requests in 1 second. Anything over the limit falls off with an error.
At the same time, the external api provides the ability to accept a combination of requests; up to 10 requests to methods can fit in one call.
What is the best way to implement the collection of requests into a group and subsequent work with them?
Linux server with full root.
I considered the option of storing queries in a mysql table and a cron handler, but parsing once a minute (as far as cron allows) is too long. It is necessary to process requests within 1-5 seconds.
Answer the question
In order to leave comments, you need to log in
You can use - https://github.com/bandwidth-throttle/token-bucket
It will allow you to limit the number of requests per second to an external service. It will turn out like this - an external request comes in, you immediately try to proxy it to the service. Before the request, try to get the token $consumer->consume(1); if the token is received, then there is not yet 3x per second, if not received, then there will be a delay until the nearest possible time.
If you need to respond to a request to your service as quickly as possible, without waiting for a response from an external service, then you need to use some kind of queue service. An external request will add a message to the queue, and your service will already have a worker that will immediately try to process it.
These options do not use the function of sending 10 requests, but I think that this is enough for 1000 requests per hour.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question