-
-
Notifications
You must be signed in to change notification settings - Fork 167
Support HTTP keep-alive for HTTP client (reusing persistent connections) #486
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1c911d2
Refactor to add new `ClientConnectionManager` to manage HTTP connections
clue ab3bfee
Prepare to hand back connections when keep-alive is possible
clue 28943f4
Reuse existing connections for HTTP keep-alive
clue ebaf6f1
Add `Connection: close` default header to allow toggling keep-alive
clue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| <?php | ||
|
|
||
| namespace React\Http\Io; | ||
|
|
||
| use Psr\Http\Message\UriInterface; | ||
| use React\EventLoop\LoopInterface; | ||
| use React\EventLoop\TimerInterface; | ||
| use React\Promise\PromiseInterface; | ||
| use React\Socket\ConnectionInterface; | ||
| use React\Socket\ConnectorInterface; | ||
|
|
||
| /** | ||
| * [Internal] Manages outgoing HTTP connections for the HTTP client | ||
| * | ||
| * @internal | ||
| * @final | ||
| */ | ||
| class ClientConnectionManager | ||
| { | ||
| /** @var ConnectorInterface */ | ||
| private $connector; | ||
|
|
||
| /** @var LoopInterface */ | ||
| private $loop; | ||
|
|
||
| /** @var string[] */ | ||
| private $idleUris = array(); | ||
|
|
||
| /** @var ConnectionInterface[] */ | ||
| private $idleConnections = array(); | ||
|
|
||
| /** @var TimerInterface[] */ | ||
| private $idleTimers = array(); | ||
|
|
||
| /** @var \Closure[] */ | ||
| private $idleStreamHandlers = array(); | ||
|
|
||
| /** @var float */ | ||
| private $maximumTimeToKeepAliveIdleConnection = 0.001; | ||
|
|
||
| public function __construct(ConnectorInterface $connector, LoopInterface $loop) | ||
| { | ||
| $this->connector = $connector; | ||
| $this->loop = $loop; | ||
| } | ||
|
|
||
| /** | ||
| * @return PromiseInterface<ConnectionInterface> | ||
| */ | ||
| public function connect(UriInterface $uri) | ||
| { | ||
| $scheme = $uri->getScheme(); | ||
| if ($scheme !== 'https' && $scheme !== 'http') { | ||
| return \React\Promise\reject(new \InvalidArgumentException( | ||
| 'Invalid request URL given' | ||
| )); | ||
| } | ||
|
|
||
| $port = $uri->getPort(); | ||
| if ($port === null) { | ||
| $port = $scheme === 'https' ? 443 : 80; | ||
| } | ||
| $uri = ($scheme === 'https' ? 'tls://' : '') . $uri->getHost() . ':' . $port; | ||
|
|
||
| // Reuse idle connection for same URI if available | ||
| foreach ($this->idleConnections as $id => $connection) { | ||
| if ($this->idleUris[$id] === $uri) { | ||
| assert($this->idleStreamHandlers[$id] instanceof \Closure); | ||
| $connection->removeListener('close', $this->idleStreamHandlers[$id]); | ||
| $connection->removeListener('data', $this->idleStreamHandlers[$id]); | ||
| $connection->removeListener('error', $this->idleStreamHandlers[$id]); | ||
|
|
||
| assert($this->idleTimers[$id] instanceof TimerInterface); | ||
| $this->loop->cancelTimer($this->idleTimers[$id]); | ||
| unset($this->idleUris[$id], $this->idleConnections[$id], $this->idleTimers[$id], $this->idleStreamHandlers[$id]); | ||
|
|
||
| return \React\Promise\resolve($connection); | ||
| } | ||
| } | ||
|
|
||
| // Create new connection if no idle connection to same URI is available | ||
| return $this->connector->connect($uri); | ||
WyriHaximus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Hands back an idle connection to the connection manager for possible future reuse. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function keepAlive(UriInterface $uri, ConnectionInterface $connection) | ||
| { | ||
| $scheme = $uri->getScheme(); | ||
| assert($scheme === 'https' || $scheme === 'http'); | ||
|
|
||
| $port = $uri->getPort(); | ||
| if ($port === null) { | ||
| $port = $scheme === 'https' ? 443 : 80; | ||
| } | ||
|
|
||
| $this->idleUris[] = ($scheme === 'https' ? 'tls://' : '') . $uri->getHost() . ':' . $port; | ||
| $this->idleConnections[] = $connection; | ||
|
|
||
| $that = $this; | ||
| $cleanUp = function () use ($connection, $that) { | ||
| // call public method to support legacy PHP 5.3 | ||
| $that->cleanUpConnection($connection); | ||
| }; | ||
|
|
||
| // clean up and close connection when maximum time to keep-alive idle connection has passed | ||
| $this->idleTimers[] = $this->loop->addTimer($this->maximumTimeToKeepAliveIdleConnection, $cleanUp); | ||
|
|
||
| // clean up and close connection when unexpected close/data/error event happens during idle time | ||
| $this->idleStreamHandlers[] = $cleanUp; | ||
| $connection->on('close', $cleanUp); | ||
| $connection->on('data', $cleanUp); | ||
| $connection->on('error', $cleanUp); | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| * @return void | ||
| */ | ||
| public function cleanUpConnection(ConnectionInterface $connection) // private (PHP 5.4+) | ||
| { | ||
| $id = \array_search($connection, $this->idleConnections, true); | ||
| if ($id === false) { | ||
| return; | ||
| } | ||
|
|
||
| assert(\is_int($id)); | ||
| assert($this->idleTimers[$id] instanceof TimerInterface); | ||
| $this->loop->cancelTimer($this->idleTimers[$id]); | ||
| unset($this->idleUris[$id], $this->idleConnections[$id], $this->idleTimers[$id], $this->idleStreamHandlers[$id]); | ||
|
|
||
| $connection->close(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.