| Copyright | (c) 2015-2018 Christopher Reichert 2017-2018 Daniel Fithian |
|---|---|
| License | BSD3 |
| Maintainer | Christopher Reichert <creichert07@gmail.com>, Daniel Fithian <daniel.m.fithian@gmail.com> |
| Stability | experimental |
| Portability | POSIX |
| Safe Haskell | None |
| Language | Haskell2010 |
Network.Wai.Middleware.Throttle
Description
Uses a Token Bucket algorithm (from the token-bucket package) to throttle WAI Requests.
Example
main = do
let expirationSpec = TimeSpec 5 0 -- five seconds
th <- initThrottler (defaultThrottleSettings expirationSpec) (const $ Right 1)
let appl = throttle th $ _ f -> f $
responseLBS ok200 [] "ok"
Warp.run 3000 app
Synopsis
- data ThrottleSettings = ThrottleSettings {}
- data Throttle a = Throttle {}
- newtype Address = Address SockAddr
- extractAddress :: Request -> Either Response Address
- defaultThrottleSettings :: TimeSpec -> ThrottleSettings
- initThrottler :: ThrottleSettings -> IO (Throttle Address)
- initCustomThrottler :: ThrottleSettings -> (Request -> Either Response a) -> IO (Throttle a)
- throttle :: (Eq a, Hashable a) => Throttle a -> Application -> Application
Documentation
data ThrottleSettings Source #
Throttle settings for controlling token bucket algorithm and cache expiration.
Constructors
| ThrottleSettings | |
Fields
| |
A throttle for a hashable key type. Initialize using initThrottler with defaultThrottleSettings.
Constructors
| Throttle | |
Fields
| |
defaultThrottleSettings :: TimeSpec -> ThrottleSettings Source #
Default throttle settings.
initThrottler :: ThrottleSettings -> IO (Throttle Address) Source #
initCustomThrottler :: ThrottleSettings -> (Request -> Either Response a) -> IO (Throttle a) Source #
Initialize a throttle using settings and a way to extract the key from the request.
throttle :: (Eq a, Hashable a) => Throttle a -> Application -> Application Source #
Run the throttling middleware given a throttle that has been initialized.