valve

package
v0.0.0-...-cab2a4f Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2018 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Example
package main

import (
	"log"
	"net/http"
	"time"

	"github.com/keratin/throttled-valve/valve"
	"github.com/throttled/throttled"
	"github.com/throttled/throttled/store/memstore"
)

func main() {
	store, err := memstore.New(65536)
	if err != nil {
		log.Fatal(err)
	}

	// Valve will allow:
	// * 10/minute (1/6 seconds) with an additional burst of 4
	// * After two minutes: 2/minute (1/30 seconds)
	valve := valve.NewValve(store, valve.NewSchedule(4,
		valve.Entry{Rate: throttled.PerMin(10)},
		valve.Entry{Rate: throttled.PerMin(2), Delay: 2 * time.Minute},
	))

	// valve is compatible with throttled.HTTPRateLimiter
	loginLimiter := throttled.HTTPRateLimiter{
		RateLimiter: valve,
		VaryBy:      &throttled.VaryBy{RemoteAddr: true},
	}

	// you may wish to use a router and only apply the limit on specific endpoints
	var loginHandler http.HandlerFunc
	http.ListenAndServe(":8080", loginLimiter.RateLimit(loginHandler))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Rate  throttled.Rate
	Delay time.Duration
}

Entry is a configuration struct for NewSchedule. It describes a Rate that will only take effect after some Delay in maximally sustained traffic according to the previous rate.

type Schedule

type Schedule struct {
	Rates []*throttled.RateQuota
}

Schedule is configuration struct for NewValve. It comprises multiple RateQuotas that have been calculated by NewSchedule to work together in a constricting fashion.

func NewSchedule

func NewSchedule(burst int, entries ...Entry) *Schedule

NewSchedule constructs a Schedule from the set of Entries provided. The Entries are converted to throttled.RateQuota structs with burst values calculated to allow the previous rate in the Schedule to be maximally sustained during the delay.

TODO: ensure entries are ordered from most to least permissive

type Valve

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

Valve is a RateLimiter implementation comprising multiple limiters that must each be queried and maintained on every unit of work. It is intended to be used in situations where sustained work is suspicious because it is sustained, so that the sustained rate can be throttled down to a crawl.

Accomplishing this requires querying and maintaining each limiter on every call to RateLimit. A Valve with 3 limiters will take 3x longer to verify, which may be meaningful if checking a limiter requires network I/O to a centralized store.

func NewValve

func NewValve(store throttled.Store, schedule *Schedule) *Valve

NewValve builds a Valve from a Schedule

func (*Valve) RateLimit

func (q *Valve) RateLimit(key string, quantity int) (bool, throttled.RateLimitResult, error)

RateLimit will return true (valve is limited) if any of its limiters are limited. The RateLimitResult will describe the limiter with the least remaining capacity.

Jump to

Keyboard shortcuts

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