-
Notifications
You must be signed in to change notification settings - Fork 0
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
20 changed files
with
247 additions
and
245 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
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jenky\Atlas\Pool\Concurrency; | ||
|
||
enum Driver | ||
{ | ||
case PSL; | ||
case REACT; | ||
} |
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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jenky\Atlas\Pool\Concurrency; | ||
|
||
use Clue\React\Mq\Queue; | ||
use Psl\Async\Awaitable; | ||
|
||
final class DriverDiscovery | ||
{ | ||
private static ?Driver $cached = null; | ||
|
||
private static ?Driver $prefer = null; | ||
|
||
/** | ||
* Find the appropriate async driver based on the installed package. | ||
* | ||
* @throws \RuntimeException | ||
*/ | ||
public static function find(bool $cacheResult = true): Driver | ||
{ | ||
if (self::$prefer !== null) { | ||
return self::$prefer; | ||
} | ||
|
||
if ($cacheResult && self::$cached !== null) { | ||
return self::$cached; | ||
} | ||
|
||
if (self::isPslInstalled()) { | ||
$driver = Driver::PSL; | ||
} elseif (self::isReactInstalled()) { | ||
$driver = Driver::REACT; | ||
} else { | ||
throw new \RuntimeException('Unable to find async driver.'); | ||
} | ||
|
||
if ($cacheResult) { | ||
self::$cached = $driver; | ||
} | ||
|
||
return $driver; | ||
} | ||
|
||
/** | ||
* Set the preferred async driver. | ||
*/ | ||
public static function prefer(Driver $driver): void | ||
{ | ||
self::$prefer = $driver; | ||
} | ||
|
||
public static function isReactInstalled(): bool | ||
{ | ||
return \function_exists('React\\Async\\async') && \class_exists(Queue::class); | ||
} | ||
|
||
public static function isPslInstalled(): bool | ||
{ | ||
return \class_exists(Awaitable::class); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jenky\Atlas\Pool; | ||
|
||
use Jenky\Atlas\Contracts\ConnectorInterface; | ||
use Jenky\Atlas\Pool\Client\AsyncClientInterface; | ||
use Jenky\Atlas\Pool\Exception\InvalidPoolRequestException; | ||
use Jenky\Atlas\Pool\Exception\UnsupportedClientException; | ||
use Jenky\Atlas\Request; | ||
use Jenky\Atlas\Response; | ||
|
||
/** | ||
* @implements PoolInterface<Request|callable(ConnectorInterface): Response, Response> | ||
*/ | ||
final class ConnectorPool implements PoolInterface | ||
{ | ||
use PoolTrait; | ||
|
||
private AsyncClientInterface $client; | ||
|
||
public function __construct(private readonly ConnectorInterface $connector) | ||
{ | ||
$client = $connector->client(); | ||
|
||
if (! $client instanceof AsyncClientInterface) { | ||
// @codeCoverageIgnoreStart | ||
throw new UnsupportedClientException(\sprintf( | ||
'The client %s is not supported. Please swap the underlying client to supported one.', | ||
\get_debug_type($client) | ||
)); | ||
// @codeCoverageIgnoreEnd | ||
} | ||
|
||
$this->client = $client; | ||
} | ||
|
||
public function send(iterable $requests): array | ||
{ | ||
$promises = static function (ConnectorInterface $connector, iterable $requests) { | ||
foreach ($requests as $key => $request) { | ||
if ($request instanceof Request) { | ||
yield $key => static fn (): Response => $connector->send($request); | ||
} elseif (\is_callable($request)) { | ||
yield $key => static fn (): Response => $request($connector); | ||
} else { | ||
throw new InvalidPoolRequestException(Request::class, Response::class); | ||
} | ||
} | ||
}; | ||
|
||
return $this->getRunner($this->client)->run($promises($this->connector, $requests)); | ||
} | ||
} |
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.