Giter Club home page Giter Club logo

throttlers's Introduction

Adaptive Throttler

A thread-safe throttler library that

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

The adaptive throttler is a wrapper around the golang.org/x/time/rate library.

Installation

go get github.com/jessicaxiejw/adaptive_throttler

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 successfully 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.

throttlers's People

Contributors

jessicaxiejw avatar sepetrov avatar

Stargazers

 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.