A social network has API, but also it has some limitations like the amount of requests that can be done in one second (let's say API will give an error, if it accepts more than 3 requests per second)
I want to write a web application that allows me to send messages via API. However, I want to automatically do requests to API in order to get some information. Therefore, it's possible that the program will make more than 3 requests per second. In order to fix this problem, the program should handle requests in one place, so my first idea is to create a function that will accept new requests (from different places) via tokio::sync::mpsc
in infinite loop and then handle them. However, there are two issues:
- The function that starts at the beginning of the program seems to me like a poor solution
- It's tricky to return the response from API (the solution is to create
tokio::sync::oneshot
channel and send data through this channel)
Another approach is to create Context
so that we can call ctx.make_request(...).await
(we can call this function from different places of the program). And make_request
controls not to exceed limitations.
What approach is better, or maybe there are other options? (Maybe is there a design pattern that already exists?)