Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 1.18 KB

README.md

File metadata and controls

42 lines (31 loc) · 1.18 KB

Throttled-Valve GoDoc

Extends throttled/throttled with a new RateLimiter that can decrease the allowed rate of clients sustaining too much traffic for too long.

Installation

go get -u github.com/keratin/throttled-valve

Example

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

// Valve will allow:
// * 10/minute (1/6 seconds) with an additional burst of 4
// * After two minutes: 2/minute (1/30 seconds)
valve := valve.NewValve(store, valve.NewSchedule(4,
  valve.Entry{Rate: throttled.PerMin(10)},
  valve.Entry{Rate: throttled.PerMin(2), Delay: 2 * time.Minute},
))

// valve is compatible with throttled.HTTPRateLimiter
loginLimiter := throttled.HTTPRateLimiter{
  RateLimiter: valve,
  VaryBy:      &throttled.VaryBy{RemoteAddr: true},
}

// you may wish to use a router and only apply the limit on specific endpoints
http.ListenAndServe(":8080", loginLimiter.RateLimit(loginHandler))

License

Copyright (c) 2018 Lance Ivy & contributors

Released under the MIT license.