Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [Unreleased]

### Added

- Internal `Innmind\HttpTransport\Config::asAsync()`

### Deprecated

- `Innmind\HttpTransport\Transport::async()`

## 9.2.0 - 2026-08-16

### Changed
Expand Down
42 changes: 42 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

namespace Innmind\HttpTransport;

use Innmind\HttpTransport\Config\Async;
use Innmind\IO\IO;
use Innmind\Url\Url;
use Innmind\Time\{
Clock,
Halt,
};
use Innmind\Immutable\Maybe;

/**
Expand All @@ -14,11 +20,13 @@ final class Config
/**
* @param Maybe<int<1, max>> $maxConcurrency
* @param Maybe<Url> $proxy
* @param Maybe<Async> $async,
*/
private function __construct(
private Maybe $maxConcurrency,
private bool $verifySSL,
private Maybe $proxy,
private Maybe $async,
) {
}

Expand All @@ -31,11 +39,14 @@ public static function new(): self
$maxConcurrency = Maybe::nothing();
/** @var Maybe<Url> */
$proxy = Maybe::nothing();
/** @var Maybe<Async> */
$async = Maybe::nothing();

return new self(
$maxConcurrency,
true,
$proxy,
$async,
);
}

Expand All @@ -49,6 +60,7 @@ public function limitConcurrencyTo(int $max): self
Maybe::just($max),
$this->verifySSL,
$this->proxy,
$this->async,
);
}

Expand All @@ -63,6 +75,7 @@ public function disableSSLVerification(): self
$this->maxConcurrency,
false,
$this->proxy,
$this->async,
);
}

Expand All @@ -73,6 +86,25 @@ public function throughProxy(Url $proxy): self
$this->maxConcurrency,
$this->verifySSL,
Maybe::just($proxy),
$this->async,
);
}

#[\NoDiscard]
public function asAsync(
Clock $clock,
Halt $halt,
IO $io,
): self {
return new self(
$this->maxConcurrency,
$this->verifySSL,
$this->proxy,
Maybe::just(new Async(
$clock,
$halt,
$io,
)),
);
}

Expand Down Expand Up @@ -103,4 +135,14 @@ public function proxy(): Maybe
{
return $this->proxy;
}

/**
* @internal
*
* @return Maybe<Async>
*/
public function async(): Maybe
{
return $this->async;
}
}
37 changes: 37 additions & 0 deletions src/Config/Async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types = 1);

namespace Innmind\HttpTransport\Config;

use Innmind\IO\IO;
use Innmind\Time\Clock;
use Innmind\Time\Halt;

/**
* @psalm-immutable
* @internal
*/
final class Async
{
public function __construct(
private Clock $clock,
private Halt $halt,
private IO $io,
) {
}

public function clock(): Clock
{
return $this->clock;
}

public function halt(): Halt
{
return $this->halt;
}

public function io(): IO
{
return $this->io;
}
}
1 change: 1 addition & 0 deletions src/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public static function logger(

/**
* @internal
* @deprecated
*
* @param callable(): void $heartbeat
*/
Expand Down
19 changes: 17 additions & 2 deletions src/Transport/CircuitBreaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Innmind\HttpTransport\Transport;

use Innmind\HttpTransport\{
Config,
Success,
ServerError,
ConnectionFailed,
Expand Down Expand Up @@ -78,14 +79,28 @@ public static function of(
#[\Override]
public function map(callable $map): self
{
$fulfill = $this->fulfill->map($map);

return new self(
$this->fulfill->map($map),
$this->clock,
$fulfill,
$fulfill->config()->async()->match(
static fn($async) => $async->clock(),
fn() => $this->clock,
),
$this->delayBeforeRetry,
Map::of(),
);
}

/**
* @psalm-mutation-free
*/
#[\Override]
public function config(): Config
{
return $this->fulfill->config();
}

private function open(
Request $request,
ServerError|ConnectionFailed $error,
Expand Down
27 changes: 24 additions & 3 deletions src/Transport/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,39 @@ public function map(callable $map): self
return new self(
$config,
$this->headerFactory,
$this->io,
$config->async()->match(
static fn($async) => $async->io(),
fn() => $this->io,
),
Concurrency::new($config->maxConcurrency()->match(
static fn($max) => $max,
static fn() => null,
)),
$this->timeout,
$this->heartbeat,
$config->async()->match(
static fn() => Period::millisecond(10), // this is blocking the active task so it needs to be low
fn() => $this->timeout,
),
$config
->async()
->map(static fn($async) => $async->halt())
->match(
static fn($halt) => static fn() => $halt(Period::millisecond(1))->unwrap(), // this allows to jump between tasks
fn() => $this->heartbeat,
),
!$config->verifySSL(),
$config->proxy()->match(
static fn($proxy) => $proxy,
static fn() => null,
),
);
}

/**
* @psalm-mutation-free
*/
#[\Override]
public function config(): Config
{
return $this->config;
}
}
19 changes: 17 additions & 2 deletions src/Transport/ExponentialBackoff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Innmind\HttpTransport\Transport;

use Innmind\HttpTransport\{
Config,
Success,
Information,
Redirection,
Expand Down Expand Up @@ -80,12 +81,26 @@ public static function of(
#[\Override]
public function map(callable $map): self
{
$fulfill = $this->fulfill->map($map);

return self::of(
$this->fulfill->map($map),
$this->halt,
$fulfill,
$fulfill->config()->async()->match(
static fn($async) => $async->halt(),
fn() => $this->halt,
),
);
}

/**
* @psalm-mutation-free
*/
#[\Override]
public function config(): Config
{
return $this->fulfill->config();
}

/**
* @param Sequence<Period> $retries
*
Expand Down
10 changes: 10 additions & 0 deletions src/Transport/FollowRedirections.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Innmind\HttpTransport\Transport;

use Innmind\HttpTransport\{
Config,
Success,
Redirection,
};
Expand Down Expand Up @@ -59,6 +60,15 @@ public function map(callable $map): self
return self::of($this->fulfill->map($map));
}

/**
* @psalm-mutation-free
*/
#[\Override]
public function config(): Config
{
return $this->fulfill->config();
}

/**
* @param int<0, max> $hops
*
Expand Down
6 changes: 6 additions & 0 deletions src/Transport/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public function __invoke(Request $request): Either;
*/
#[\NoDiscard]
public function map(callable $map): self;

/**
* @psalm-mutation-free
*/
#[\NoDiscard]
public function config(): Config;
}
10 changes: 10 additions & 0 deletions src/Transport/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Innmind\HttpTransport\Transport;

use Innmind\HttpTransport\{
Config,
Success,
Information,
Redirection,
Expand Down Expand Up @@ -62,6 +63,15 @@ public function map(callable $map): self
);
}

/**
* @psalm-mutation-free
*/
#[\Override]
public function config(): Config
{
return $this->fulfill->config();
}

private function logRequest(Request $request): string
{
$this->logger->debug(
Expand Down
19 changes: 17 additions & 2 deletions src/Transport/Via.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Innmind\HttpTransport\Success;
use Innmind\Http\Request;
use Innmind\HttpTransport\Config;
use Innmind\Immutable\Either;

/**
Expand All @@ -18,6 +19,7 @@ final class Via implements Implementation
*/
private function __construct(
private \Closure $fulfill,
private ?Config $config,
) {
}

Expand All @@ -33,7 +35,7 @@ public function __invoke(Request $request): Either
public static function of(callable $fulfill): self
{
// todo support exposing a Config to the callable ?
return new self(\Closure::fromCallable($fulfill));
return new self(\Closure::fromCallable($fulfill), null);
}

/**
Expand All @@ -42,6 +44,19 @@ public static function of(callable $fulfill): self
#[\Override]
public function map(callable $map): self
{
return $this;
/** @psalm-suppress ImpureFunctionCall */
return new self(
$this->fulfill,
$map($this->config()),
);
}

/**
* @psalm-mutation-free
*/
#[\Override]
public function config(): Config
{
return $this->config ?? Config::new();
}
}
Loading