Rate Limiting Rules
Rate limiting rules define how many requests are allowed within a given time frame. These rules are crucial for controlling access to your resources and preventing abuse or overuse of your services.
Creating Rate Limit Rules
You can create rate limit rules using the RateLimitRule
class. This class provides a fluent interface for defining rules, making them easy to read and understand.
Available Methods
The RateLimitRule
class provides several methods to define your rate limiting rules:
Static Factory Method
allow(int $limit)
: Starts the rule definition with the number of allowed requests.
Time Interval Methods
every(int $amount, Interval $unit)
: Defines a custom time interval.perSecond()
: Sets the interval to one second.perSeconds(int $seconds)
: Sets the interval to a specified number of seconds.perMinute()
: Sets the interval to one minute.perMinutes(int $minutes)
: Sets the interval to a specified number of minutes.perHour()
: Sets the interval to one hour.perHours(int $hours)
: Sets the interval to a specified number of hours.perDay()
: Sets the interval to one day.perDays(int $days)
: Sets the interval to a specified number of days.perWeek()
: Sets the interval to one week.perWeeks(int $weeks)
: Sets the interval to a specified number of weeks.perMonth()
: Sets the interval to one month.perMonths(int $months)
: Sets the interval to a specified number of months.
Expiration Methods
dailyUntil(string $time)
: Sets the rule to expire daily at a specific time (format: 'H:i').untilMidnightTonight()
: Sets the rule to expire at midnight.untilEndOfMonth()
: Sets the rule to expire at the end of the current month.
Getter Methods
getLimit()
: Returns the number of allowed requests.getInterval()
: Returns the interval enum value.getDuration()
: Returns the duration of the interval.getUntil()
: Returns the expiration time, if set.getTotalSeconds()
: Returns the total number of seconds for the interval.getKey(string $prefix = '', string $suffix = '')
: Generates a unique key for the rule.
Examples
Here are some more complex examples of how you can use these rules:
Using Rate Limit Rules
Once you've defined your rules, you can add them to your Guardian instance:
These rules will now be applied to all requests handled by this Guardian instance.
Best Practices
Combine rules: Use multiple rules to create more complex rate limiting strategies. For example, you might allow a high number of requests per day, but still limit the per-minute rate to prevent sudden spikes.
Use appropriate intervals: Choose intervals that make sense for your application. For an API, per-second or per-minute limits might be appropriate, while for a bulk operation, per-hour or per-day limits might make more sense.
Consider user tiers: If your application has different user tiers, you might create different sets of rules for each tier.
Monitor and adjust: Regularly review your rate limits and adjust them based on your application's performance and user behavior.
By effectively using rate limiting rules, you can protect your resources, ensure fair usage, and maintain the performance and reliability of your application.
Last updated