-
Notifications
You must be signed in to change notification settings - Fork 2
Connection: keep-alive #22
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
Open
thekid
wants to merge
14
commits into
master
Choose a base branch
from
feature/keep-alive
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
39b4978
Abstract I/O functionality to Channel class
thekid f8d1073
Refactor reading HTTP body to separate classes:
thekid 96bd585
Extract HTTP body reading
thekid c1df9a3
Remove hardcoded "Connection: keep-alive"
thekid 3d72b56
Close existing socket when bind() is called
thekid a0e907d
Remove `use lang\Object` (PHP 7.2 compat)
thekid c3ae515
Do not reuse connections if server responds with "Connection: close"
thekid 7268fda
Add close operation to explicitely disconnect
thekid b5e9746
Fix HHVM compatibility
thekid 22a1c80
Reconnect if reused connection has been closed on the server side
thekid ce0fd4e
Add tests for Channel class
thekid 47992b6
Always close connections for HTTP/1.0 servers
thekid 0386340
Add test
thekid 0051f70
Simplify code
thekid 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?php namespace peer\http; | ||
|
|
||
| use peer\SocketInputStream; | ||
|
|
||
| class Channel implements \io\streams\InputStream { | ||
| private $socket; | ||
| private $reuseable= null; | ||
|
|
||
| /** @param peer.Socket */ | ||
| public function __construct($socket) { | ||
| $this->socket= $socket; | ||
| } | ||
|
|
||
| /** @return peer.Socket */ | ||
| public function socket() { return $this->socket; } | ||
|
|
||
| /** | ||
| * Rebinds to a new socket, closing the existing one if necessary | ||
| * | ||
| * @param peer.Socket | ||
| * @return void | ||
| */ | ||
| public function bind($socket) { | ||
| if ($this->socket->isConnected()) { | ||
| $this->socket->close(); | ||
| } | ||
| $this->socket= $socket; | ||
| } | ||
|
|
||
| /** | ||
| * Disconnect (if necessary) | ||
| * | ||
| * @return void | ||
| */ | ||
| public function disconnect() { | ||
| $this->socket->isConnected() && $this->socket->close(); | ||
| } | ||
|
|
||
| /** | ||
| * Sends a request and returns the response | ||
| * | ||
| * @param peer.http.HttpRequest $request | ||
| * @param float $connectTimeout | ||
| * @param float $readTimeout | ||
| * @return peer.http.HttpResponse | ||
| */ | ||
| public function send($request, $connectTimeout, $readTimeout) { | ||
|
|
||
| // Previous call didn't finish reading all data, connection is not reusable | ||
| if (false === $this->reuseable) { | ||
| $this->socket->close(); | ||
| } | ||
|
|
||
| do { | ||
| if ($this->socket->isConnected()) { | ||
| $reused= true; | ||
| } else { | ||
| $reused= false; | ||
| $this->socket->setTimeout($readTimeout); | ||
| $this->socket->connect($connectTimeout); | ||
| } | ||
|
|
||
| $this->socket->write($request->getRequestString()); | ||
| $this->reuseable= false; | ||
| $input= new HttpInputStream($this, function() { $this->reuseable= true; }); | ||
|
|
||
| // If we reused the connection and we receive EOF, disconnect & retry | ||
| $line= $input->readLine(); | ||
| if ($this->socket->eof()) { | ||
| if (!$reused) throw new SocketException('Received EOF (timeout: '.$readTimeout.' seconds)'); | ||
| $this->socket->close(); | ||
| continue; | ||
| } | ||
|
|
||
| // Success | ||
| $input->pushBack($line."\r\n"); | ||
| return new HttpResponse($input, true); | ||
| } while (true); | ||
| } | ||
|
|
||
| /** @return int */ | ||
| public function available() { | ||
| return $this->socket->eof() ? 0 : 1; | ||
| } | ||
|
|
||
| /** @return string */ | ||
| public function read($limit= 8192) { | ||
| return $this->socket->readBinary($limit); | ||
| } | ||
|
|
||
| /** @return void */ | ||
| public function close() { | ||
| // NOOP | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class needs some tests!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script used for integration testing timeouts and reconnection behaviors: