diff options
author | Christopher Speller <crspeller@gmail.com> | 2016-05-12 15:08:58 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2016-05-12 16:37:29 -0400 |
commit | 84d2482ddbff9564c9ad75b2d30af66e3ddfd44d (patch) | |
tree | 8bfa567d2b6381f4a996ada2deff8a16aa85a3ac /Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/throttler.go | |
parent | d1efb66ad7b017f0fbfe6f0c20843b30f396e504 (diff) | |
download | chat-84d2482ddbff9564c9ad75b2d30af66e3ddfd44d.tar.gz chat-84d2482ddbff9564c9ad75b2d30af66e3ddfd44d.tar.bz2 chat-84d2482ddbff9564c9ad75b2d30af66e3ddfd44d.zip |
Updating go depencancies. Switching to go1.6 vendoring (#2949)
Diffstat (limited to 'Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/throttler.go')
-rw-r--r-- | Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/throttler.go | 86 |
1 files changed, 0 insertions, 86 deletions
diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/throttler.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/throttler.go deleted file mode 100644 index 06da13051..000000000 --- a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/throttler.go +++ /dev/null @@ -1,86 +0,0 @@ -package throttled - -import ( - "net/http" - "sync" -) - -var ( - // DefaultDeniedHandler handles the requests that were denied access because - // of a throttler. By default, returns a 429 status code with a - // generic message. - DefaultDeniedHandler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - http.Error(w, "limit exceeded", 429) - })) - - // Error is the function to call when an error occurs on a throttled handler. - // By default, returns a 500 status code with a generic message. - Error = ErrorFunc(func(w http.ResponseWriter, r *http.Request, err error) { - http.Error(w, "internal error", http.StatusInternalServerError) - }) -) - -// ErrorFunc defines the function type for the Error variable. -type ErrorFunc func(w http.ResponseWriter, r *http.Request, err error) - -// The Limiter interface defines the methods required to control access to a -// throttled handler. -type Limiter interface { - Start() - Limit(http.ResponseWriter, *http.Request) (<-chan bool, error) -} - -// Custom creates a Throttler using the provided Limiter implementation. -func Custom(l Limiter) *Throttler { - return &Throttler{ - limiter: l, - } -} - -// A Throttler controls access to HTTP handlers using a Limiter. -type Throttler struct { - // DeniedHandler is called if the request is disallowed. If it is nil, - // the DefaultDeniedHandler variable is used. - DeniedHandler http.Handler - - limiter Limiter - // The mutex protects the started flag - mu sync.Mutex - started bool -} - -// Throttle wraps a HTTP handler so that its access is controlled by -// the Throttler. It returns the Handler with the throttling logic. -func (t *Throttler) Throttle(h http.Handler) http.Handler { - dh := t.start() - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ch, err := t.limiter.Limit(w, r) - if err != nil { - Error(w, r, err) - return - } - ok := <-ch - if ok { - h.ServeHTTP(w, r) - } else { - dh.ServeHTTP(w, r) - } - }) -} - -// start starts the throttling and returns the effective denied handler to -// use for requests that were denied access. -func (t *Throttler) start() http.Handler { - t.mu.Lock() - defer t.mu.Unlock() - // Get the effective denied handler - dh := t.DeniedHandler - if dh == nil { - dh = DefaultDeniedHandler - } - if !t.started { - t.limiter.Start() - t.started = true - } - return dh -} |