Documentation ¶
Index ¶
- Variables
- func NowFuncFromContextForTest(ctx context.Context) (func() time.Time, bool)
- func WithNowFuncForTest(ctx context.Context, nowFunc func() time.Time) context.Context
- type AllowRequest
- type Driver
- type DriverFunc
- type GormDriver
- type KV
- type RateLimiter
- type RedisDriver
- type Reservation
- type ReserveRequest
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrInvalidParameters = errors.New("ratelimiter: invalid parameters")
View Source
var Test = false
Functions ¶
func NowFuncFromContextForTest ¶ added in v0.2.0
Types ¶
type AllowRequest ¶
type Driver ¶
type Driver interface {
Reserve(ctx context.Context, req *ReserveRequest) (*Reservation, error)
}
type DriverFunc ¶
type DriverFunc func(ctx context.Context, req *ReserveRequest) (*Reservation, error)
func (DriverFunc) Reserve ¶
func (f DriverFunc) Reserve(ctx context.Context, req *ReserveRequest) (*Reservation, error)
type GormDriver ¶ added in v0.2.0
type GormDriver struct {
// contains filtered or unexported fields
}
func InitGormDriver ¶ added in v0.2.0
InitGormDriver initializes a GormDriver with the provided Gorm DB. Sometimes you may not need to auto migrate the KV table, you can use `NewGormDriver` instead.
func NewGormDriver ¶ added in v0.2.0
func NewGormDriver(db *gorm.DB) *GormDriver
NewGormDriver returns a Driver that uses Gorm as the storage. Sometimes you may need to auto migrate the KV table, you can use `InitGormDriver` instead.
Example ¶
limiter := New( NewGormDriver(db), ) runExample(limiter, "ExampleNewGormDriver")
Output: 0s: allowed: true 1m0s: allowed: true 2m0s: allowed: true 3m0s: allowed: true 4m0s: allowed: true 5m0s: allowed: false , you can retry after 5m0s 6m0s: allowed: false , you can retry after 4m0s 7m0s: allowed: false , you can retry after 3m0s 8m0s: allowed: false , you can retry after 2m0s 9m0s: allowed: false , you can retry after 1m0s 10m0s: allowed: true 11m0s: allowed: false , you can retry after 9m0s 12m0s: allowed: false , you can retry after 8m0s 13m0s: allowed: false , you can retry after 7m0s 14m0s: allowed: false , you can retry after 6m0s 15m0s: allowed: false , you can retry after 5m0s 16m0s: allowed: false , you can retry after 4m0s 17m0s: allowed: false , you can retry after 3m0s 18m0s: allowed: false , you can retry after 2m0s 19m0s: allowed: false , you can retry after 1m0s 20m0s: allowed: true 21m0s: allowed: false , you can retry after 9m0s 22m0s: allowed: false , you can retry after 8m0s 23m0s: allowed: false , you can retry after 7m0s 24m0s: allowed: false , you can retry after 6m0s --- Sleep 20 minutes --- 45m0s: allowed: true 46m0s: allowed: true 47m0s: allowed: false , you can retry after 3m0s 48m0s: allowed: false , you can retry after 2m0s 49m0s: allowed: false , you can retry after 1m0s 50m0s: allowed: true 51m0s: allowed: false , you can retry after 9m0s 52m0s: allowed: false , you can retry after 8m0s 53m0s: allowed: false , you can retry after 7m0s 54m0s: allowed: false , you can retry after 6m0s --- Sleep 100 minutes --- 2h35m0s: allowed: true 2h36m0s: allowed: true 2h37m0s: allowed: true 2h38m0s: allowed: true 2h39m0s: allowed: true 2h40m0s: allowed: false , you can retry after 5m0s 2h41m0s: allowed: false , you can retry after 4m0s 2h42m0s: allowed: false , you can retry after 3m0s 2h43m0s: allowed: false , you can retry after 2m0s 2h44m0s: allowed: false , you can retry after 1m0s
func (*GormDriver) Reserve ¶ added in v0.2.0
func (d *GormDriver) Reserve(ctx context.Context, req *ReserveRequest) (*Reservation, error)
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
func New ¶
func New(driver Driver) *RateLimiter
func (*RateLimiter) Allow ¶
func (lim *RateLimiter) Allow(ctx context.Context, req *AllowRequest) (bool, error)
func (*RateLimiter) Reserve ¶
func (lim *RateLimiter) Reserve(ctx context.Context, req *ReserveRequest) (*Reservation, error)
type RedisDriver ¶ added in v0.1.0
type RedisDriver struct {
// contains filtered or unexported fields
}
func InitRedisDriver ¶ added in v0.1.0
func InitRedisDriver(ctx context.Context, client *redis.Client) (*RedisDriver, error)
Example ¶
d, err := InitRedisDriver(context.Background(), redisCli) if err != nil { panic(err) } limiter := New(d) runExample(limiter, "ExampleInitRedisDriver")
Output: 0s: allowed: true 1m0s: allowed: true 2m0s: allowed: true 3m0s: allowed: true 4m0s: allowed: true 5m0s: allowed: false , you can retry after 5m0s 6m0s: allowed: false , you can retry after 4m0s 7m0s: allowed: false , you can retry after 3m0s 8m0s: allowed: false , you can retry after 2m0s 9m0s: allowed: false , you can retry after 1m0s 10m0s: allowed: true 11m0s: allowed: false , you can retry after 9m0s 12m0s: allowed: false , you can retry after 8m0s 13m0s: allowed: false , you can retry after 7m0s 14m0s: allowed: false , you can retry after 6m0s 15m0s: allowed: false , you can retry after 5m0s 16m0s: allowed: false , you can retry after 4m0s 17m0s: allowed: false , you can retry after 3m0s 18m0s: allowed: false , you can retry after 2m0s 19m0s: allowed: false , you can retry after 1m0s 20m0s: allowed: true 21m0s: allowed: false , you can retry after 9m0s 22m0s: allowed: false , you can retry after 8m0s 23m0s: allowed: false , you can retry after 7m0s 24m0s: allowed: false , you can retry after 6m0s --- Sleep 20 minutes --- 45m0s: allowed: true 46m0s: allowed: true 47m0s: allowed: false , you can retry after 3m0s 48m0s: allowed: false , you can retry after 2m0s 49m0s: allowed: false , you can retry after 1m0s 50m0s: allowed: true 51m0s: allowed: false , you can retry after 9m0s 52m0s: allowed: false , you can retry after 8m0s 53m0s: allowed: false , you can retry after 7m0s 54m0s: allowed: false , you can retry after 6m0s --- Sleep 100 minutes --- 2h35m0s: allowed: true 2h36m0s: allowed: true 2h37m0s: allowed: true 2h38m0s: allowed: true 2h39m0s: allowed: true 2h40m0s: allowed: false , you can retry after 5m0s 2h41m0s: allowed: false , you can retry after 4m0s 2h42m0s: allowed: false , you can retry after 3m0s 2h43m0s: allowed: false , you can retry after 2m0s 2h44m0s: allowed: false , you can retry after 1m0s
func (*RedisDriver) Reserve ¶ added in v0.1.0
func (d *RedisDriver) Reserve(ctx context.Context, req *ReserveRequest) (*Reservation, error)
type Reservation ¶
func (*Reservation) Delay ¶
func (r *Reservation) Delay() time.Duration
func (*Reservation) RetryAfter ¶
func (r *Reservation) RetryAfter() time.Duration
func (*Reservation) RetryAfterFrom ¶
func (r *Reservation) RetryAfterFrom(t time.Time) time.Duration
Click to show internal directories.
Click to hide internal directories.