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 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
⌂ 换网址