ratelimiter

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2017 License: MIT Imports: 4 Imported by: 1

README

Gear-Ratelimiter

Smart rate limiter middleware for Gear, base on redis or memory limiter.

Build Status Coverage Status License GoDoc

Installation

go get github.com/teambition/gear-ratelimiter
import "github.com/teambition/gear-ratelimiter"

Demo

import (
  "github.com/teambition/gear-ratelimiter"
  redisClient "github.com/teambition/gear-ratelimiter/redis"
  redis "gopkg.in/redis.v5"
)

limiter := ratelimiter.New(&ratelimiter.Options{
  Client: redisClient.NewRedisClient(&redis.Options{Addr: "127.0.0.1:6379"})
  GetID: func(ctx *gear.Context) string {
    return "user-123465"
  },
  Max:      10,
  Duration: time.Minute, // limit to 1000 requests in 1 minute.
  Policy: map[string][]int{
    "/":      []int{16, 6 * 1000},
    "GET /a": []int{3, 5 * 1000, 10, 60 * 1000},
    "GET /b": []int{5, 60 * 1000},
    "/c":     []int{6, 60 * 1000},
  },
})
app.UseHandler(limiter)

API

ratelimiter.New(ratelimiter.Options)

returns a Gear middleware handler.

  • options.Client: Optional, a wrapped redis client. if omit, it will use memory limiter.
  • options.Max: Optional, Type: int, The max count in duration and using it when limiter cannot found the appropriate policy, default to 100.
  • options.Prefix: Optional, Type: String, redis key namespace, default to LIMIT.
  • options.Duration: Optional, {Number}, of limit in milliseconds, default to 3600000
  • options.GetID: Optional, {Function}, generate a identifier for requests, default to user's IP
  • options.Policy: Required, {map[string][]int}, limit policy

Example

Try into github.com/teambition/gear-ratelimiter directory:

go run example/main.go

License

Gear-Ratelimiter is licensed under the MIT license. Copyright © 2016-2017 Teambition.

Documentation

Index

Constants

View Source
const Version = "1.0.1"

Version is ratelimiter's version

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Key prefix, default is "LIMIT:".
	Prefix string
	// The max count in duration for no policy, default is 100.
	Max int
	// Count duration for no policy, default is 1 Minute.
	Duration time.Duration
	// Policy is a map of custom limiter policy.
	Policy map[string][]int
	// GetID returns limiter id for a request.
	GetID func(ctx *gear.Context) string
	// Use a redis client for limiter, if omit, it will use a memory limiter.
	Client baselimiter.RedisClient
}

Options for Limiter

type RateLimiter

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

RateLimiter ...

func New

func New(opts *Options) (l *RateLimiter)

New ...

func (*RateLimiter) Serve

func (l *RateLimiter) Serve(ctx *gear.Context) error

Serve ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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