martini-contrib/throttle — pull request #6 by albrow (2014-09-01 20:54:00+00:00)
Title: Improve compatibility with negroni and other http-based middleware.

Throttle looks like a really handy tool, and I'm interested in adapting it to work with [negroni](https://github.com/codegangsta/negroni). This small change makes it easier to write a negroni-compatible middleware wrapper around the throttle handler.

For example, I can now write negroni-compatible middleware (which uses throttle behind the scenes) in just a few lines:

``` go
func Policy(quota *throttle.Quota, options ...*throttle.Options) negroni.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
handler := throttle.Policy(quota, options...)
handler(res, req)
if res.Header().Get("X-Ratelimit-Remaining") == "0" {
// the policy was triggered and the request should fail
// in this case, we should not call next
} else {
// the policy was not triggered, and the request should continue
// on to the next handler
next(res, req)
}
}
}
```

I expect that this will also make throttle more compatible for those who prefer to roll their own middleware micro-frameworks. The tests still pass, and Policy still returns a handler which is directly compatible with martini, so as far as I know this change comes with no downsides.