davedevelopment/stiphle

First/second call to throttle not really trottled?

Opened this issue · 3 comments

Hi,

I've got a question about the TimeWindow throttler:

With the following piece of code:

$throttler = new TimeWindow();
while(true) {
            $throttler->throttle('key', 1, 1000);
            echo microtime(true) . "\n";
}

The following outcome appears

1431528461.287
1431528461.5689
1431528462.5681
1431528463.5691

The first and second call are less than a second apart, but I would expect that every call is (at least) a second apart from the next/previous..

Can you explain why this happens?

It's been a while, but I think the idea is that only one call can happen in each time "window", so it's fine to have a call at the very end of one window, followed by one at the beginning of the next window. This is more clear on my system:

1431546054.5258
1431546055.001
1431546056.0001
1431546057.0003
1431546058.0005
1431546059.0006
1431546060.0008
1431546061.001

What OS are you using? If it's windows, it's probably something to do with usleep.

Yep! I'm using Windows.. When I'm following your explanation, shouldn't the 2nd call be immediately after the first? (Because the window ends directly after the first call)

I think the windows are like you imagine them to be, rather than when the process starts. So if you set a 1 second window, the windows will be:

0.0 - 1.0
1.0 - 2.0
2.0 - 3.0

and so on.

So if you set a time window of 1 day, the quota should reset at midnight, if that makes sense.