keter-rate-limiting-plugin-0.2.0.0: Simple Keter rate limiting plugin.
Copyright(c) 2025 Oleksandr Zhabenko
LicenseMIT
Maintaineroleksandr.zhabenko@yahoo.com
Stabilitystable
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

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

General Cache Helpers

incStoreWithZone Source #

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

Expand
newCount <- incStoreWithZone cache "api-throttle" "zone-a" "user123" 3600
print newCount  -- Prints the incremented counter value

readCacheWithZone Source #

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

Expand
maybeValue <- readCacheWithZone cache "throttle1" "zone1" "user456"
case maybeValue of
  Just val -> print val
  Nothing  -> putStrLn "No entry found"

writeCacheWithZone Source #

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

Expand
writeCacheWithZone cache "throttle1" "zone1" "user789" (42 :: Int) 1800
putStrLn "Value written to cache"

deleteCacheWithZone Source #

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

Expand
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

Expand
allowed <- allowFixedWindowRequest myCache "throttle1" "zone1" "userA" 10 60
if allowed
  then putStrLn "Request allowed"
  else putStrLn "Request denied - rate limit exceeded"