How to Implement a Token Bucket Rate Limiter in Go

Implement a token bucket rate limiter in Go using the golang.org/x/time/rate package to control request frequency.

Use the golang.org/x/time/rate package to implement a token bucket rate limiter in Go. It provides a Limiter type that allows bursts up to a specified limit while maintaining an average rate.

package main

import (
	"context"
	"fmt"
	"time"

	"golang.org/x/time/rate"
)

func main() {
	// Allow 2 requests per second, with a burst of 4
	limiter := rate.NewLimiter(rate.Limit(2), 4)

	for i := 0; i < 5; i++ {
		if !limiter.Allow() {
			fmt.Println("Rate limit exceeded, waiting...")
			limiter.Wait(context.Background())
		}
		fmt.Printf("Request %d allowed at %v\n", i+1, time.Now())
	}
}

Install the package first with go get golang.org/x/time/rate.