-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
218 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,60 @@ | ||
<?php | ||
|
||
use Amp\CancellationToken; | ||
use Amp\Http\Client\Connection\LimitedConnectionPool; | ||
use Amp\Http\Client\Connection\Stream; | ||
use Amp\Http\Client\Connection\UnlimitedConnectionPool; | ||
use Amp\Http\Client\HostRequestKey; | ||
use Amp\Http\Client\HttpClientBuilder; | ||
use Amp\Http\Client\HttpException; | ||
use Amp\Http\Client\NetworkInterceptor; | ||
use Amp\Http\Client\Request; | ||
use Amp\Http\Client\Response; | ||
use Amp\Loop; | ||
use Amp\Promise; | ||
use Amp\Sync\LocalKeyedSemaphore; | ||
use function Amp\call; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
Loop::run(static function () { | ||
try { | ||
// There's no need to create a custom pool here, we just need it to access the statistics. | ||
$pool = new LimitedConnectionPool(new UnlimitedConnectionPool, new LocalKeyedSemaphore(1), new HostRequestKey); | ||
|
||
$logger = new class implements NetworkInterceptor { | ||
public function requestViaNetwork( | ||
Request $request, | ||
CancellationToken $cancellation, | ||
Stream $stream | ||
): Promise { | ||
return call(static function () use ($request, $cancellation, $stream) { | ||
print 'Starting request to ' . $request->getUri() . '...' . PHP_EOL; | ||
|
||
try { | ||
return yield $stream->request($request, $cancellation); | ||
} finally { | ||
print 'Done @ ' . $request->getUri() . ' ' . PHP_EOL; | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
$client = (new HttpClientBuilder)->usingPool($pool)->followRedirects(0)->interceptNetwork($logger)->build(); | ||
|
||
for ($i = 0; $i < 3; $i++) { | ||
$promises = []; | ||
for ($j = 0; $j < 10; $j++) { | ||
$promises[] = call(static function () use ($client, $i, $j) { | ||
/** @var Response $response */ | ||
$response = yield $client->request(new Request("http://amphp.org/$i.$j")); | ||
yield $response->getBody()->buffer(); | ||
}); | ||
} | ||
|
||
yield $promises; | ||
} | ||
} catch (HttpException $error) { | ||
echo $error; | ||
} | ||
}); |
This file contains 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 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 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 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 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 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 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,70 @@ | ||
<?php | ||
|
||
namespace Amp\Http\Client\Connection; | ||
|
||
use Amp\CancellationToken; | ||
use Amp\Http\Client\RequestKey; | ||
use Amp\Http\Client\Request; | ||
use Amp\Http\Client\Response; | ||
use Amp\Promise; | ||
use Amp\Sync\KeyedSemaphore; | ||
use Amp\Sync\Lock; | ||
use function Amp\call; | ||
use function Amp\coroutine; | ||
|
||
final class LimitedConnectionPool implements ConnectionPool | ||
{ | ||
/** @var ConnectionPool */ | ||
private $delegate; | ||
|
||
/** @var KeyedSemaphore */ | ||
private $semaphore; | ||
|
||
/** @var RequestKey */ | ||
private $keyExtractor; | ||
|
||
public function __construct(ConnectionPool $delegate, KeyedSemaphore $semaphore, RequestKey $keyExtractor) | ||
{ | ||
$this->delegate = $delegate; | ||
$this->semaphore = $semaphore; | ||
$this->keyExtractor = $keyExtractor; | ||
} | ||
|
||
public function getStream(Request $request, CancellationToken $token): Promise | ||
{ | ||
return call(function () use ($request, $token) { | ||
/** @var Lock $lock */ | ||
$lock = yield $this->semaphore->acquire($this->keyExtractor->getKey($request)); | ||
|
||
/** @var Stream $stream */ | ||
$stream = yield $this->delegate->getStream($request, $token); | ||
|
||
return HttpStream::fromStream( | ||
$stream, | ||
coroutine(static function (Request $request, CancellationToken $cancellationToken) use ( | ||
$stream, | ||
$lock | ||
) { | ||
try { | ||
/** @var Response $response */ | ||
$response = yield $stream->request($request, $cancellationToken); | ||
|
||
// await response being completely received | ||
$response->getTrailers()->onResolve(static function () use ($lock) { | ||
$lock->release(); | ||
}); | ||
} catch (\Throwable $e) { | ||
$lock->release(); | ||
|
||
throw $e; | ||
} | ||
|
||
return $response; | ||
}), | ||
static function () use ($lock) { | ||
$lock->release(); | ||
} | ||
); | ||
}); | ||
} | ||
} |
This file contains 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 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,11 @@ | ||
<?php | ||
|
||
namespace Amp\Http\Client; | ||
|
||
final class HostRequestKey implements RequestKey | ||
{ | ||
public function getKey(Request $request): string | ||
{ | ||
return $request->getUri()->getHost(); | ||
} | ||
} |
This file contains 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.