ratelimitx

package module
v0.0.0-...-0f3c1f9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2018 License: MIT Imports: 6 Imported by: 0

README

ratelimitx

Build Status Coverage

A simple ratelimit for golang, implemented with memcache and gobreak, aims on high availability.

test results

PASS
coverage: 85.2% of statements
ok  	ratelimitx	1.811s
Success: Tests passed.

benchmark results

$ go test -bench=.
goos: windows
goarch: amd64
pkg: ratelimitx
BenchmarkAllowSecond-4                                      5000            253200 ns/op
BenchmarkAllowMinute-4                                      5000            237330 ns/op
BenchmarkAllowHour-4                                       10000            247800 ns/op
BenchmarkMemcacheUnavailableWithFallback-4               1000000              1075 ns/op
BenchmarkMemcacheUnavailableWithoutFallback-4            2000000               918 ns/op
PASS
ok      go_third_party/app/lib/ratelimitx       12.950s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter struct {

	// Optional fallback limiter used when Memcache is unavailable.
	Fallback *rate.Limiter
	// contains filtered or unexported fields
}

Limiter controls how frequently events are allowed to happen.

func New

func New(server ...string) *Limiter

New creates a Limiter

func NewWithMemcache

func NewWithMemcache(client *memcache.Client) *Limiter

NewWithMemcache creats a Limiter

func (*Limiter) Allow

func (l *Limiter) Allow(name string, maxn uint64, dur time.Duration) (count uint64, delay time.Duration, allow bool)

Allow is shorthand for AllowN(name, max, dur, 1).

func (*Limiter) AllowHour

func (l *Limiter) AllowHour(name string, maxn uint64) (count uint64, delay time.Duration, allow bool)

AllowHour is shorthand for Allow(name, maxn, time.Hour).

func (*Limiter) AllowMinute

func (l *Limiter) AllowMinute(name string, maxn uint64) (count uint64, delay time.Duration, allow bool)

AllowMinute is shorthand for Allow(name, maxn, time.Minute).

func (*Limiter) AllowN

func (l *Limiter) AllowN(name string, maxn uint64, dur time.Duration, n int64) (count uint64, delay time.Duration, allow bool)

AllowN reports whether an event with given name may happen at time now. It allows up to maxn events within duration dur, with each interaction incrementing the limit by n.

func (*Limiter) AllowRate

func (l *Limiter) AllowRate(name string, rateLimit rate.Limit) (delay time.Duration, allow bool)

AllowRate reports whether an event may happen at time now. It allows up to rateLimit events each second.

func (*Limiter) Reset

func (l *Limiter) Reset(name string, dur time.Duration) error

Reset resets the rate limit for the name in the given rate limit window.

func (*Limiter) ResetRate

func (l *Limiter) ResetRate(name string, rateLimit rate.Limit) error

ResetRate resets the rate limit for the name and limit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL