wai-throttler-0.1.0.5: Wai middleware for request throttling

Safe HaskellNone
LanguageHaskell2010

Network.Wai.Middleware.Throttler

Description

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.

Synopsis

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.

Methods

cacheCount Source

Arguments

:: 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.

newMemoryThrottleCache Source

Arguments

:: 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.

throttle Source

Arguments

:: 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

throttlePath Source

Arguments

:: 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