Skip to content

Rate Limit is a Go API providing rate limited functionalities.

License

Notifications You must be signed in to change notification settings

tgirier/ratelimit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rate-limit

import "github/tgirier/ratelimit"

Rate Limit is a Go API providing rate limited functionalities.

You can choose from to various types according to your needs. Each type comes with a set of rate-limited methods.

Built with ❤️ in Paris.

🙌 A huge thanks to @bitfield for his mentoring on this project. 🙌

Installation

To import the API within your Go program, simply add the following statement to your go package:

import "github/tgirier/ratelimit"

or (if you want to you use the proxy sub-package):

import "github.com/tgirier/ratelimit/proxy"

Usage

Rate Limit regroups two packages:

  • Main rate limit package
  • Proxy sub package

Main package

Two rate limited types are available within the main package:

  • Worker (general purpose): executes a given function at a given rate.
  • HTTPClient (http only): executes HTTP requests at a given rate

Both types need to be initialized using the corresponding constructor. It enables the rate limiting functionality to be configured:

c := ratelimit.NewHTTPClient(rate)

The HTTPClient embeds an http.Client.

Examples are provided:

Proxy package

The proxy sub-package provides two HTTP rate limited reverse proxy types:

  • rateLimitedSingleRP: is a single host reverse proxy. It embeds an httputil.ReverseProxy.

  • rateLimitedMultipleRP: proxies requests to multiple hosts based on the request path. It embeds an http.ServeMux which handlers are httputil.ReverseProxy.

Both types needs to be initialized using the provied constructor. It enables the rate limiting functionality to be configured:

singleProxy := proxy.NewRateLimitedSingleRP(rate, urlToProxy)
multipleProxy := proxy.NewRateLimitedMultipleRP(rate, urlsToProxy...)

Rate limited is enforced at the struct level. Therefore, for the multipleRP, a global rate limit is enforced whatever backends host is targeted by the request.

Proxy configuration can be achieved by configuring the embedded structs:

  • singleRP: httputil.ReverseProxy exposed as Server
singleProxy.Server.Transport = backend.Client().Transport
  • multipleRP: http.ServeMux exposed as Router
multipleProxy.Router.Handle(customPattern, customHandler)

Examples are provided:

Contributions

Contributions are welcomed 👌

Releases

No releases published

Packages

No packages published

Languages