Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting a response as NULL after 4 minutes #75

Closed
marioshtika opened this issue Feb 18, 2020 · 5 comments
Closed

Getting a response as NULL after 4 minutes #75

marioshtika opened this issue Feb 18, 2020 · 5 comments

Comments

@marioshtika
Copy link

I am sending multiple requests to one or more TCP client, using the below code

$promises = [];
foreach ($packets as $packet) {
    $promises[] = call(function () use ($packet, $uri) {

        /** @var \Amp\Socket\ClientSocket $socket */
        $socket = yield connect($uri);
        try {
            yield $socket->write($packet);

            $chunk = yield $socket->read(); // modbus packet is so small that one read is enough
            if ($chunk === null) {
                return null;
            }
            return ResponseFactory::parseResponse($chunk);
        } finally {
            $socket->close();
        }
    });
}

try {
    // will run multiple request in parallel using non-blocking php stream io
    $responses = wait(all($promises));
    // print_r($responses);
} catch (Throwable $e) {
    print_r($e);
}

One of the clients is delaying too much and after approximately 4 minutes it returns "null".

Can you help me understand what happens after 4 minutes?
Is this something like a timeout for the read command?
Can I change it for example to 1 minute?

Thank you in advance

@kelunik
Copy link
Member

kelunik commented Feb 19, 2020

My guess is that the other side closes the connection after that time.

@marioshtika
Copy link
Author

Thank you @kelunik for the quick response.

Is there a function to add a timeout for the read() command?

Thank you in advance?

@kelunik
Copy link
Member

kelunik commented Feb 19, 2020

We currently don't have cancelation support for reads, but you can use Amp\Promise\timeout to apply a timeout to any promise. However, this will not cancel the operation, but closing the socket on timeout will.

@marioshtika
Copy link
Author

Thank you @kelunik for your suggestion.

Do you think it's ok to put that timeout() before the read() function, since read() also returns a promise, instead of putting it in the wait() function?

Example:

$chunk = yield timeout($socket->read(), 1000);

@kelunik
Copy link
Member

kelunik commented Feb 19, 2020

Yup, that's generally how you do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants