Skip to content

classes_base_ratelimit.class

Daniel Spors edited this page Dec 19, 2023 · 2 revisions

Classes in file base/ratelimit.class.php

class RateLimit

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();	

Define

Entry point for method chaining a new rate limit.

Definition: public static function Define($name)

Returns: static

Parameters:

  • string $name Name of the limit.

Delete

INTERNAL RateLimits are never removed like this

GetTableName

IMPLEMENTS Model::GetTableName()

PerDays

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

PerHours

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

PerMinutes

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

PerMonths

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

PerSeconds

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

PerYears

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

Reserve

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

Save

INTERNAL RateLimits are never saved like this

Clone this wiki locally