| Copyright | (c) 2015-2017 Christopher Reichert |
|---|---|
| License | BSD3 |
| Maintainer | Christopher Reichert <creichert07@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
st <- initThrottler
let payload = "{ "api": "return data" }"
app = throttle defaultThrottleSettings st
$ _ f -> f (responseLBS status200 [] payload)
Warp.run 3000 app
- throttle :: RequestHashable a => ThrottleSettings -> CustomWaiThrottle a -> Application -> Application
- type WaiThrottle = CustomWaiThrottle Address
- data CustomWaiThrottle a
- initThrottler :: IO WaiThrottle
- initCustomThrottler :: IO (CustomWaiThrottle a)
- data ThrottleSettings = ThrottleSettings {
- isThrottled :: !(Request -> IO Bool)
- onThrottled :: !(Word64 -> Response)
- onRequestError :: !(Text -> Response)
- throttleRate :: !Integer
- throttlePeriod :: !Integer
- throttleBurst :: !Integer
- defaultThrottleSettings :: ThrottleSettings
- newtype Address = Address SockAddr
- class (Eq a, Ord a, Hashable a) => RequestHashable a where
Documentation
Wai Request Throttling Middleware
throttle :: RequestHashable a => ThrottleSettings -> CustomWaiThrottle a -> Application -> Application #
WAI Request Throttling Middleware.
Uses a Requests remoteHost function to resolve the
remote IP address.
Wai Throttle middleware state.
Essentially, a TVar with a HashMap for indexing remote IP address
type WaiThrottle = CustomWaiThrottle Address #
A synonym for an IP address throttle
data CustomWaiThrottle a #
A throttle for a type implementing RequestHashable
initThrottler :: IO WaiThrottle #
Initialize an IP address throttler
initCustomThrottler :: IO (CustomWaiThrottle a) #
Initialize a "custom" throttler that implements the RequestHashable class
Throttle settings and configuration
data ThrottleSettings #
Settings which control various behaviors in the middleware.
Constructors
| ThrottleSettings | |
Fields
| |
defaultThrottleSettings :: ThrottleSettings #
Default settings to throttle requests.