π¦ Basic Rate Limiting
Learn how to control actions per key using Toolkitify's RateLimiter with configurable limits, intervals, and storage.
Using Basic Rate Limiting
For example, you can create a Basic Rate Limit to limit a button to 3 clicks every 5 seconds.
Basic Rate LimitCode Sandbox
Creating a Basic RateLimit
import { createRateLimit } from "toolkitify/rate-limit";
// Create a rate limiter for IP addresses: max 5 requests per 10 seconds.
const ratelimitIp = createRateLimit(5, "10s", "ip", {
blockDuration: "10s" // Optional: Default value is the same as interval.
// Here you can optionally pass the rest of the settings found in Rate Limiter.
});
async function handleRequest(ip: string) {
const { success, limit, remaining, reset } = await ratelimitIp.limit(ip);
if (!success) {
console.log(`Rate limit exceeded. Try again after ${new Date(reset).toLocaleTimeString()}`);
return;
}
console.log(`Request allowed. Remaining: ${remaining}/${limit}`);
// Proceed with your request logic here
};
// Example usage
handleRequest("192.168.0.1");
handleRequest("192.168.0.1");
handleRequest("192.168.0.1");