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

Naive attempt to implement keepalive connections #132

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
31 changes: 28 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,41 @@ class Client
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null)
{
if ($connector === null) {
$connector = new Connector($loop);
$connector = new KeepaliveConnector(new Connector($loop));
}

$this->connector = $connector;
$this->connector = new KeepaliveConnector($connector);
}

public function request($method, $url, array $headers = array(), $protocolVersion = '1.0')
{
$requestData = new RequestData($method, $url, $headers, $protocolVersion);

return new Request($this->connector, $requestData);
$that = $this;
$request = new Request($this->connector, $requestData);
$request->on('close', function() use ($that, $url) {
$that->connector->handleConnectionClose($url);
});
$request->on('response',
function(Response $response) use ($url, $headers, $that) {
if ($that->isConnectionClose($headers)
|| $that->isConnectionClose($response->getHeaders())) {
$that->connector->handleConnectionClose($url);
}
}
);

return $request;
}

private function isConnectionClose($headers)
{
$normalizedHeaders = array_change_key_case($headers, CASE_LOWER);

if (!isset($normalizedHeaders['connection'])) {
return false;
}

return strtolower($normalizedHeaders['connection']) === 'close';
}
}
47 changes: 47 additions & 0 deletions src/KeepaliveConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace React\HttpClient;

use React\Promise\PromiseInterface;
use React\Socket\ConnectionInterface;
use React\Socket\ConnectorInterface;

/**
* @internal
*/
class KeepaliveConnector implements ConnectorInterface
{
private $directConnector;

/** @var PromiseInterface[] */
private $aliveConnections = array();

public function __construct(ConnectorInterface $directConnector)
{
$this->directConnector = $directConnector;
}

public function connect($uri)
{
return isset($this->aliveConnections[$uri])
? $this->tryReuseAliveConnection($uri)
: $this->createNewConnection($uri);
}

public function handleConnectionClose($uri)
{
unset($this->aliveConnections[$uri]);
}

private function tryReuseAliveConnection($uri)
{
return $this->aliveConnections[$uri];
Copy link
Author

Choose a reason for hiding this comment

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

there was a check for ConnectionInterface#isWritable() but then i realized $this->aliveConnections is actually an array of Promises.

}

private function createNewConnection($uri)
{
$connection = $this->directConnector->connect($uri);
$this->aliveConnections[$uri] = $connection;
return $connection;
}
}