Senior
How to limit the number of polling worker calls if the API is paid?
sobes.tech AI
Answer from AI
To limit the number of polling worker calls when working with a paid API, several approaches can be used:
-
Token Bucket or Rate Limiter
- Store a call counter and the time of the last reset.
- Check before each call whether the limit has been exceeded.
-
Scheduling calls with delays
- Use timers or sleep to maintain the required interval between calls.
-
Storing state in memory or external storage
- If the worker is distributed, the limit state should be stored in Redis or a database.
Example in Go with a simple rate limiter:
package main
import (
"fmt"
"time"
)
func main() {
maxCalls := 5
interval := time.Minute
calls := 0
resetTime := time.Now().Add(interval)
for {
now := time.Now()
if now.After(resetTime) {
calls = 0
resetTime = now.Add(interval)
}
if calls < maxCalls {
// API call
fmt.Println("Calling API")
calls++
} else {
fmt.Println("Rate limit reached, waiting...")
time.Sleep(time.Until(resetTime))
continue
}
// Wait between calls if needed
time.Sleep(10 * time.Second)
}
}
This approach allows controlling the number of requests and not exceeding the limits of the paid API.