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

Missed requests over a certain period of time #11

Closed anbotev closed 6 years ago

anbotev commented 7 years ago

Over a certain period of time, my curl requests are more than my limit. Any ideas ?

malkusch commented 6 years ago

Could you please give more details?

anbotev commented 6 years ago

I use this library to limit my outgoing curl requests. When I start a performance test to my endpoint in log files I see that in beginning requests are over my limit but in 3-5 seconds everything begins to normalize.

malkusch commented 6 years ago

I pretty much sound like you bootstrap the token bucket with tokens. Those tokens can be used instantly in a burst. Bootstrap the bucket with 0.

anbotev commented 6 years ago

It's not working again :( . I'm posting my config: $storage = new FileStorage(DIR. "/api.bucket"); $rate = new Rate(15, Rate::SECOND); $bucket = new TokenBucket(15, $rate, $storage); $consumer = new BlockingConsumer($bucket); $bucket->bootstrap(0); $consumer->consume(1);

malkusch commented 6 years ago

Then my next guess is that you don't want a bucket with a capacity of 15. Look, you gave your bucket a capacity of 15. Whenever there are no tokens consumed, tokens are cumulated in the bucket. Then you can burst instantly those cumulated tokens.

If this doesn't explain your observed behaviour, I'd ask you to explain exactly what behaviour you observe, and what you would expect.

anbotev commented 6 years ago

Let's explain it again. If I set bucket capacity of 3 and I don't want to have more then 3 requests per second. When I run tests with JMeter or other tool first second I have 5 requests which are success and two of them are over limit. Next second everything is alright. That's my problem.

[2017-10-18 14:39:54.345972] logger.INFO: [CONNECTOR] REQUEST [2017-10-18 14:39:54.354407] logger.INFO: [CONNECTOR] REQUEST [2017-10-18 14:39:54.381782] logger.INFO: [CONNECTOR] REQUEST [2017-10-18 14:39:54.385026] logger.INFO: [CONNECTOR] REQUEST [2017-10-18 14:39:54.385703] logger.INFO: [CONNECTOR] REQUEST [2017-10-18 14:39:54.397703] logger.INFO: [CONNECTOR] REQUEST

[2017-10-18 14:39:55.345972] logger.INFO: [CONNECTOR] REQUEST [2017-10-18 14:39:55.354407] logger.INFO: [CONNECTOR] REQUEST [2017-10-18 14:39:55.381782] logger.INFO: [CONNECTOR] REQUEST

[2017-10-18 14:39:56.345972] logger.INFO: [CONNECTOR] REQUEST [2017-10-18 14:39:56.354407] logger.INFO: [CONNECTOR] REQUEST [2017-10-18 14:39:56.381782] logger.INFO: [CONNECTOR] REQUEST ....

malkusch commented 6 years ago

Does it work for you when you use bucket with a capacity of 1?

anbotev commented 6 years ago

Yes, it does. When I set capacity to 1 it works.