ratelimit

package module
v0.0.0-...-066bb41 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2018 License: MIT Imports: 4 Imported by: 0

README

The golang ratelimit package based on redis-cell

##Example

import "fmt"
import "github.com/leeeboo/ratelimit"

func test() {

    key := "user-send-message-rate"
    capacity := 10
    countPerPeriod := 5
    period := 60

    b := ratelimit.NewBucket(key, capacity, countPerPeriod, period)

    res, err := b.Take(1)

    if !res.Allow {

        fmt.Fprintln("Request limited. Waiting for %d seconds", res.WaitSecond)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	Key            string
	Capacity       int
	CountPerPeriod int
	Period         int
}

func NewBucket

func NewBucket(key string, capacity int, countPerPeriod int, period int) *Bucket

func (*Bucket) Take

func (this *Bucket) Take(quota int) (*Result, error)

type Config

type Config struct {
	Host string `env:"RATELIMIT_REDIS_HOST"`
	Port int    `env:"RATELIMIT_REDIS_PORT"`
	Db   int    `env:"RATELIMIT_REDIS_DB"`
}

type Result

type Result struct {
	Allow        bool
	Capacity     int
	LeftQuota    int
	WaitSecond   int
	RefullSecond int
}

Jump to

Keyboard shortcuts

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