Skip to content

Commit

Permalink
Use default or prior keep alive timeout if none is given
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jul 12, 2019
1 parent 5bd4a38 commit 5532eef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/Connection/DefaultConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function getConnection(Request $request, CancellationToken $cancellation)
if (!empty($this->connections[$key])) {
foreach ($this->connections[$key] as $index => $connection) {
if ($connection instanceof Promise) {
// Wait for concurrent connection request before creating a new connection.
$connection = yield $connection;
}

Expand Down Expand Up @@ -132,6 +133,7 @@ public function getConnection(Request $request, CancellationToken $cancellation)
return;
}

// Replace Promise with Connection object.
$this->connections[$key][$index] = $connection;
});

Expand Down
17 changes: 10 additions & 7 deletions src/Connection/Http1Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ final class Http1Connection implements Connection
/** @var string|null Keep alive timeout watcher ID. */
private $timeoutWatcher;

/** @var int Keep-Alive timeout from last response. */
private $priorTimeout = self::MAX_KEEP_ALIVE_TIMEOUT;

/** @var callable[]|null */
private $onClose = [];

public function __construct(Socket $socket)
{
$this->socket = $socket;

if ($this->socket->isClosed()) {
$this->onClose = null;
}
}

public function isBusy(): bool
Expand Down Expand Up @@ -258,7 +265,7 @@ private function doRead(
}
}

if ($timeout = $this->determineKeepAliveTimeout($response)) {
if ($parser->getState() !== Http1Parser::BODY_IDENTITY_EOF && $timeout = $this->determineKeepAliveTimeout($response)) {
$this->timeoutWatcher = Loop::delay($timeout * 1000, [$this, 'close']);
Loop::unreference($this->timeoutWatcher);
} else {
Expand Down Expand Up @@ -390,7 +397,7 @@ private function determineKeepAliveTimeout(Response $response): int
$responseKeepAliveHeader = $response->getHeader('keep-alive');

if ($responseKeepAliveHeader === null) {
return self::MAX_KEEP_ALIVE_TIMEOUT;
return $this->priorTimeout;
}

$parts = \array_map('trim', \explode(',', $responseKeepAliveHeader));
Expand All @@ -401,11 +408,7 @@ private function determineKeepAliveTimeout(Response $response): int
$params[$key] = (int) $value;
}

if ($this->requestCounter >= $params['max'] ?? \PHP_INT_MAX) {
return 0;
}

return \min(\max(0, $params['timeout'] ?? 0), self::MAX_KEEP_ALIVE_TIMEOUT);
return $this->priorTimeout = \min(\max(0, $params['timeout'] ?? 0), self::MAX_KEEP_ALIVE_TIMEOUT);
}

private function determineProtocolVersion(Request $request): string
Expand Down

0 comments on commit 5532eef

Please sign in to comment.