Files
tm_back/pkg/ratelimit/limit.go
T
2026-05-30 12:29:45 +03:30

23 lines
424 B
Go

package ratelimit
import (
"context"
"time"
"tm/pkg/redis"
)
// Allow returns whether the request is within limit for the given key.
func Allow(ctx context.Context, client redis.Client, key string, limit int64, window time.Duration) (bool, error) {
if client == nil {
return true, nil
}
count, err := client.IncrWithExpire(ctx, key, window)
if err != nil {
return false, err
}
return count <= limit, nil
}