package module
Version: v2.1.0+incompatible Latest Latest

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

Go to latest
Published: May 28, 2016 License: MIT Imports: 4 Imported by: 1



Build Status GoDoc Go Report Card Coverage

rerate is a redis-based ratecounter and ratelimiter

  • Dead simple api
  • With redis as backend, multiple rate counters/limiters can work as a cluster
  • Count/Limit requests any period, 2 day, 1 hour, 5 minute or 2 second, it's up to you
  • Recording requests as a histotram, which can be used to visualize or monitor
  • Limit requests from single ip, userid, applicationid, or any other unique identifier


package main

import (


func main() {
    pool := newRedisPool("localhost:6379", "")
    // Counter
    counter := rerate.NewCounter(pool, "rl:test", 10 * time.Minute, 15 * time.Second)
    c, err := counter.Count("click")
    // Limiter
    limiter := rerate.NewLimiter(pool, "rl:test", 1 * time.Hour, 15 * time.Minute, 100)
    rem, err := limiter.Remaining("")
    exceed, err := limiter.Exceeded("")


Install rerate using the "go get" command:

go get





rerate is available under the The MIT License (MIT).




This section is empty.


This section is empty.


This section is empty.


type Counter

type Counter struct {
	// contains filtered or unexported fields

Counter count total occurs during a period, it will store occurs during every time slice interval: (now ~ now - intervl), (now - intervl ~ now - 2*intervl)...

func NewCounter

func NewCounter(pool Pool, prefix string, period, interval time.Duration) *Counter

NewCounter create a new Counter

func (*Counter) Count

func (c *Counter) Count(id string) (int64, error)

Count return total occurs in recent period

func (*Counter) Histogram

func (c *Counter) Histogram(id string) ([]int64, error)

Histogram return count histogram in recent period, order by time desc

func (*Counter) Inc

func (c *Counter) Inc(id string) error

Inc increment id's occurs with current timestamp, the count before period will be cleanup

func (*Counter) Reset

func (c *Counter) Reset(id string) error

Reset cleanup occurs, set it to zero

type Limiter

type Limiter struct {
	// contains filtered or unexported fields

Limiter a redis-based ratelimiter

func NewLimiter

func NewLimiter(pool Pool, pfx string, period, interval time.Duration, max int64) *Limiter

NewLimiter create a new redis-based ratelimiter the Limiter limits the rate to max times per period

func (*Limiter) Exceeded

func (l *Limiter) Exceeded(id string) (bool, error)

Exceeded is exceeded the rate limit or not

func (*Limiter) Remaining

func (l *Limiter) Remaining(id string) (int64, error)

Remaining return the number of requests left for the time window

type Pool

type Pool interface {
	Get() redis.Conn

Pool maintains a pool of connections. The application calls the Get method to get a connection from the pool and the connection's Close method to return the connection's resources to the pool.

Jump to

Keyboard shortcuts

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