From 7bce546defa2bec75485d4bf1d68b72cf311c489 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 21 Apr 2021 16:39:10 +0200 Subject: [PATCH] Allow for 'Auth#throttle' to be used even when throttling is disabled --- README.md | 2 ++ src/Auth.php | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b86f0ab..8ddd4c3 100644 --- a/README.md +++ b/README.md @@ -872,6 +872,8 @@ Allowing short bursts of activity during peak demand is possible by specifying a In some cases, you may just want to *simulate* the throttling or rate limiting. This lets you check whether an action would be permitted without actually modifying the activity tracker. To do so, simply pass `true` as the fifth argument. +**Note:** When you disable throttling on the instance (using the [`$throttling` parameter](#creating-a-new-instance) passed to the constructor), this turns off both the automatic internal protection and the effect of any calls to `Auth#throttle` in your own application code – unless you also set the optional `$force` parameter to `true` in specific `Auth#throttle` calls. + ### Administration (managing users) The administrative interface is available via `$auth->admin()`. You can call various method on this interface, as documented below. diff --git a/src/Auth.php b/src/Auth.php index cd10953..ed33c9a 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -1728,12 +1728,16 @@ final class Auth extends UserManager { * @param int|null $burstiness (optional) the permitted degree of variation or unevenness during peaks (>= 1) * @param bool|null $simulated (optional) whether to simulate a dry run instead of actually consuming the requested units * @param int|null $cost (optional) the number of units to request (>= 1) + * @param bool|null $force (optional) whether to apply throttling locally (with this call) even when throttling has been disabled globally (on the instance, via the constructor option) * @return float the number of units remaining from the supply * @throws TooManyRequestsException if the actual demand has exceeded the designated supply * @throws AuthError if an internal problem occurred (do *not* catch) */ - public function throttle(array $criteria, $supply, $interval, $burstiness = null, $simulated = null, $cost = null) { - if (!$this->throttling) { + public function throttle(array $criteria, $supply, $interval, $burstiness = null, $simulated = null, $cost = null, $force = null) { + // validate the supplied parameters and set appropriate defaults where necessary + $force = ($force !== null) ? (bool) $force : false; + + if (!$this->throttling && !$force) { return $supply; }