bandwidth-throttle / token-bucket

Implementation of the Token Bucket algorithm in PHP.
Do What The F*ck You Want To Public License
504 stars 79 forks source link

"Could not write to storage" for FileStorage #21

Open maximzasorin opened 6 years ago

maximzasorin commented 6 years ago

When I use next code for bootstrap token bucket:

use bandwidthThrottle\tokenBucket\Rate;
use bandwidthThrottle\tokenBucket\TokenBucket;
use bandwidthThrottle\tokenBucket\storage\FileStorage;

$storage = new FileStorage(__DIR__ . "/api.bucket");
$rate = new Rate(10, Rate::SECOND);
$bucket = new TokenBucket(10, $rate, $storage);
$bucket->bootstrap(10);

On Linux and macOS all works well, but in Windows I get an error:

Could not write to storage.

Maybe it's because the file opens twice, first, in FileStorage::__construct, then file locked by flock and then file opens in FileStorage::bootstrap method.

tepeumut commented 5 years ago

Did you remove $bucket->bootstrap(10); ?

emmanuelmahove commented 2 years ago

Had the same problem on Windows but it went away when I commented out the second file open in the bootstrap method. https://github.com/bandwidth-throttle/token-bucket/blob/5038add13124fa72f7c606ed11524dfde019658f/classes/storage/FileStorage.php#L84

simillo01 commented 2 years ago

For some reason with PHP 8.1.6 and that commit on my windows machine is not working, if i comment that line it works. Someone know why?