Skip to content

Commit

Permalink
More use of property promotion and other code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Dec 7, 2024
1 parent d23e0b8 commit 3088887
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 122 deletions.
7 changes: 2 additions & 5 deletions src/Connection/ConnectionLimitingPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ private static function formatUri(Request $request): string
return $scheme . '://' . $authority;
}

private int $connectionLimit;

private ConnectionFactory $connectionFactory;

/** @var array<string, \ArrayObject<int, Future<Connection>>> */
Expand All @@ -67,14 +65,13 @@ private static function formatUri(Request $request): string

private int $openConnectionCount = 0;

private function __construct(int $connectionLimit, ?ConnectionFactory $connectionFactory = null)
private function __construct(private readonly int $connectionLimit, ?ConnectionFactory $connectionFactory = null)
{
if ($connectionLimit < 1) {
throw new \Error('The connection limit must be greater than 0');
}

$this->connectionLimit = $connectionLimit;
$this->connectionFactory = $connectionFactory ?? new DefaultConnectionFactory;
$this->connectionFactory = $connectionFactory ?? new DefaultConnectionFactory();
}

public function __clone()
Expand Down
15 changes: 7 additions & 8 deletions src/Connection/DefaultConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@

final class DefaultConnectionFactory implements ConnectionFactory
{
private ?Socket\SocketConnector $connector;
private readonly ConnectContext $connectContext;

private ?ConnectContext $connectContext;

public function __construct(?Socket\SocketConnector $connector = null, ?ConnectContext $connectContext = null)
{
$this->connector = $connector;
$this->connectContext = $connectContext;
public function __construct(
private readonly ?Socket\SocketConnector $connector = null,
?ConnectContext $connectContext = null,
) {
$this->connectContext = $connectContext ?? new ConnectContext();
}

public function create(Request $request, Cancellation $cancellation): Connection
{
$connectStart = now();

$connector = $this->connector ?? Socket\socketConnector();
$connectContext = $this->connectContext ?? new ConnectContext;
$connectContext = $this->connectContext;

$uri = $request->getUri();
$scheme = $uri->getScheme();
Expand Down
5 changes: 1 addition & 4 deletions src/Connection/Http1Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ final class Http1Connection implements Connection
/** @var list<\Closure():void>|null */
private ?array $onClose = [];

private float $timeoutGracePeriod;

private float $lastUsedAt;

private bool $explicitTimeout = false;
Expand All @@ -87,13 +85,12 @@ public function __construct(
Socket $socket,
private readonly float $connectDuration,
private readonly ?float $tlsHandshakeDuration,
float $timeoutGracePeriod = 2
private readonly float $timeoutGracePeriod = 2,
) {
$this->socket = $socket;
$this->localAddress = $socket->getLocalAddress();
$this->remoteAddress = $socket->getRemoteAddress();
$this->tlsInfo = $socket->getTlsInfo();
$this->timeoutGracePeriod = $timeoutGracePeriod;
$this->lastUsedAt = now();
$this->watchIdleConnection();
}
Expand Down
67 changes: 37 additions & 30 deletions src/Connection/HttpStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,73 @@
use Amp\Socket\TlsInfo;
use function Amp\Http\Client\processRequest;

/**
* @psalm-type RequestCallbackType = callable(Request, Cancellation, HttpStream):Response
* @psalm-type ReleaseCallbackType = callable():void
*/
final class HttpStream implements Stream
{
use ForbidSerialization;
use ForbidCloning;

/**
* @param RequestCallbackType $RequestCallbackType
* @param ReleaseCallbackType $ReleaseCallbackType
*/
public static function fromConnection(
Connection $connection,
callable $requestCallback,
callable $releaseCallback
callable $RequestCallbackType,
callable $ReleaseCallbackType
): self {
return new self(
$connection->getLocalAddress(),
$connection->getRemoteAddress(),
$connection->getTlsInfo(),
$requestCallback,
$releaseCallback
$RequestCallbackType,
$ReleaseCallbackType,
);
}

public static function fromStream(Stream $stream, callable $requestCallback, callable $releaseCallback): self
/**
* @param RequestCallbackType $RequestCallbackType
* @param ReleaseCallbackType $ReleaseCallbackType
*/
public static function fromStream(Stream $stream, callable $RequestCallbackType, callable $ReleaseCallbackType): self
{
return new self(
$stream->getLocalAddress(),
$stream->getRemoteAddress(),
$stream->getTlsInfo(),
$requestCallback,
$releaseCallback
$RequestCallbackType,
$ReleaseCallbackType,
);
}

private SocketAddress $localAddress;

private SocketAddress $remoteAddress;

private ?TlsInfo $tlsInfo;

/** @var callable */
private $requestCallback;
private $RequestCallbackType;

/** @var callable|null */
private $releaseCallback;
private $ReleaseCallbackType;

/**
* @param RequestCallbackType $RequestCallbackType
* @param ReleaseCallbackType $ReleaseCallbackType
*/
private function __construct(
SocketAddress $localAddress,
SocketAddress $remoteAddress,
?TlsInfo $tlsInfo,
callable $requestCallback,
callable $releaseCallback
private readonly SocketAddress $localAddress,
private readonly SocketAddress $remoteAddress,
private readonly ?TlsInfo $tlsInfo,
callable $RequestCallbackType,
callable $ReleaseCallbackType,
) {
$this->localAddress = $localAddress;
$this->remoteAddress = $remoteAddress;
$this->tlsInfo = $tlsInfo;
$this->requestCallback = $requestCallback;
$this->releaseCallback = $releaseCallback;
$this->RequestCallbackType = $RequestCallbackType;
$this->ReleaseCallbackType = $ReleaseCallbackType;
}

public function __destruct()
{
if ($this->releaseCallback !== null) {
($this->releaseCallback)();
if ($this->ReleaseCallbackType !== null) {
($this->ReleaseCallbackType)();
}
}

Expand All @@ -80,13 +87,13 @@ public function __destruct()
*/
public function request(Request $request, Cancellation $cancellation): Response
{
if ($this->releaseCallback === null) {
if ($this->ReleaseCallbackType === null) {
throw new \Error('A stream may only be used for a single request');
}

$this->releaseCallback = null;
$this->ReleaseCallbackType = null;

return processRequest($request, [], fn (): Response => ($this->requestCallback)($request, $cancellation, $this));
return processRequest($request, [], fn (): Response => ($this->RequestCallbackType)($request, $cancellation, $this));
}

public function getLocalAddress(): SocketAddress
Expand Down
13 changes: 5 additions & 8 deletions src/Connection/InterceptedStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ final class InterceptedStream implements Stream

private static \WeakMap $requestInterceptors;

private Stream $stream;

private ?NetworkInterceptor $interceptor;

public function __construct(Stream $stream, NetworkInterceptor $interceptor)
public function __construct(private readonly Stream $stream, NetworkInterceptor $interceptor)
{
$this->stream = $stream;
$this->interceptor = $interceptor;
}

Expand All @@ -37,14 +34,14 @@ public function __construct(Stream $stream, NetworkInterceptor $interceptor)
public function request(Request $request, Cancellation $cancellation): Response
{
return processRequest($request, [], function () use ($request, $cancellation): Response {
if (!$this->interceptor) {
$interceptor = $this->interceptor;
$this->interceptor = null;

if (!$interceptor) {
throw new \Error(__METHOD__ . ' may only be invoked once per instance. '
. 'If you need to implement retries or otherwise issue multiple requests, register an ApplicationInterceptor to do so.');
}

$interceptor = $this->interceptor;
$this->interceptor = null;

/** @psalm-suppress RedundantPropertyInitializationCheck */
self::$requestInterceptors ??= new \WeakMap();
$requestInterceptors = self::$requestInterceptors[$request] ?? [];
Expand Down
37 changes: 16 additions & 21 deletions src/Connection/Internal/Http1Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Http1\Rfc7230;
use Amp\Http\HttpMessage;
use Amp\Http\HttpStatus;
use Amp\Http\InvalidHeaderException;
use function Amp\Http\Client\events;
use function Amp\Http\mapHeaderPairs;

/** @internal */
/**
* @internal
*
* @psalm-import-type HeaderMapType from HttpMessage
*/
final class Http1Parser
{
use ForbidSerialization;
Expand All @@ -34,10 +39,6 @@ final class Http1Parser
public const TRAILERS_START = 4;
public const TRAILERS = 5;

private Request $request;

private Stream $stream;

private ?Response $response = null;

private int $state = self::AWAITING_HEADERS;
Expand All @@ -57,26 +58,20 @@ final class Http1Parser

private bool $complete = false;

private int $maxHeaderBytes;

private int $maxBodyBytes;
private readonly int $maxHeaderBytes;

/** @var callable */
private $bodyDataCallback;

/** @var callable */
private $trailersCallback;
private readonly int $maxBodyBytes;

/**
* @param \Closure(string):void $bodyDataCallback
* @param \Closure(HeaderMapType):void $trailersCallback
*/
public function __construct(
Request $request,
Stream $stream,
callable $bodyDataCallback,
callable $trailersCallback,
private readonly Request $request,
private readonly Stream $stream,
private readonly \Closure $bodyDataCallback,
private readonly \Closure $trailersCallback,
) {
$this->request = $request;
$this->stream = $stream;
$this->bodyDataCallback = $bodyDataCallback;
$this->trailersCallback = $trailersCallback;
$this->maxHeaderBytes = $request->getHeaderSizeLimit();
$this->maxBodyBytes = $request->getBodySizeLimit();
}
Expand Down
18 changes: 8 additions & 10 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
*/
final class HttpClient implements DelegateHttpClient
{
private DelegateHttpClient $httpClient;

/** @var EventListener[] */
private array $eventListeners;

public function __construct(DelegateHttpClient $httpClient, array $eventListeners)
{
$this->httpClient = $httpClient;
$this->eventListeners = $eventListeners;
/**
* @param EventListener[] $eventListeners
*/
public function __construct(
private readonly DelegateHttpClient $httpClient,
private readonly array $eventListeners,
) {
}

/**
Expand All @@ -32,7 +30,7 @@ public function request(Request $request, ?Cancellation $cancellation = null): R
return processRequest(
$request,
$this->eventListeners,
fn () => $this->httpClient->request($request, $cancellation ?? new NullCancellation())
fn () => $this->httpClient->request($request, $cancellation ?? new NullCancellation()),
);
}
}
20 changes: 7 additions & 13 deletions src/InterceptedHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,22 @@ final class InterceptedHttpClient implements DelegateHttpClient

private static \WeakMap $requestInterceptors;

private DelegateHttpClient $httpClient;

private ApplicationInterceptor $interceptor;

/** @var EventListener[] */
private array $eventListeners;

/**
* @param EventListener[] $eventListeners
*/
public function __construct(
DelegateHttpClient $httpClient,
ApplicationInterceptor $interceptor,
array $eventListeners
private readonly DelegateHttpClient $httpClient,
private readonly ApplicationInterceptor $interceptor,
private readonly array $eventListeners,
) {
$this->httpClient = $httpClient;
$this->interceptor = $interceptor;
$this->eventListeners = $eventListeners;
}

public function request(Request $request, Cancellation $cancellation): Response
{
return processRequest($request, $this->eventListeners, function () use ($request, $cancellation) {
/** @psalm-suppress RedundantPropertyInitializationCheck */
self::$requestInterceptors ??= new \WeakMap();

$requestInterceptors = self::$requestInterceptors[$request] ?? [];
$requestInterceptors[] = $this->interceptor;
self::$requestInterceptors[$request] = $requestInterceptors;
Expand Down
2 changes: 1 addition & 1 deletion src/Interceptor/DecompressResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class DecompressResponse implements NetworkInterceptor
use ForbidCloning;
use ForbidSerialization;

private bool $hasZlib;
private readonly bool $hasZlib;

public function __construct()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Interceptor/FollowRedirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ private static function mergePaths(string $basePath, string $pathToMerge): strin
return self::removeDotSegments(\implode('/', $parts));
}

private int $maxRedirects;
private readonly int $maxRedirects;

private bool $autoReferrer;
private readonly bool $autoReferrer;

public function __construct(int $limit, bool $autoReferrer = true)
{
Expand Down
Loading

0 comments on commit 3088887

Please sign in to comment.