rate

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

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

Go to latest
Published: May 13, 2018 License: BSD-3-Clause Imports: 1 Imported by: 2

README

Rate

Go Report Card

Usage

package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/as/rate"
	"github.com/as/rate/httprate"
)

func main() {
	lim := rate.New(time.Minute * 5)
	http.Handle("/", httprate.HandlerFunc(lim, time.Minute, nil, func(tx http.ResponseWriter, rx *http.Request) {
		tx.Write([]byte("welcome to /"))
	}))
	fmt.Println(http.ListenAndServe(":80", nil))
}

Documentation

Overview

Package rate provides a very simple rate limiter based on the passage of time.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Allow

func Allow(l Limiter, task string) bool

Allow returns true if task may execute for 1s at time.Now()

func AllowSlice

func AllowSlice(l Limiter, task string, slice time.Duration) bool

AllowSlice returns true if task may execute for the slice duration at time.Now()

func New

func New(quantum time.Duration) *limiter

New returns a limiter that allows task to run for the specified quantum Calls to Allow and AllowSlice reduce a task's available quantum if that task is allowed to run. The quantum is replenished naturally via the passage of time.

Types

type Limiter

type Limiter interface {
	// Quantum returns the duration allocated for every named task. This value is a
	// limiter-scoped, maximum-duration watermark. It does not represent the quantum
	// available for a specific task.
	//
	// If the Limiter is a aggregate of multiple Limiters, Quantum() should return the smallest
	// time.Duration in the aggregate.
	Quantum() time.Duration

	// Schedule schedules the task to run for the given time slice if there is quantum
	// available for that task.
	//
	// If the delay is <= 0 the task can run immediately and the time slice provided
	// is subtracted from the task's quantum. If delay is > 0, the caller may wait the delay
	// and attempt to schedule the task again, otherwise the task should be abandoned.
	Schedule(task string, slice time.Duration) (delay time.Duration)

	// Close closes the limiter
	Close() error
}

Limiter provides a way to schedule named tasks for execution.

Directories

Path Synopsis
Package httprate is a helper package for rate-limiting http endpoints
Package httprate is a helper package for rate-limiting http endpoints

Jump to

Keyboard shortcuts

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