@@ -95,13 +95,21 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
9595 {
9696 $ tokenName = $ this ->prefix . $ key ;
9797
98+ // Number of tokens to add back per second
99+ $ rate = $ capacity / $ seconds ;
100+ // Number of seconds to get one token
101+ $ refresh = 1 / $ rate ;
102+
98103 // Check to see if the bucket has even been created yet.
99104 if (($ tokens = $ this ->cache ->get ($ tokenName )) === null ) {
100105 // If it hasn't been created, then we'll set it to the maximum
101106 // capacity - 1, and save it to the cache.
102- $ this ->cache ->save ($ tokenName , $ capacity - $ cost , $ seconds );
107+ $ tokens = $ capacity - $ cost ;
108+ $ this ->cache ->save ($ tokenName , $ tokens , $ seconds );
103109 $ this ->cache ->save ($ tokenName . 'Time ' , $ this ->time (), $ seconds );
104110
111+ $ this ->tokenTime = max (1 , (int ) $ refresh );
112+
105113 return true ;
106114 }
107115
@@ -110,15 +118,6 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
110118 $ throttleTime = $ this ->cache ->get ($ tokenName . 'Time ' );
111119 $ elapsed = $ this ->time () - $ throttleTime ;
112120
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-
122121 // Add tokens based up on number per second that
123122 // should be refilled, then checked against capacity
124123 // to be sure the bucket didn't overflow.
@@ -128,12 +127,21 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
128127 // If $tokens >= 1, then we are safe to perform the action, but
129128 // we need to decrement the number of available tokens.
130129 if ($ tokens >= 1 ) {
131- $ this ->cache ->save ($ tokenName , $ tokens - $ cost , $ seconds );
130+ $ tokens = $ tokens - $ cost ;
131+ $ this ->cache ->save ($ tokenName , $ tokens , $ seconds );
132132 $ this ->cache ->save ($ tokenName . 'Time ' , $ this ->time (), $ seconds );
133133
134+ $ this ->tokenTime = max (1 , (int ) ($ refresh - $ elapsed ));
135+
134136 return true ;
135137 }
136138
139+ // How many seconds till a new token is available.
140+ // We must have a minimum wait of 1 second for a new token.
141+ // Primarily stored to allow devs to report back to users.
142+ $ newTokenAvailable = (int ) ($ refresh - $ elapsed - $ refresh * $ tokens );
143+ $ this ->tokenTime = max (1 , $ newTokenAvailable );
144+
137145 return false ;
138146 }
139147
0 commit comments