limiter

package module
v0.0.0-...-003ee67 Latest Latest
Warning

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

Go to latest
Published: May 3, 2018 License: MIT Imports: 4 Imported by: 2 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

rate-limiter

Simple rate-limiter middleware written in go. I made a break from golang for 8 months - so this is a comeback :)

Requirements

Buiding

  • Clone this repository
  • dep ensure
  • cd cmd
  • go build -o limiter

There is a Makefile available with make all option but notice I am tempering with GOPATH (sandboxing building) if you did not go through hell with GOPATH/GOROOT please do not use it - just build it manually. If you select this approach the executable will be created in ./.gopath~/bin/ meaning just run ./.gopath~/bin/limiter binary.

Running

I have implemented two strategies for rate limiting one in-memory and one in redis. The advantages of redis should be obvious (horizontal scalling) Please check the ./cfg/limiter.yml file to set the configuration for your run.

To run in-memory example:

  • source go run cmd/* rlmem --config ./cfg/limiter.yml
  • binary ./.gopath~/bin/limiter rlmem --config ./cfg/limiter.yml
➜ curl localhost:8080/greet/me -v
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /greet/me HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< X-Ratelimit-Limit: 5
< X-Ratelimit-Remaining: 5
< X-Ratelimit-Reset: 2018-05-02 20:03:47.693777422 +0200 CEST
< Date: Wed, 02 May 2018 18:03:41 GMT
< Content-Length: 93
<
* Connection #0 to host localhost left intact
{"name":"limiter","description":"Simple rate limiter demonstration.","uptime":"2.997741413s"}%

To run redis example:

  • source go run cmd/* --config ./cfg/limiter.yml
  • binary ./.gopath~/bin/limiter --config ./cfg/limiter.yml
➜ curl localhost:8080/greet/me/redis -v
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /greet/me/redis HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< X-Ratelimit-Limit: 5
< X-Ratelimit-Remaining: 5
< X-Ratelimit-Reset: 2018-05-02 20:06:13.686774342 +0200 CEST
< Date: Wed, 02 May 2018 18:06:07 GMT
< Content-Length: 94
<
* Connection #0 to host localhost left intact
{"name":"limiter","description":"Simple rate limiter demonstration.","uptime":"18.883323352s"}%

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Name of the project
	Name = "limiter"
	//Description - Project short description
	Description = "Simple rate limiter demonstration."
	//LongDescription - Verbose project description.
	LongDescription = "" /* 161-byte string literal not displayed */
	// Author - Project author.
	Author = "Senad Ibraimoski <senad.ibraimoski@gmail.com>"
)

Functions

This section is empty.

Types

type Rate

type Rate struct {
	Remaining int       `json:"remaining"`
	ResetTime time.Time `json:"reset_time"`
}

Rate - Contains information about remaiming number of requests and the reset time.

type RateLimiter

type RateLimiter struct {
	Limit    int
	Duration time.Duration
	Cache    caches.RateCache
}

RateLimiter - Container about limit/time rate limit requirements and chache implementation that is used to achive the same.

func NewRateLimiter

func NewRateLimiter(limit int, duration time.Duration, cache caches.RateCache) *RateLimiter

NewRateLimiter - Creates new RateLimiter object

func (*RateLimiter) Get

func (rl *RateLimiter) Get(ip string) (*Rate, error)

Get - RateCache Get implementation - Fetch the ip:Rate pair from

func (*RateLimiter) Set

func (rl *RateLimiter) Set(ip string, rate Rate) error

Set - RateCache Set implementation - Puts the ip:Rate pair in cache.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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