Copyright | (c) 2025 Oleksandr Zhabenko |
---|---|
License | MIT |
Maintainer | oleksandr.zhabenko@yahoo.com |
Stability | stable |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Keter.RateLimiter.CacheWithZone
Description
This module provides utility functions for interacting with Cache
instances
in a zone-aware manner. Each key is built from an algorithm-specific prefix,
an IP zone label, and a user-specific identifier. This allows different
logical groups (IP zones) to maintain independent rate limiting state even if
they share the same user identifiers.
These helpers wrap common operations — incrementing counters, reading or writing values, or deleting entries — by automatically composing the correct cache keys.
A convenience function, allowFixedWindowRequest
, is provided to evaluate
requests using the Fixed Window rate-limiting strategy.
Synopsis
- incStoreWithZone :: (CacheStore store v IO, FromJSON v, ToJSON v, Num v, Ord v) => Cache store -> Text -> Text -> Text -> Int -> IO v
- readCacheWithZone :: CacheStore store v IO => Cache store -> Text -> Text -> Text -> IO (Maybe v)
- writeCacheWithZone :: CacheStore store v IO => Cache store -> Text -> Text -> Text -> v -> Int -> IO ()
- deleteCacheWithZone :: CacheStore store v IO => Cache store -> Text -> Text -> Text -> IO ()
- allowFixedWindowRequest :: Cache (InMemoryStore 'FixedWindow) -> Text -> Text -> Text -> Int -> Int -> IO Bool
General Cache Helpers
Arguments
:: (CacheStore store v IO, FromJSON v, ToJSON v, Num v, Ord v) | |
=> Cache store | Cache with algorithm-specific store |
-> Text | Throttle name |
-> Text | IP zone |
-> Text | User key |
-> Int | TTL (seconds) |
-> IO v | New counter value |
Increment a cache entry using a composed key derived from the zone and user key.
This is typically used in fixed-window rate limiters to track the number of requests within a time period. The function creates a composite cache key from the throttle name, algorithm type, IP zone, and user key, then increments the stored counter value.
The stored type must be JSON-serializable and support numeric operations.
Examples
newCount <- incStoreWithZone cache "api-throttle" "zone-a" "user123" 3600 print newCount -- Prints the incremented counter value
Arguments
:: CacheStore store v IO | |
=> Cache store | Cache handle |
-> Text | Throttle name |
-> Text | IP zone |
-> Text | User key |
-> IO (Maybe v) | Optional stored value |
Read a cache entry using a composed key.
Useful for debugging or non-mutating cache inspection. The function constructs the appropriate cache key from the provided components and retrieves the stored value if it exists.
Examples
maybeValue <- readCacheWithZone cache "throttle1" "zone1" "user456" case maybeValue of Just val -> print val Nothing -> putStrLn "No entry found"
Arguments
:: CacheStore store v IO | |
=> Cache store | Cache handle |
-> Text | Throttle name |
-> Text | IP zone |
-> Text | User key |
-> v | Value to store |
-> Int | Expiration in seconds |
-> IO () |
Write a value into the cache using a zone-aware key.
Typically used for manual updates, testing, or initializing state. The function creates a composite key and stores the provided value with the specified expiration time.
Examples
writeCacheWithZone cache "throttle1" "zone1" "user789" (42 :: Int) 1800 putStrLn "Value written to cache"
Arguments
:: CacheStore store v IO | |
=> Cache store | Cache handle |
-> Text | Throttle name |
-> Text | IP zone |
-> Text | User key |
-> IO () |
Delete an entry from the cache using a zone-aware key.
Can be used to forcibly reset rate limiter state or clean up expired entries manually. The function constructs the appropriate cache key and removes the corresponding entry.
Examples
deleteCacheWithZone cache "throttle1" "zone1" "user123" putStrLn "Cache entry deleted"
Fixed Window Rate Limiting
allowFixedWindowRequest Source #
Arguments
:: Cache (InMemoryStore 'FixedWindow) | FixedWindow-based cache handle |
-> Text | Throttle name |
-> Text | IP zone or tenant key |
-> Text | User key |
-> Int | Request limit |
-> Int | Period in seconds |
-> IO Bool | Whether the request is allowed |
Attempt to allow a request under a Fixed Window rate-limiting strategy.
This increments the counter for the composed key built from the given zone and user key. The request is allowed if the resulting count does not exceed the limit for the given period.
The function takes a FixedWindow cache, throttle name, IP zone, user key,
request limit, and time period. It returns True
if the request should
be allowed, False
otherwise.
Examples
allowed <- allowFixedWindowRequest myCache "throttle1" "zone1" "userA" 10 60 if allowed then putStrLn "Request allowed" else putStrLn "Request denied - rate limit exceeded"