Sobes.tech
Senior

// the service uses an external API for route building, // within the tariff, we have RPS limitations. // Need to design a ratelimiter. // 100 rps type RouteClient struct { limiter } func (rc *RouteClient) Get(req string) string { // todo return rc.get(req) }

sobes.tech AI

Answer from AI

To implement a rate limiter with a limit of 100 RPS (requests per second), you can use a token bucket or a counter with a timer. In Go, it is convenient to use a channel or the golang.org/x/time/rate library.

Example using rate.Limiter from the standard library:

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

type RouteClient struct {
    limiter *rate.Limiter
}

func NewRouteClient() *RouteClient {
    // 100 requests per second
    limiter := rate.NewLimiter(100, 100) // burst equals 100
    return &RouteClient{limiter: limiter}
}

func (rc *RouteClient) Get(req string) string {
    // Wait for permission to request
    err := rc.limiter.Wait(context.Background())
    if err != nil {
        // handle error, e.g., timeout
        return "error: rate limit"
    }
    return rc.get(req)
}

func (rc *RouteClient) get(req string) string {
    // Call external API
    return "response"
}

This limiter guarantees that no more than 100 requests per second are made, while allowing short bursts (burst) up to 100 requests.