ratelimiter

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2020 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Rate Limiter

The ratelimiter service type creates a rate limiter with specified limit. When it is used in the step, it applies limit against supplied token. If a spikeThreshold is specified then traffic will be blocked with a probability (that has an exponential decay) when a traffic spike occurs above the spikeThreshold multiple.

The available service settings are as follows:

Name Type Description
limit string Limit can be specifed in the format of "limit-period". Valid periods are 'S', 'M' & 'H' to represent Second, Minute & Hour. Example: "10-S" represents 10 request/second
spikeThreshold decimal Multiple above base traffic load which triggers the spike block logic. Spike blocking is disabled by default.
decayRate decimal Exponential decay rate for the spike blocking probability. Default .01

The available input for the request are as follows:

Name Type Description
token string Token for which rate limit has to be applied

The available response outputs are as follows:

Name Type Description
limitReached bool If the limit exceeds
limitAvailable integer Available limit
error bool If any error occured while applying the rate limit
errorMessage string The error message

A sample service definition is:

{
    "name": "RateLimiter",
    "description": "Rate limiter",
    "ref": "github.com/project-flogo/microgateway/activity/ratelimiter",
    "settings": {
        "limit": "5-M"
    }
}

An example step that invokes the above ratelimiter service to consume a token is:

{
    "service": "RateLimiter",
    "input": {
        "token": "=$.payload.headers.Token"
    }
}

Note: When token is not supplied or empty, service sets error to true. This can be handled by configuring token to some constant value, In this way service can be operated as global rate limiter. An example shown below:

{
    "service": "RateLimiter",
    "input": {
        "token": "MY_GLOBAL_TOKEN"
    }
}

Utilizing and extracting the response values can be seen in both a conditional evaluation:

{"if": "$.RateLimiter.outputs.limitReached == true"}

and a response handler:

{
    "if": "$.RateLimiter.outputs.limitReached == true",
    "error": true,
    "output": {
        "code": 403,
        "data": {
            "status":"Rate Limit Exceeded - The service you have requested is over the allowed limit."
        }
    }
}

Documentation

Index

Constants

View Source
const (
	// MemorySize is the size of the circular buffer holding the request times
	MemorySize = 256
)

Variables

This section is empty.

Functions

func New

New creates a new rate limiter

Types

type Activity

type Activity struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Activity is a rate limiter service Limit can be specified in the format "<limit>-<period>"

Valid periods: * "S": second * "M": minute * "H": hour

Examples: * 5 requests / second : "5-S" * 5 requests / minute : "5-M" * 5 requests / hour : "5-H"

func (*Activity) Eval

func (a *Activity) Eval(ctx activity.Context) (done bool, err error)

Eval executes the activity

func (*Activity) Metadata

func (a *Activity) Metadata() *activity.Metadata

Metadata return the metadata for the activity

type Context

type Context struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Context is a token context

type Input

type Input struct {
	Token string `md:"token,required"`
}

Input is the input for the rate limiter

func (*Input) FromMap

func (r *Input) FromMap(values map[string]interface{}) error

FromMap converts the settings from a map of settings

func (*Input) ToMap

func (r *Input) ToMap() map[string]interface{}

ToMap converts the settings to a map from a struct

type Output

type Output struct {
	LimitReached   bool   `md:"limitReached"`
	LimitAvailable int64  `md:"limitAvailable"`
	Error          bool   `md:"error"`
	ErrorMessage   string `md:"errorMessage"`
}

Output is the output of the rate limiter

func (*Output) FromMap

func (o *Output) FromMap(values map[string]interface{}) error

FromMap converts the output from a map to a struct

func (*Output) ToMap

func (o *Output) ToMap() map[string]interface{}

ToMap converts the output to a map from a struct

type Settings

type Settings struct {
	Limit          string  `md:"limit,required"`
	SpikeThreshold float64 `md:"spikeThreshold"`
	DecayRate      float64 `md:"decayRate"`
}

Settings are the settings for the rate limiter

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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