Senior
Design a rate limiter for an external API with a limit of 100 requests per second/minute
sobes.tech AI
Answer from AI
To implement a rate limiter with a limit of 100 requests per second or minute on an external API in Go, you can use the Token Bucket or Leaky Bucket algorithm. One of the simplest ways is to use the golang.org/x/time/rate library.
Example with a limit of 100 requests per second:
package main
import (
"context"
"fmt"
"golang.org/x/time/rate"
"time"
)
func main() {
limiter := rate.NewLimiter(100, 100) // 100 requests per second, with a "bucket" of 100 tokens
for i := 0; i < 105; i++ {
if err := limiter.Wait(context.Background()); err != nil {
fmt.Println("Wait error:", err)
return
}
fmt.Printf("Request %d sent at %v\n", i+1, time.Now())
}
}
For a per-minute limit, you can create a limiter with a frequency of 100/60 requests per second:
limiter := rate.NewLimiter(rate.Every(time.Minute/100), 100)
Main points:
rate.NewLimitertakes the rate and the bucket size.- The
Waitmethod blocks execution until it is possible to send a request. - You can use the non-blocking
Allowmethod to check if a request can be sent without waiting.
This approach allows flexible control over the request rate to an external API.