fastlimiter

package module
v0.0.0-...-8f3fdc5 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2016 License: MIT Imports: 3 Imported by: 0

README

fastlimiter

The fastest abstract rate limiter, base on memory

Build Status Coverage Status License GoDoc

Installation

go get github.com/mushroomsir/fastlimiter

Example

Try into github.com/teambition/fastlimiter directory:

go run examples/main.go

Visit: http://127.0.0.1:8080/a

package main

import (
	"fmt"
	"html"
	"log"
	"net/http"
	"strconv"
	"time"

	"github.com/mushroomsir/fastlimiter"
)

func main() {

	limiter := fastlimiter.New(fastlimiter.Options{})

	http.HandleFunc("/a", func(w http.ResponseWriter, r *http.Request) {
		policy := []int{3, 30000, 2, 60000}
		res, err := limiter.Get(r.URL.Path, policy...)
		if err != nil {
			http.Error(w, err.Error(), 500)
			return
		}

		header := w.Header()
		header.Set("X-Ratelimit-Limit", strconv.FormatInt(int64(res.Total), 10))
		header.Set("X-Ratelimit-Remaining", strconv.FormatInt(int64(res.Remaining), 10))
		header.Set("X-Ratelimit-Reset", strconv.FormatInt(res.Reset.Unix(), 10))

		if res.Remaining >= 0 {
			w.WriteHeader(200)
			fmt.Fprintf(w, "Path: %q\n", html.EscapeString(r.URL.Path))
			fmt.Fprintf(w, "Remaining: %d\n", res.Remaining)
			fmt.Fprintf(w, "Total: %d\n", res.Total)
			fmt.Fprintf(w, "Duration: %v\n", res.Duration)
			fmt.Fprintf(w, "Reset: %v\n", res.Reset)
		} else {
			after := int64(res.Reset.Sub(time.Now())) / 1e9
			header.Set("Retry-After", strconv.FormatInt(after, 10))
			w.WriteHeader(429)
			fmt.Fprintf(w, "Rate limit exceeded, retry in %d seconds.\n", after)
		}
	})
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Benchmark and Test

go test -bench="."
BenchmarkGet-4                            100000               409 ns/op              96 B/op          3 allocs/op
BenchmarkGetAndEexceeding-4               100000               379 ns/op              96 B/op          3 allocs/op
BenchmarkGetAndParallel-4                 100000               389 ns/op              96 B/op          3 allocs/op
BenchmarkGetAndClean-4                     10000               399 ns/op              96 B/op          3 allocs/op
BenchmarkGetForDifferentUser-4             10000              1600 ns/op             466 B/op          8 allocs/op
PASS
ok      github.com/mushroomsir/fastlimiter      6.121s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FastLimiter

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

FastLimiter ...

func New

func New(opts Options) (limiter *FastLimiter)

New ...

func (*FastLimiter) Clean

func (l *FastLimiter) Clean()

Clean ...

func (*FastLimiter) Count

func (l *FastLimiter) Count() int

Count ...

func (*FastLimiter) Get

func (l *FastLimiter) Get(id string, policy ...int) (result Result, err error)

Get ...

func (*FastLimiter) Remove

func (l *FastLimiter) Remove(id string) error

Remove ...

type Options

type Options struct {
	Prefix        string
	Max           int
	CleanDuration time.Duration
	Duration      time.Duration
}

Options ...

type Result

type Result struct {
	Total     int
	Remaining int
	Duration  time.Duration
	Reset     time.Time
}

Result of limiter

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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