项目作者: martini-contrib

项目描述 :
Throttling Middleware for Martini
高级语言: Go
项目地址: git://github.com/martini-contrib/throttle.git
创建时间: 2014-04-22T20:50:27Z
项目社区:https://github.com/martini-contrib/throttle

开源协议:MIT License

下载


throttle wercker status

Simple throttling for martini, negroni or Macaron.

API Reference

Description

Package throttle provides quota-based throttling.

Policy

throttle.Policy is a middleware that allows you to throttle by the given quota.

Quota

throttle.Quota is a quota type with a limit and a duration

Usage

This package provides a way to install rate limit or interval policies for throttling:

  1. m := martini.Classic()
  2. // A Rate Limit Policy
  3. m.Use(throttle.Policy(&throttle.Quota{
  4. Limit: 1000,
  5. Within: time.Hour,
  6. }))
  7. // An Interval Policy
  8. m.Use(throttle.Policy(&throttle.Quota{
  9. Limit: 1,
  10. Within: time.Second,
  11. }))
  12. m.Any("/test", func() int {
  13. return http.StatusOK
  14. })
  15. // A Policy local to a given route
  16. adminPolicy := Policy(&throttle.Quota{
  17. Limit: 100,
  18. Within: time.Hour,
  19. })
  20. m.Get("/admin", adminPolicy, func() int {
  21. return http.StatusOK
  22. })
  23. ...

Options

You can configure the options for throttling by passing in throttle.Options as the second argument to throttle.Policy. Use it to configure the following options (defaults are used here):

  1. &throttle.Options{
  2. // The Status Code returned when the client exceeds the quota. Defaults to 429 Too Many Requests
  3. StatusCode int
  4. // The response body returned when the client exceeds the quota
  5. Message string
  6. // A function to identify a request, must satisfy the interface func(*http.Request)string
  7. // Defaults to a function identifying the request by IP or X-Forwarded-For Header if provided
  8. // So if you want to identify by an API key given in request headers or something else, configure this option
  9. IdentificationFunction func(*http.Request) string
  10. // The key prefix to use in any key value store
  11. KeyPrefix string
  12. // The store to use. The key value store has to satisfy the throttle.KeyValueStorer interface
  13. // For further explanation, see below
  14. Store KeyValueStorer
  15. // If the throttle is disabled or not
  16. // defaults to false
  17. Disabled bool
  18. }

State Storage

Throttling relies on storage of one key per Policy and user in a (KeyValue) Storage. The interface the store has to satisfy is throttle.KeyValueStorer, or, more explicit:

  1. type KeyValueStorer interface {
  2. Get(key string) ([]byte, error)
  3. Set(key string, value []byte) error
  4. }

This allows for drop in replacement of the store with most common go libraries for key value stores like redis.go

  1. var client redis.Client
  2. m.Use(throttle.Policy(&throttle.Quota{
  3. Limit: 10,
  4. Within: time.Minute,
  5. }, &throttle.Options{
  6. Store: &client,
  7. }))

Adapters are also very easy to write. throttle prefixes every key, your adapter does not have to care about it, and the stored value is stringified JSON.

The default state storage is in memory via a concurrent-safe map[string][]byte cleaning up every 15 minutes. While this works fine for clients running one instance of a martini server, for all other uses you should obviously opt for a proper key value store.

Headers & Status Codes

throttle adds the following X-RateLimit-*-Headers to every response it controls:

  • X-RateLimit-Limit: The maximum number of requests that the consumer is permitted to make within the given time window
  • X-RateLimit-Remaining: The number of requests remaining in the current rate limit window
  • X-RateLimit-Reset: The time at which the current rate limit window resets in UTC epoch seconds

No Retry-After Header is added to the response, since the X-RateLimit-Reset makes it redundant. Also it is not recommended to use a 503 Service Unavailable Status Code when Limiting the rate of requests, since the 5xx Status Code Family indicates an error on the servers side.

Authors