-
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.
* Fix generics * Refactor defer * Add amphp driver * Fix test case * Add @return doc for generic * Support keyed response for amp * Update readme * refactor AmpDeferred: move delay check and callback invocation inside Amp\async function * Add suggest * Separate client and connector test * Ignore coverage * Update test case * Ignore coverage * Merge driver discovery and async client factory test * Fix issue with incorrect driver * Test driver discovery * Revert test case * Ignore coverage for now * Update benchmark * Add benchmark workflow * Update benchmark workflow * Change bench command * Add generic docblock
- Loading branch information
Showing
36 changed files
with
480 additions
and
104 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
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fansipan\Peak\Concurrency; | ||
|
||
use Amp; | ||
|
||
final class AmpDeferred implements Deferrable | ||
{ | ||
public function defer(callable $callback, float $delay = 0): mixed | ||
{ | ||
return Amp\async(static function (callable $callback, float $delay) { | ||
if ($delay > 0) { | ||
Amp\delay($delay); | ||
} | ||
|
||
return $callback(); | ||
}, $callback, $delay)->await(); | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fansipan\Peak\Concurrency; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
final class AmpTask | ||
{ | ||
/** | ||
* @var T | ||
*/ | ||
private mixed $value = null; | ||
|
||
/** | ||
* @param \Closure(): T $task | ||
*/ | ||
public function __construct( | ||
private readonly string|int $key, | ||
private readonly \Closure $task | ||
) { | ||
} | ||
|
||
public function key(): string|int | ||
{ | ||
return $this->key; | ||
} | ||
|
||
/** | ||
* @return T | ||
*/ | ||
public function value(): mixed | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function __invoke(): self | ||
{ | ||
$this->value = ($this->task)(); | ||
|
||
return $this; | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fansipan\Peak\Concurrency; | ||
|
||
use Amp; | ||
use Amp\Future; | ||
use Amp\Pipeline\Pipeline; | ||
|
||
final class AmpWorker implements Worker | ||
{ | ||
/** | ||
* @param int<1, max> $limit | ||
*/ | ||
public function __construct(private readonly int $limit = 10) | ||
{ | ||
if ($limit < 1) { | ||
// @codeCoverageIgnoreStart | ||
throw new \ValueError('Argument #1 ($limit) must be positive, got '.$limit); | ||
// @codeCoverageIgnoreEnd | ||
} | ||
} | ||
|
||
public function run(iterable $tasks): array | ||
{ | ||
$promises = Pipeline::fromIterable(static function () use ($tasks): \Generator { | ||
foreach ($tasks as $key => $task) { | ||
yield new AmpTask($key, $task); | ||
} | ||
}) | ||
->concurrent($this->limit) | ||
->unordered() | ||
->map(static fn (AmpTask $task) => Amp\async(\Closure::fromCallable($task))); | ||
|
||
$results = []; | ||
|
||
foreach (Future::iterate($promises) as $promise) { | ||
try { | ||
/** @var AmpTask $t */ | ||
$t = $promise->await(); | ||
$results[$t->key()] = $t->value(); | ||
} catch (\Throwable) { | ||
} | ||
} | ||
|
||
return $results; | ||
} | ||
} |
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
Oops, something went wrong.