ratelimit

package module
v0.0.0-...-faa118d Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2014 License: MIT Imports: 3 Imported by: 0

README

ratelimit

GoDoc

Rate Limiter for Go, backed by Redis.

View the docs.

Dependencies

Features

  • Utilises connection pool from Redigo
  • Handles race conditions
  • Efficient
  • Distributed Store: coming soon

Installation

$ go get github.com/r8k/ratelimit

Example

package main

import "fmt"
import "net"
import "github.com/r8k/ratelimit"

func main() {
    store, err := ratelimit.Init(&net.TCPAddr{Port: 6379})
    if err != nil {
        panic(err)
    }
    defer store.Close()

    // Get `RateLimit` for `identifier` client_ip
    Limit, err := store.Get("client_ip")
    if err != nil {
        panic(err)
    }

    fmt.Printf("Quota: %d\n", Limit.Quota)
    fmt.Printf("Used: %d\n", Limit.Used)
    fmt.Printf("Remaining: %d\n", Limit.Remaining)
    fmt.Printf("Retry After: %s\n", Limit.RetryAfter)
}

Run the above example

❯ go run main.go

Quota: 5000
Used: 1
Remaining: 4999
Retry After: 2014-08-09 17:14:55 +0530 IST
```

## Benchmark

❯ go test -bench=.

PASS BenchmarkGetSequential 10000 190953 ns/op BenchmarkGetParallel 100 19507315 ns/op ok github.com/r8k/ratelimit 3.954s


## License

MIT

Documentation

Index

Constants

View Source
const IdleTimeout = 240 * time.Second
View Source
const LimitInterval = 3600 * time.Second
View Source
const MaxIdle = 40
View Source
const MaxQuota = 5000

module constants

View Source
const MilliSecond = int64(time.Millisecond)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	// contains filtered or unexported fields
}

`Bucket` defines the model for `Quota`, `Used` & `Remaining`

type Limit

type Limit struct {
	Quota      int
	Used       int
	Remaining  int
	RetryAfter time.Time
}

`Limit` struct defines the model of `Limiter`. essential fields include:

  • `Quota`
  • `Used`
  • `Remaining`
  • `RetryAfter` epoch timestamp

type RedisLimiter

type RedisLimiter struct {
	Pool            *redis.Pool
	PrefixQuota     string
	PrefixRemaining string
	PrefixReset     string
	Duration        time.Duration
	Quota           int
}

`RedisLimiter` Limits connections based on a max quota that they (`end_user_of_api`) are entitled to, and the quota that they have already consumed. Useful for Web Applications, to avoid abuse of APIs etc, by limiting everyone to a max quota of ex: 5000 requests per hour per `identifier`. The `identifier` has to be choosen by the `user` cosuming `RedisLimiter`

func Init

func Init(address net.Addr) (*RedisLimiter, error)

Init returns a new RedisLimiter. Options:

  • `address` net.Addr

@return *RedisLimiter, error

func (*RedisLimiter) Close

func (s *RedisLimiter) Close() error

Close closes the underlying *redis.Pool

func (*RedisLimiter) Get

func (s *RedisLimiter) Get(id string) (*Limit, error)

Get `Limit` for an `identifier` from Redis and implicitly apply `RateLimit` calculations against this `identifier` back into Redis.

type RetryAfter

type RetryAfter struct {
	// contains filtered or unexported fields
}

`RetryAfter` defines for `self`

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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