This app:
- Uses the most efficient way of querying the API which is keeping as near as possible to the 5 minutes minimum interval allowed by the API provider.
- Can respect the API constraint if it crashes and is restarted, since it takes note of its operations on a persistent database.
- Can operate if the database is corrupt.
- Takes into account CPU clock errors and avoids banning due to them.
- Takes into account possible network or API infrastructure poor performance during the communication.
- Optimizes wait times based on the last response or query dates, on restart too.
- Make sure you have Node.js v12.14.0 or higher and npm 6.13.4 or higher installed on your machine.
- Open a terminal window and
cd
into the project folder. - Run
npm install
and wait for it to complete.
cd
into the project folder.- Run
npm run start
.
All the source files are located in the src
folder: index.js
contains all the important logic, start.js
imports index.js
and boots the application.
In this app I could have set an interval to do the polling, like this:
setInterval(doThePolling, 1000 * 60 * 5);
but I decided to take another direction. Here I explain why.
Let's examine the polling flow:
- Our app sends message through network to their API endpoint.
- Their app receives message from network.
- Their app computes whether we satisfy the minimum interval constraint.
- Their app sends response to network.
- Our app receives the response.
Between point 1 and 3 there can be variable time amounts taken by our requests, depending on the network performance, traffic going through their infrastructure and how much their machines are busy handling other tasks at each of our requests. Sending requests at regular intervals with solutions like setInterval
can cause problems in situations like the following:
- Our app sends a message at 00:00:00.
- The network performs poorly, their infrastructure is saturated and their app checks whether we comply with the constraint at 00:00:10 (ten seconds later) and responds at 00:00:11.
- Our app sends a second message at 00:05:00 (five minutes after our first message was sent).
- The networks performs better and their infrastructure is less busy, so their app checks on our constraint compliance at 00:05:01 (1 second later it was sent). Their app will detect that only 4 minutes and 51 seconds have passed since our initial message. At this point we are banned.
Knowing for certain that the amount of time between point 1 and 3 is always less or equal to the time between point 1 and 5, if we wait for point 5 (the app gets the response) to happen before setting the wait time for the next polling call we are sure to avoid the just mentioned banning situation.