'Polling two APIs in a single infinite for loop but with different delay [closed]
I have two APIs on a server. Let's say API A and API B. I want to call API A every 3 seconds and API B every 200 seconds. I have coded the program in the following structure:
- Main: that handles authentication and handles API calls.
- API-A: that calls API A and processes its data
- API-B: that calls API B and processes its data.
Can anyone tell me how can I implement both of the API calls in a single program (Main). I am running a single for loop for API A with sleeping it for 3 seconds now I want to fit the API B with its condition of sleeping.
I want both of them to run simultaneously with their condition, while both be working in one program, main as it is handling authentication and I don't want to make two seperate programs for these two APIs.
Solution 1:[1]
You can set up two timers and wait for events on both channels in a loop.
aTicker := time.NewTicker(time.Second * 3)
bTicker := time.NewTicker(time.Second * 200)
for {
select {
case <-aTicker.C:
callApiA()
case <-bTicker.C:
callApiB()
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AJcodez |
