-
Notifications
You must be signed in to change notification settings - Fork 34
classes_base_ratelimit.class
Tool class to help stop flooding systems. You may define a RateLimit on the fly directly where it is needed:
if( !RateLimit::Define('mycontroller1')->PerSeconds(10,100)->Reserve(5) )
WdfException::Raise("429 Too Many Requests");
Or you may inhertit your own class with some predefined limits:
class MyLimits extends RateLimit
{
static function Dashboard()
{
return RateLimit::Define(__METHOD__)->PerSeconds(5,5)->PerDays(1,1000)->Reserve(3);
}
static function Login()
{
if( !RateLimit::Define(__METHOD__)->PerSeconds(5,5)->PerDays(1,1000)->Reserve(3) )
WdfException::Raise("429 Too Many Requests");
}
}
// and later
if( !MyLimits::Dashboard() )
WdfException::Raise("429 Too Many Requests");
// or even
MyLimits::Login();
Entry point for method chaining a new rate limit.
Definition: public static function Define($name)
Returns: static
Parameters:
-
string $name
Name of the limit.
INTERNAL RateLimits are never removed like this
IMPLEMENTS Model::GetTableName()
Limit to $limit calls per $days days
Definition: public function PerDays($days, $limit)
Returns: static
Parameters:
-
int $days
Days -
int $limit
Max calls per interval
Limit to $limit calls per $hours hours
Definition: public function PerHours($hours, $limit)
Returns: static
Parameters:
-
int $hours
Hours -
int $limit
Max calls per interval
Limit to $limit calls per $minute minutes
Definition: public function PerMinutes($minutes, $limit)
Returns: static
Parameters:
-
int $minutes
Minutes -
int $limit
Max calls per interval
Limit to $limit calls per $months months
Definition: public function PerMonths($months, $limit)
Returns: static
Parameters:
-
int $months
Months -
int $limit
Max calls per interval
Limit to $limit calls per $seconds seconds
Definition: public function PerSeconds($seconds, $limit)
Returns: static
Parameters:
-
int $seconds
Seconds -
int $limit
Max calls per interval
Limit to $limit calls per $years years
Definition: public function PerYears($years, $limit)
Returns: static
Parameters:
-
int $years
Years -
int $limit
Max calls per interval
End point for method chaining. Tries to reseve a 'slot' for this limit if possible for $timeout_seconds seconds.
Definition: public function Reserve($timeout_seconds, $verbose=true)
Returns: bool
True if reserved successfully, else false
Parameters:
-
int $timeout_seconds
Maximum secods to try for -
bool $verbose
If true will log_debug if Reserve fails
INTERNAL RateLimits are never saved like this