Skip to content

Rate limiting

Rate limiting controls the frequency of operations. The time.Ticker or time.After can be used to implement rate limiting (e.g., limit to one request per second).

Process a slice of requests at 1 per second.

package main
import (
"fmt"
"time"
)
func main() {
requests := make(chan int, 5)
for i := 1; i <= 5; i++ {
requests <- i
}
close(requests)
limiter := time.Tick(200 * time.Millisecond) // 5 per second
for req := range requests {
<-limiter
fmt.Println("request", req, time.Now())
}
}

Output (timestamps will be spaced 200ms apart)

Section titled “Output (timestamps will be spaced 200ms apart)”
Terminal window
request 1 2025-01-01 12:00:00.000 +0000 UTC
request 2 2025-01-01 12:00:00.200 +0000 UTC
request 3 2025-01-01 12:00:00.400 +0000 UTC
request 4 2025-01-01 12:00:00.600 +0000 UTC
request 5 2025-01-01 12:00:00.800 +0000 UTC