Skip to content

[WIP][RFC] Concept for connection timeout #10

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": ">=5.4.0",
"react/dns": "0.4.*",
"react/event-loop": "0.4.*",
"react/promise": "~2.0"
"react/promise": "~2.2"
},
"autoload": {
"psr-4": {
Expand Down
34 changes: 23 additions & 11 deletions src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use React\Dns\Resolver\Resolver;
use React\Stream\Stream;
use React\Promise;
use React\Promise\Deferred;

class Connector implements ConnectorInterface
{
use TimeoutTrait;

private $loop;
private $resolver;

Expand All @@ -28,7 +29,7 @@ public function create($host, $port)
});
}

public function createSocketForAddress($address, $port)
public function createSocketForAddress($address, $port, $timeout = 30)
{
$url = $this->getSocketUrl($address, $port);

Expand All @@ -46,24 +47,35 @@ public function createSocketForAddress($address, $port)
// wait for connection

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

protected function waitForStreamOnce($stream)
protected function waitForStreamOnce($stream, $timeout)
{
$deferred = new Deferred();
$resolver = function(callable $resolve, callable $reject, callable $notify) use ($stream) {
$this->loop->addWriteStream($stream, function($stream) use ($resolve) {
$this->loop->removeWriteStream($stream);

$resolve($stream);
});
};

$canceller = function(callable $resonse, callable $reject, callable $progress) use ($stream, $timeout) {
$this->loop->removeWriteStream($stream);

stream_socket_shutdown($stream, STREAM_SHUT_RDWR);
fclose($stream);

$loop = $this->loop;
$reject(new \RuntimeException("Timeout: failed to connect after {$timeout} seconds"));
};

$this->loop->addWriteStream($stream, function ($stream) use ($loop, $deferred) {
$loop->removeWriteStream($stream);
$promise = new Promise\Promise($resolver, $canceller);

$deferred->resolve($stream);
});
$this->setTimeout($this->loop, $promise, $timeout);

return $deferred->promise();
return $promise;
}

public function checkConnectedSocket($socket)
Expand Down
22 changes: 22 additions & 0 deletions src/TimeoutTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace React\SocketClient;

use React\EventLoop\LoopInterface;
use React\Promise\CancellablePromiseInterface;

trait TimeoutTrait
{
protected function setTimeout(LoopInterface $loop, CancellablePromiseInterface $promise, $seconds = 30)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is much gained from having a default value for $seconds here? Any value is pretty arbitrary.

{
$seconds = (int)$seconds;

$timer = $loop->addTimer($seconds, function() use ($promise, $seconds) {
$promise->cancel();
});

$promise->then(function() use ($timer) {
$timer->cancel();
});
}
}