Safe Haskell | None |
---|---|
Language | Haskell2010 |
Wai middleware for request throttling.
Basic idea: on every (matching) request a counter is incremented. If it exceeds given limit, request is blocked and error response is sent to client. Request counter resets after defined period of time.
The throttle
function limits request to the underlying
application. If you wish to limit only parts of your requests
you need to do the routing yourself. For convenience,
throttlePath
function is provided which applies throttling
only for requests with matching URL path.
- class ThrottleCache cache where
- cacheCount :: cache -> ByteString -> IO Int
- newMemoryThrottleCache :: Int -> NominalDiffTime -> IO MemoryThrottleCache
- throttle :: ThrottleCache cache => cache -> Int -> (Request -> Maybe ByteString) -> Middleware
- throttlePath :: ThrottleCache cache => ByteString -> cache -> Int -> (Request -> Maybe ByteString) -> Middleware
Documentation
class ThrottleCache cache where Source
Cache type class. Throttle cache is used to store request counts. Can store multiple counts via different keys. E.g. keys can be client IP addresses or user logins.
:: cache | cache |
-> ByteString | key |
-> IO Int |
Increment count for given key and return new count value. Cache should automatically reset counts to zero after a defined period.
:: Int | limit |
-> NominalDiffTime | limit renew period |
-> IO MemoryThrottleCache |
Create in-memory throttle cache.
Normally throttle cache does not need to know what the limit is. But this one uses some trickery to prevent unnecessary calls to slow getCurrentTime function.
:: ThrottleCache cache | |
=> cache | cache to store request counts |
-> Int | request limit |
-> (Request -> Maybe ByteString) | function to get cache key based on request. If Nothing is returned, request is not throttled |
-> Middleware |
Wai middleware that cuts requests if request rate is higher than defined level. Responds with 429 if limit exceeded
:: ThrottleCache cache | |
=> ByteString | URL path to match |
-> cache | cache to store request counts |
-> Int | request limit |
-> (Request -> Maybe ByteString) | function to get cache key based on request. If Nothing is returned, request is not throttled |
-> Middleware |
Apply throttling to requests with matching URL path