Coder Social home page Coder Social logo

isgasho / throttled Goto Github PK

View Code? Open in Web Editor NEW

This project forked from throttled/throttled

0.0 1.0 0.0 7.76 MB

Package throttled implements rate limiting access to resources such as HTTP endpoints.

License: BSD 3-Clause "New" or "Revised" License

Shell 0.52% Go 99.07% Makefile 0.41%

throttled's Introduction

Throttled Build Status Go Reference

Package throttled implements rate limiting using the generic cell rate algorithm to limit access to resources such as HTTP endpoints.

The 2.0.0 release made some major changes to the throttled API. If this change broke your code in problematic ways or you wish a feature of the old API had been retained, please open an issue. We don't guarantee any particular changes but would like to hear more about what our users need. Thanks!

Installation

Go Modules are required to use Throttled (check that there's a go.mod in your package's root). Import Throttled:

import (
	"github.com/throttled/throttled/v2"
)

Then any of the standard Go tooling like go build, go test, will find the package automatically.

You can also pull it into your project using go get:

go get -u github.com/throttled/throttled/v2

Upgrading from the pre-Modules version

The current /v2 of Throttled is perfectly compatible with the pre-Modules version of Throttled, but when upgrading, you'll have to add /v2 to your imports. Sorry about the churn, but because Throttled was already on its semantic version 2 by the time Go Modules came around, its tooling didn't play nice because it expects the major version in the path to match the major in its tags.

Documentation

API documentation is available on godoc.org.

Usage

This example demonstrates the usage of HTTPLimiter for rate-limiting access to an http.Handler to 20 requests per path per minute with bursts of up to 5 additional requests:

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/throttled/throttled/v2"
	"github.com/throttled/throttled/v2/store/memstore"
)

func myHandlerFunc(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, world")
}

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

	quota := throttled.RateQuota{
		MaxRate:  throttled.PerMin(20),
		MaxBurst: 5,
	}
	rateLimiter, err := throttled.NewGCRARateLimiter(store, quota)
	if err != nil {
		log.Fatal(err)
	}

	httpRateLimiter := throttled.HTTPRateLimiter{
		RateLimiter: rateLimiter,
		VaryBy:      &throttled.VaryBy{Path: true},
	}

	handler := http.HandlerFunc(myHandlerFunc)
	http.ListenAndServe(":8080", httpRateLimiter.RateLimit(handler))
}

Related Projects

See throttled/gcra for a list of other projects related to rate limiting and GCRA.

Release

  1. Update CHANGELOG.md. Please use semantic versioning and the existing conventions established in the file. Commit the changes with a message like Bump version to 2.2.0.
  2. Tag master with a new version prefixed with v. For example, v2.2.0.
  3. git push origin master --tags.
  4. Publish a new release on the releases page. Copy the body from the contents of CHANGELOG.md for the version and follow other conventions from previous releases.

License

The BSD 3-clause license. Copyright (c) 2014 Martin Angers and contributors.

throttled's People

Contributors

brandur avatar mna avatar andrew-stripe avatar metcalf avatar amallek avatar beatrichartz avatar dvic avatar jason-costello avatar jpcoenen avatar kevburnsjr avatar marfi avatar groovili avatar roshan-gorasia-cko avatar mehcode avatar simonbarendse avatar majewsky avatar tback avatar yazinsai avatar zwass avatar andycook-bcs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.