Skip to content

Improved function waitUntilRequestQuotaAvailable #939

Open
@igor3b

Description

@igor3b

The current function is not precise and is complicated by a bug that existed before.

private function waitUntilRequestQuotaAvailable()
{
    $sleep_until = (float)($this->currentStartTime + $this->intervalSeconds);
    $sleep_seconds = $sleep_until - microtime(true);

    // Avoid using time_sleep_until() as it appears to be less precise and not sleep long enough.
    usleep((int) $sleep_seconds * 1000000);

    // Ensure that enough time has passed as usleep() may not have waited long enough.
    $this->currentStartTime = microtime(true);
    if ($this->currentStartTime < $sleep_until) {
        do {
            usleep(1000000 / 4);
            $this->currentStartTime = microtime(true);
        } while ($this->currentStartTime < $sleep_until);
    }

    $this->currentRequestCount = 0;
}

The bug is in this line
usleep((int) $sleep_seconds * 1000000);
casting is performed on $sleep_seconds not on the product.

The new change does not really solve the problem.

private function waitUntilRequestQuotaAvailable()
{
    $sleep_until = (float)($this->currentStartTime + $this->intervalSeconds);
    $sleep_seconds = $sleep_until - microtime(true);

    // Avoid using time_sleep_until() as it appears to be less precise and not sleep long enough.
    // Avoid using usleep(): "Values larger than 1000000 (i.e. sleeping for
    //   more than a second) may not be supported by the operating system.
    //   Use sleep() instead."
    $sleep_seconds_int = (int)$sleep_seconds;
    if ($sleep_seconds_int >= 1) {
        sleep($sleep_seconds_int);
    }

    // Ensure that enough time has passed as usleep() may not have waited long enough.
    $this->currentStartTime = microtime(true);
    if ($this->currentStartTime < $sleep_until) {
        do {
            usleep(1000000 / 4);
            $this->currentStartTime = microtime(true);
        } while ($this->currentStartTime < $sleep_until);
    }

    $this->currentRequestCount = 0;
}

Тhe real solution is this function

private function waitUntilRequestQuotaAvailable()
{
    $sleep_until = (float) ($this->currentStartTime + $this->intervalSeconds);
    $sleep_seconds = $sleep_until - microtime(true);

    usleep((int) ($sleep_seconds * 1000000));

    $this->currentStartTime = microtime(true);
    $this->currentRequestCount = 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy