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 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
32 changes: 29 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,42 @@ 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;
}

/** @internal */
public function isConnectionClose($headers)
{
$normalizedHeaders = array_change_key_case($headers, CASE_LOWER);

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

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

namespace React\HttpClient;

use React\Promise\PromiseInterface;
use React\Promise;
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 Promise\resolve($this->aliveConnections[$uri]);
}

private function createNewConnection($uri)
{
$connection = $this->directConnector->connect($uri);

$that = $this;
return $connection->then(function(ConnectionInterface $connection) use ($that, $uri) {
$that->aliveConnections[$uri] = $connection;
return $connection;
});
}
}
6 changes: 3 additions & 3 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ public function handleDrain()
/** @internal */
public function handleData($data)
{
$this->buffer .= $data;

// buffer until double CRLF (or double LF for compatibility with legacy servers)
if (false !== strpos($this->buffer, "\r\n\r\n") || false !== strpos($this->buffer, "\n\n")) {
if (false !== strpos($data, "\r\n\r\n") || false !== strpos($data, "\n\n")) {
$this->buffer .= $data;

try {
list($response, $bodyChunk) = $this->parseResponse($this->buffer);
} catch (\InvalidArgumentException $exception) {
Expand Down