@@ -79,10 +79,9 @@ public function getTokenTime(): int
7979 *
8080 * Example:
8181 *
82- * if (! $throttler->check($request->ipAddress(), 60, MINUTE))
83- * {
82+ * if (! $throttler->check($request->ipAddress(), 60, MINUTE)) {
8483 * die('You submitted over 60 requests within a minute.');
85- * }
84+ * }
8685 *
8786 * @param string $key The name to use as the "bucket" name.
8887 * @param int $capacity The number of requests the "bucket" can hold
@@ -95,13 +94,21 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
9594 {
9695 $ tokenName = $ this ->prefix . $ key ;
9796
97+ // Number of tokens to add back per second
98+ $ rate = $ capacity / $ seconds ;
99+ // Number of seconds to get one token
100+ $ refresh = 1 / $ rate ;
101+
98102 // Check to see if the bucket has even been created yet.
99103 if (($ tokens = $ this ->cache ->get ($ tokenName )) === null ) {
100104 // If it hasn't been created, then we'll set it to the maximum
101105 // capacity - 1, and save it to the cache.
102- $ this ->cache ->save ($ tokenName , $ capacity - $ cost , $ seconds );
106+ $ tokens = $ capacity - $ cost ;
107+ $ this ->cache ->save ($ tokenName , $ tokens , $ seconds );
103108 $ this ->cache ->save ($ tokenName . 'Time ' , $ this ->time (), $ seconds );
104109
110+ $ this ->tokenTime = 0 ;
111+
105112 return true ;
106113 }
107114
@@ -110,15 +117,6 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
110117 $ throttleTime = $ this ->cache ->get ($ tokenName . 'Time ' );
111118 $ elapsed = $ this ->time () - $ throttleTime ;
112119
113- // Number of tokens to add back per second
114- $ rate = $ capacity / $ seconds ;
115-
116- // How many seconds till a new token is available.
117- // We must have a minimum wait of 1 second for a new token.
118- // Primarily stored to allow devs to report back to users.
119- $ newTokenAvailable = (1 / $ rate ) - $ elapsed ;
120- $ this ->tokenTime = max (1 , $ newTokenAvailable );
121-
122120 // Add tokens based up on number per second that
123121 // should be refilled, then checked against capacity
124122 // to be sure the bucket didn't overflow.
@@ -128,12 +126,21 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
128126 // If $tokens >= 1, then we are safe to perform the action, but
129127 // we need to decrement the number of available tokens.
130128 if ($ tokens >= 1 ) {
131- $ this ->cache ->save ($ tokenName , $ tokens - $ cost , $ seconds );
129+ $ tokens = $ tokens - $ cost ;
130+ $ this ->cache ->save ($ tokenName , $ tokens , $ seconds );
132131 $ this ->cache ->save ($ tokenName . 'Time ' , $ this ->time (), $ seconds );
133132
133+ $ this ->tokenTime = 0 ;
134+
134135 return true ;
135136 }
136137
138+ // How many seconds till a new token is available.
139+ // We must have a minimum wait of 1 second for a new token.
140+ // Primarily stored to allow devs to report back to users.
141+ $ newTokenAvailable = (int ) ($ refresh - $ elapsed - $ refresh * $ tokens );
142+ $ this ->tokenTime = max (1 , $ newTokenAvailable );
143+
137144 return false ;
138145 }
139146
0 commit comments