GithubHelp home page GithubHelp logo

jessicaxiejw / adaptive_throttler Goto Github PK

View Code? Open in Web Editor NEW
31.0 1.0 2.0 14 KB

manages multiple throttlers with ability to ramp up and down

License: MIT License

Go 100.00%
golang-library golang throttle throttler rate-limiting rate-limit rate-limiter ratelimit rate-limits ratelimiter rate throttle-calls throttle-requests

adaptive_throttler's Introduction

Throttlers

A thread-safe throttler library that is:

  • generic for different use cases (e.g. controls the rate limit per host, limit database reads/writes, etc.)
  • easy to keep track of throttlers for multiple use cases
  • allows you to ramp up and ramp down request rate per throttler

Throttlers is a wrapper around the golang.org/x/time/rate library.

Installation

go get github.com/jessicaxiejw/throttlers

See godoc for in-depth explanation on the functions and parameters.

Example Usage

manager := throttlers.New(throttlers.Params{
	StartingRate: 10,	// request per second
	Burst: 5,
	LowerBound: 1,
	UpperBound: 20,
	Increment: 2,
	Decrement: 1,
})

keys := []string{"http://example.com/", "http://another-example.com/"}
for _, key := range keys {
	go func() {
		manager.Wait(key)	// waiting until request can be sent

		resp, err := http.Get(key)
		if err != nil {
			manager.Decrement(key)
		} else {
			manager.Increment(key)
		}	
	}
}
<-done

How does the package work?

When the throttlers are first created, it will use StartingRate for every key. The request rate per key is adjusted based on the Increment and Decrement call. For example, say we set

StartingRate: 10
Burst: 1
LowerBound: 1
UpperBound: 20
Increment: 2
Decrement: 1

We would at first allow 10 requests/s for a given key. Two requests were successful and we called the Increment function twice. The request rate limit was raised to StartingRate + 2 * Increment = 10 + 2 * 2 = 14 request/s.

Right after, one request wasn't successful and you called Decrement. The rate limit was now set to (current request rate) - Decrement = 14 - 1 = 13 requests/s.

We then successfully sent 4 requests, the new request rate should be (current request rate) + Increment * 4 = 13 + 2 * 4 = 21 requests/s. However, because the UpperBound was set to 20 requests/s, the new request rate actually became 20 requests/s.

Say there were 20 unsuccessful requests in series, the new request rate would hit the LowerBound, which was 1 requests/s.

Can the package only be used for handling HTTP requests?

No.

Even though the package was originally created for keeping track of throttle limits for different hosts, it is designed to be generic for any types of throttling. For example, you can use it for rate limiting a database's read and write per user. The key will be the user name.

adaptive_throttler's People

Contributors

jessicaxiejw avatar sepetrov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

jpfaria sepetrov

adaptive_throttler's Issues

Cannot go get package

The module name in go.mod does not match the URL.

$ go get github.com/jessicaxiejw/adaptive_throttler        
go: downloading github.com/jessicaxiejw/adaptive_throttler v0.0.0-20190508181346-27621fed9271
go: github.com/jessicaxiejw/adaptive_throttler upgrade => v0.0.0-20190508181346-27621fed9271
go get: github.com/jessicaxiejw/[email protected]: parsing go.mod:
        module declares its path as: github/jessicaxiejw/adaptive_throttler
                but was required as: github.com/jessicaxiejw/adaptive_throttler

The package name throttlers is also different from the URL segment that corresponds to the package name.

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.