Answer the question
In order to leave comments, you need to log in
How to limit API call to 3 requests per second?
The Vkontakte API has a limit on the number of requests: no more than 3 requests per second. Neither more nor less, but still you have to put up with it.
The question is: there is a PHP daemon (no criticism please, works great) that uses this API in multiple threads (for multiple users). It is necessary to somehow synchronize the call of methods from all threads to "no more than 3 per second".
The solution "on the forehead" is to use some kind of "general fast storage" to store information about calls. Read-check-call.
But perhaps there are more elegant or even ready-made solutions?
Answer the question
In order to leave comments, you need to log in
If "for several users", then you need to limit each thread separately, because the limit of 3 requests is imposed on 1 token.
Solution in php: we write a demon that will pull queries from the database (well, most likely in mysql) and execute them in turn, respectively deleting already processed queries from the table. And your old demons will put these queries into the database.
If you have a small application, then I advise you to implement it on nodejs using the ready-made vk-io library https://github.com/negezor/vk-io . This lib implements the collection of requests in execute, as a result - we can make 75 requests per second from one token.
well, users are different, just do it in each thread for a third of a second
Isn't this the concern of the web server (like nginx, apache)? I can’t tell by the settings, but it seems to me that such restrictions are put there.
on ACKA there is a so-called circuit breaker (implementing this approach) (there seems to be no account on php)
the essence is this, there is an intermediary that monitors the execution of requests, all requests are sent through this intermediary, and it monitors their execution (in case of errors, sends again, etc.).
Also, for example, when accessing it, this intermediary starts counting the number of hits within one second -> after 3 hits, starts after 1 second pauses, for example, send further requests)
as soon as there are less than one hits per second, the counter is reset.
In general, this approach allows, for example, to make 3 calls instantly, and only then turn on pauses, as well as monitor the execution of these requests, without the need to implement such logic directly in the calls themselves.
And of course, if you have such needs, then it's time to forget about PHP)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question