23 lines
424 B
Go
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
|
|
}
|