延迟 bandwidth-throttle/token-bucket (PHP)

Delay in bandwidth-throttle/token-bucket (PHP)

我使用 https://github.com/bandwidth-throttle/token-bucket 查询仅限于外部服务器。 我的代码:

function main() {
    unlink(__DIR__ . "/api.bucket2");
    $storage  = new FileStorage(__DIR__ . "/api.bucket2");
    $rate     = new Rate(3, Rate::SECOND);
    $bucket   = new TokenBucket(3, $rate, $storage);
    $bucket->bootstrap(3);
    $consumer = new BlockingConsumer($bucket);

    for ($i = 0; $i < 12; $i++) {
        $consumer->consume(1);

        work();
    }
}

function work() {
    echo date("d.m.Y H:i:s") . substr((string)microtime(), 1, 4) . "\n";
}

main();

结果:

-bash-4.2$ php -f worker-test.php
03.05.2016 14:26:16.785
03.05.2016 14:26:16.785
03.05.2016 14:26:16.786
03.05.2016 14:26:17.118
03.05.2016 14:26:17.451
03.05.2016 14:26:17.784
...

我预计该函数每秒将被调用 3 次,但事实并非如此。前 6 个呼叫在 1 秒内完成。如果我在“$bucket->bootstrap(0);”上更改“$bucket->bootstrap(3);”,会更好:

03.05.2016 14:33:34.913
03.05.2016 14:33:35.245
03.05.2016 14:33:35.578
03.05.2016 14:33:35.911
...

但还是每秒3个多。我做错了什么?

$bucket->bootstrap(3);

TokenBucket::bootstrap(3) 将三个初始令牌放入桶中。这些初始代币可以立即使用。您实际上不会限制那些第一次调用的速率。

如果您不想要最初的爆发,那么您的做法是正确的 bootstrap,没有任何令牌。

03.05.2016 14:33:34.913
03.05.2016 14:33:35.245
03.05.2016 14:33:35.578
03.05.2016 14:33:35.911

but it is still more than 3 per second.

我每秒数3次。请容忍这个观察到的±1ms 的变化。在长 运行 中,平均每秒 3 个。

这个±1ms可能来自BlockingConsumerthis implementation detail:

// sleep at least 1 millisecond.
usleep(max(1000, $seconds * 1000000));