actix-idempotent
Middleware for handling idempotent requests in actix-web applications.
This crate provides middleware that ensures idempotency of HTTP requests by caching responses in a session store. When an identical request is made within the configured time window, the cached response is returned instead of executing the request again.
Features
- Request deduplication based on method, path, headers, and body
- Configurable response caching duration
- Header filtering options to exclude specific headers from idempotency checks
- Integration with session-based storage (via ruts)
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Example
use ;
use Duration;
use ;
use RedisSessionStore;
use SessionMiddleware;
use ;
use ;
use get;
use App;
use HttpServer;
use ;
use ;
use Result;
static COUNTER: AtomicU64 = new;
async
async
How it Works
- When a request is received, a hash is generated from the request method, path, headers (configurable), and body.
- If an identical request (same hash) is found in the session store and hasn't expired:
- The cached response is returned
- The original handler is not called
- If no cached response is found:
- The request is processed normally
- The response is cached in the session store
- The response is returned to the client
This ensures that retrying the same request (e.g., due to network issues or client retries) won't result in the operation being performed multiple times.
License
This project is licensed under the MIT License.