8000 Reduce memory consumption for failed connections by valga · Pull Request #113 · reactphp/socket · GitHub
[go: up one dir, main page]

Skip to content
8000

Reduce memory consumption for failed connections #113

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

Merged
merged 1 commit into from
Sep 4, 2017
Merged
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
39 changes: 12 additions & 27 deletions src/TcpConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,26 @@ public function connect($uri)

// wait for connection

return $this
->waitForStreamOnce($socket)
->then(array($this, 'checkConnectedSocket'))
->then(array($this, 'handleConnectedSocket'));
return $this->waitForStreamOnce($socket);
}

private function waitForStreamOnce($stream)
{
$loop = $this->loop;

return new Promise\Promise(function ($resolve) use ($loop, $stream) {
$loop->addWriteStream($stream, function ($stream) use ($loop, $resolve) {
return new Promise\Promise(function ($resolve, $reject) use ($loop, $stream) {
$loop->addWriteStream($stream, function ($stream) use ($loop, $resolve, $reject) {
$loop->removeWriteStream($stream);

$resolve($stream);
// The following hack looks like the only way to
// detect connection refused errors with PHP's stream sockets.
if (false === stream_socket_get_name($stream, true)) {
fclose($stream);

$reject(new \RuntimeException('Connection refused'));
} else {
$resolve(new Connection($stream, $loop));
}
});
}, function () use ($loop, $stream) {
$loop->removeWriteStream($stream);
Expand All @@ -113,24 +118,4 @@ private function waitForStreamOnce($stream)
throw new \RuntimeException('Cancelled while waiting for TCP/IP connection to be established');
});
}

/** @internal */
public function checkConnectedSocket($socket)
{
// The following hack looks like the only way to
// detect connection refused errors with PHP's stream sockets.
if (false === stream_socket_get_name($socket, true)) {
fclose($socket);

return Promise\reject(new \RuntimeException('Connection refused'));
}

return Promise\resolve($socket);
}

/** @internal */
public function handleConnectedSocket($socket)
{
return new Connection($socket, $this->loop);
}
}
0