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

Retry on zero bytes written #149

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/WritableResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ final class WritableResourceStream extends EventEmitter implements WritableStrea
*/
private $writeChunkSize;

/**
* @var int
*/
private $maxConsecutiveZeroWrittenRetries;

private $listening = false;
private $writable = true;
private $closed = false;
private $data = '';
private $zeroWrites = 0;

public function __construct($stream, LoopInterface $loop, $writeBufferSoftLimit = null, $writeChunkSize = null)
public function __construct($stream, LoopInterface $loop, $writeBufferSoftLimit = null, $writeChunkSize = null, $maxConsecutiveZeroWrittenRetries = null)
{
if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") {
throw new \InvalidArgumentException('First parameter must be a valid stream resource');
Expand All @@ -47,6 +53,7 @@ public function __construct($stream, LoopInterface $loop, $writeBufferSoftLimit
$this->loop = $loop;
$this->softLimit = ($writeBufferSoftLimit === null) ? 65536 : (int)$writeBufferSoftLimit;
$this->writeChunkSize = ($writeChunkSize === null) ? -1 : (int)$writeChunkSize;
$this->maxConsecutiveZeroWrittenRetries = ($maxConsecutiveZeroWrittenRetries === null) ? 13 : (int)$maxConsecutiveZeroWrittenRetries;
}

public function isWritable()
Expand Down Expand Up @@ -136,7 +143,17 @@ public function handleWrite()
// report a temporary error (EAGAIN) which we do not raise here in order
// to keep the stream open for further tries to write.
// Should this turn out to be a permanent error later, it will eventually
// send *nothing* and we can detect this.
// send *nothing* and we can detect this. How ever, not every write that
// writes nothing is a closed stream, sometimes it's not ready yet even
// though it's reported as ready for writes. As such we should retry
// writing several times before giving up.
if ($sent === 0 && $error === null && $this->zeroWrites <= $this->maxConsecutiveZeroWrittenRetries) {
$this->zeroWrites++;
return;
}

$this->zeroWrites = 0;

if ($sent === 0 || $sent === false) {
if ($error !== null) {
$error = new \ErrorException(
Expand Down