Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Mar 31, 2024
1 parent 82398d0 commit 3df87a0
Show file tree
Hide file tree
Showing 73 changed files with 615 additions and 500 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ compare-benchmark-to-reference:
./vendor/bin/phpbench run --config config/phpbench.json --ref=benchmark_reference

static-analysis: ## run static analysis checks
./vendor/bin/psalm -c config/psalm.xml --show-info=true --no-cache --threads=1
./vendor/bin/psalm -c config/psalm.xml tests/static-analysis --no-cache --threads=1
./vendor/bin/psalm -c config/psalm.xml --show-info=true --no-cache --threads=2
./vendor/bin/psalm -c config/psalm.xml tests/static-analysis --no-cache --threads=2

type-coverage: ## send static analysis type coverage metrics to https://shepherd.dev/
./vendor/bin/psalm -c config/psalm.xml --shepherd --stats --threads=1
Expand Down
6 changes: 3 additions & 3 deletions docs/component/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
#### `Classes`

- [Lock](./../../src/Psl/File/Lock.php#L9)
- [ReadHandle](./../../src/Psl/File/ReadHandle.php#L10)
- [ReadWriteHandle](./../../src/Psl/File/ReadWriteHandle.php#L11)
- [WriteHandle](./../../src/Psl/File/WriteHandle.php#L11)
- [ReadHandle](./../../src/Psl/File/ReadHandle.php#L11)
- [ReadWriteHandle](./../../src/Psl/File/ReadWriteHandle.php#L12)
- [WriteHandle](./../../src/Psl/File/WriteHandle.php#L12)

#### `Enums`

Expand Down
38 changes: 19 additions & 19 deletions docs/component/io.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- [input_handle](./../../src/Psl/IO/input_handle.php#L20)
- [output_handle](./../../src/Psl/IO/output_handle.php#L20)
- [pipe](./../../src/Psl/IO/pipe.php#L24)
- [streaming](./../../src/Psl/IO/streaming.php#L38)
- [streaming](./../../src/Psl/IO/streaming.php#L41)
- [write](./../../src/Psl/IO/write.php#L21)
- [write_error](./../../src/Psl/IO/write_error.php#L23)
- [write_error_line](./../../src/Psl/IO/write_error_line.php#L23)
Expand All @@ -41,7 +41,7 @@
- [CloseWriteHandleInterface](./../../src/Psl/IO/CloseWriteHandleInterface.php#L7)
- [CloseWriteStreamHandleInterface](./../../src/Psl/IO/CloseWriteStreamHandleInterface.php#L9)
- [HandleInterface](./../../src/Psl/IO/HandleInterface.php#L21)
- [ReadHandleInterface](./../../src/Psl/IO/ReadHandleInterface.php#L10)
- [ReadHandleInterface](./../../src/Psl/IO/ReadHandleInterface.php#L12)
- [ReadStreamHandleInterface](./../../src/Psl/IO/ReadStreamHandleInterface.php#L9)
- [ReadWriteHandleInterface](./../../src/Psl/IO/ReadWriteHandleInterface.php#L7)
- [ReadWriteStreamHandleInterface](./../../src/Psl/IO/ReadWriteStreamHandleInterface.php#L9)
Expand All @@ -54,32 +54,32 @@
- [SeekWriteHandleInterface](./../../src/Psl/IO/SeekWriteHandleInterface.php#L7)
- [SeekWriteStreamHandleInterface](./../../src/Psl/IO/SeekWriteStreamHandleInterface.php#L9)
- [StreamHandleInterface](./../../src/Psl/IO/StreamHandleInterface.php#L9)
- [WriteHandleInterface](./../../src/Psl/IO/WriteHandleInterface.php#L10)
- [WriteHandleInterface](./../../src/Psl/IO/WriteHandleInterface.php#L12)
- [WriteStreamHandleInterface](./../../src/Psl/IO/WriteStreamHandleInterface.php#L9)

#### `Classes`

- [CloseReadStreamHandle](./../../src/Psl/IO/CloseReadStreamHandle.php#L12)
- [CloseReadWriteStreamHandle](./../../src/Psl/IO/CloseReadWriteStreamHandle.php#L12)
- [CloseSeekReadStreamHandle](./../../src/Psl/IO/CloseSeekReadStreamHandle.php#L12)
- [CloseSeekReadWriteStreamHandle](./../../src/Psl/IO/CloseSeekReadWriteStreamHandle.php#L12)
- [CloseReadStreamHandle](./../../src/Psl/IO/CloseReadStreamHandle.php#L13)
- [CloseReadWriteStreamHandle](./../../src/Psl/IO/CloseReadWriteStreamHandle.php#L13)
- [CloseSeekReadStreamHandle](./../../src/Psl/IO/CloseSeekReadStreamHandle.php#L13)
- [CloseSeekReadWriteStreamHandle](./../../src/Psl/IO/CloseSeekReadWriteStreamHandle.php#L13)
- [CloseSeekStreamHandle](./../../src/Psl/IO/CloseSeekStreamHandle.php#L10)
- [CloseSeekWriteStreamHandle](./../../src/Psl/IO/CloseSeekWriteStreamHandle.php#L12)
- [CloseSeekWriteStreamHandle](./../../src/Psl/IO/CloseSeekWriteStreamHandle.php#L13)
- [CloseStreamHandle](./../../src/Psl/IO/CloseStreamHandle.php#L10)
- [CloseWriteStreamHandle](./../../src/Psl/IO/CloseWriteStreamHandle.php#L12)
- [MemoryHandle](./../../src/Psl/IO/MemoryHandle.php#L13)
- [ReadStreamHandle](./../../src/Psl/IO/ReadStreamHandle.php#L12)
- [ReadWriteStreamHandle](./../../src/Psl/IO/ReadWriteStreamHandle.php#L12)
- [Reader](./../../src/Psl/IO/Reader.php#L16)
- [SeekReadStreamHandle](./../../src/Psl/IO/SeekReadStreamHandle.php#L12)
- [SeekReadWriteStreamHandle](./../../src/Psl/IO/SeekReadWriteStreamHandle.php#L12)
- [CloseWriteStreamHandle](./../../src/Psl/IO/CloseWriteStreamHandle.php#L13)
- [MemoryHandle](./../../src/Psl/IO/MemoryHandle.php#L14)
- [ReadStreamHandle](./../../src/Psl/IO/ReadStreamHandle.php#L13)
- [ReadWriteStreamHandle](./../../src/Psl/IO/ReadWriteStreamHandle.php#L13)
- [Reader](./../../src/Psl/IO/Reader.php#L17)
- [SeekReadStreamHandle](./../../src/Psl/IO/SeekReadStreamHandle.php#L13)
- [SeekReadWriteStreamHandle](./../../src/Psl/IO/SeekReadWriteStreamHandle.php#L13)
- [SeekStreamHandle](./../../src/Psl/IO/SeekStreamHandle.php#L10)
- [SeekWriteStreamHandle](./../../src/Psl/IO/SeekWriteStreamHandle.php#L12)
- [WriteStreamHandle](./../../src/Psl/IO/WriteStreamHandle.php#L12)
- [SeekWriteStreamHandle](./../../src/Psl/IO/SeekWriteStreamHandle.php#L13)
- [WriteStreamHandle](./../../src/Psl/IO/WriteStreamHandle.php#L13)

#### `Traits`

- [ReadHandleConvenienceMethodsTrait](./../../src/Psl/IO/ReadHandleConvenienceMethodsTrait.php#L15)
- [WriteHandleConvenienceMethodsTrait](./../../src/Psl/IO/WriteHandleConvenienceMethodsTrait.php#L16)
- [ReadHandleConvenienceMethodsTrait](./../../src/Psl/IO/ReadHandleConvenienceMethodsTrait.php#L16)
- [WriteHandleConvenienceMethodsTrait](./../../src/Psl/IO/WriteHandleConvenienceMethodsTrait.php#L17)


2 changes: 1 addition & 1 deletion docs/component/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#### `Functions`

- [execute](./../../src/Psl/Shell/execute.php#L41)
- [execute](./../../src/Psl/Shell/execute.php#L42)
- [stream_unpack](./../../src/Psl/Shell/stream_unpack.php#L30)
- [unpack](./../../src/Psl/Shell/unpack.php#L16)

Expand Down
2 changes: 1 addition & 1 deletion docs/component/tcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#### `Functions`

- [connect](./../../src/Psl/TCP/connect.php#L18)
- [connect](./../../src/Psl/TCP/connect.php#L19)

#### `Classes`

Expand Down
2 changes: 1 addition & 1 deletion docs/component/unix.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#### `Functions`

- [connect](./../../src/Psl/Unix/connect.php#L18)
- [connect](./../../src/Psl/Unix/connect.php#L19)

#### `Classes`

Expand Down
4 changes: 2 additions & 2 deletions examples/async/usleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
require __DIR__ . '/../../vendor/autoload.php';

Async\main(static function (): int {
$start = DateTime\Timestamp::now();
$start = DateTime\Timestamp::monotonic();

Async\concurrently([
static fn() => Async\sleep(DateTime\Duration::hours(0)),
Expand All @@ -23,7 +23,7 @@
static fn() => Async\sleep(DateTime\Duration::milliseconds(2000)),
]);

$duration = DateTime\Timestamp::now()->since($start);
$duration = DateTime\Timestamp::monotonic()->since($start);

IO\write_error_line("duration : %s.", $duration->toString(max_decimals: 5));

Expand Down
4 changes: 2 additions & 2 deletions examples/io/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

Async\Scheduler::delay($seconds, static fn() => $input->close());

$start = DateTime\Timestamp::now();
$start = DateTime\Timestamp::monotonic();
$i = 0;
try {
while ($chunk = $input->read(65536)) {
Expand All @@ -53,7 +53,7 @@
} catch (IO\Exception\AlreadyClosedException) {
}

$duration = DateTime\Timestamp::now()->since($start);
$duration = DateTime\Timestamp::monotonic()->since($start);
$bytes = $i * 65536;
$bytes_formatted = Math\round($bytes / 1024 / 1024 / $duration->getTotalSeconds(), 1);

Expand Down
6 changes: 3 additions & 3 deletions examples/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
IO\write_error_line('- %s/%s -> started', $component, $script);

$awaitables[] = Async\run(static function() use($component, $script, $file): array {
$start = DateTime\Timestamp::now();
$start = DateTime\Timestamp::monotonic();
Shell\execute(PHP_BINARY, [$file]);
$duration = DateTime\Timestamp::now()->since($start);
$duration = DateTime\Timestamp::monotonic()->since($start);

return [$component, $script, $duration];
});
Expand All @@ -47,7 +47,7 @@
foreach (Async\Awaitable::iterate($awaitables) as $awaitable) {
[$component, $script, $duration] = $awaitable->await();

IO\write_error_line('+ %s/%s -> finished in %s', $component, $script, $duration);
IO\write_error_line('+ %s/%s -> finished in %s', $component, $script, $duration->toString(10));
}

return 0;
Expand Down
15 changes: 14 additions & 1 deletion sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@
require 'vendor/autoload.php';

Async\main(static function(): void {
$a = DateTime\Timestamp::now();
$b = DateTime\Timestamp::now();

var_dump($b->since($a)->toString(20));

$a = DateTime\Timestamp::monotonic();
$b = DateTime\Timestamp::monotonic();

var_dump($b->since($a)->toString(20));

$someday = DateTime\DateTime::fromTimestamp(
DateTime\Timestamp::fromRaw(1711846900),
DateTime\Timezone::EuropeLondon,
DateTime\Timestamp::fromRaw(1711846900),
);

var_dump($someday->format(DateTime\DatePattern::Http->value));
die();

IO\write_line('The offset of the timezone: %s', $someday->getTimezone()->getOffset($someday)->getTotalMinutes());
IO\write_line('The raw offset of the timezone: %s', $someday->getTimezone()->getRawOffset()->getTotalMinutes());
IO\write_line('The dst savings of the timezone: %s', $someday->getTimezone()->getDaylightSavingTimeSavings()->getTotalMinutes());
Expand Down
38 changes: 25 additions & 13 deletions src/Psl/Async/OptionalIncrementalTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Psl\Async;

use Closure;

use function microtime;
use Psl\DateTime\Duration;
use Psl\DateTime\Timestamp;

/**
* Manages optional incremental timeouts for asynchronous operations.
Expand All @@ -16,47 +16,59 @@
* particularly useful in asynchronous programming where operations
* might need to be interrupted or handled differently if they take
* too long to complete.
*
* @psalm-suppress MissingThrowsDocblock
*/
final class OptionalIncrementalTimeout
{
/**
* @var ?float The end time in microseconds.
* @var ?Timestamp The end time.
*/
private ?float $end;
private ?Timestamp $end;

/**
* @var (Closure(): ?float) The handler to be called upon timeout.
* @var (Closure(): ?Duration) The handler to be called upon timeout.
*/
private Closure $handler;

/**
* @param float|null $timeout The timeout duration in seconds. Null to disable timeout.
* @param (Closure(): ?float) $handler The handler to be executed if the timeout is reached.
* @param null|Duration $timeout The timeout duration. Null to disable timeout.
* @param (Closure(): ?Duration) $handler The handler to be executed if the timeout is reached.
*/
public function __construct(?float $timeout, Closure $handler)
public function __construct(?Duration $timeout, Closure $handler)
{
$this->handler = $handler;

$this->end = $timeout !== null ? (microtime(true) + $timeout) : null;
if (null === $timeout) {
$this->end = null;

return;
}

if (!$timeout->isPositive()) {
$this->end = Timestamp::monotonic();
} else {
$this->end = Timestamp::monotonic()->plus($timeout);
}
}

/**
* Retrieves the remaining time until the timeout is reached, or null if no timeout is set.
*
* If the timeout has already been exceeded, the handler is invoked, and its return value is provided.
*
* @return float|null The remaining time in seconds, null if no timeout is set, or the handler's return value if the timeout is exceeded.
* @return Duration|null The remaining time duration, null if no timeout is set, or the handler's return value if the timeout is exceeded.
*
* @external-mutation-free
*/
public function getRemaining(): ?float
public function getRemaining(): ?Duration
{
if ($this->end === null) {
return null;
}

$remaining = $this->end - microtime(true);
$remaining = $this->end->since(Timestamp::monotonic());

return $remaining <= 0 ? ($this->handler)() : $remaining;
return $remaining->isPositive() ? $remaining : ($this->handler)();
}
}
19 changes: 6 additions & 13 deletions src/Psl/Async/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,40 +115,33 @@ public static function defer(Closure $callback): string
/**
* Delay the execution of a callback.
*
* @param DateTime\Duration|float $delay The amount of time, to delay the execution for in seconds.
* @param DateTime\Duration $delay The amount of time, to delay the execution for in seconds.
* @param Closure(string): void $callback The callback to delay.
*
* @return non-empty-string A unique identifier that can be used to cancel, enable or disable the callback.
*
* @see EventLoop::delay()
*/
public static function delay(DateTime\Duration|float $delay, Closure $callback): string
public static function delay(DateTime\Duration $delay, Closure $callback): string
{
if ($delay instanceof DateTime\Duration) {
$delay = $delay->getTotalSeconds();
}

/** @var non-empty-string */
return EventLoop::delay($delay, $callback);
return EventLoop::delay($delay->getTotalSeconds(), $callback);
}

/**
* Repeatedly execute a callback.
*
* @param DateTime\Duration|float $interval The time interval, to wait between executions in seconds.
* @param DateTime\Duration $interval The time interval, to wait between executions in seconds.
* @param Closure(string): void $callback The callback to repeat.
*
* @return non-empty-string A unique identifier that can be used to cancel, enable or disable the callback.
*
* @see EventLoop::repeat()
*/
public static function repeat(DateTime\Duration|float $interval, Closure $callback): string
public static function repeat(DateTime\Duration $interval, Closure $callback): string
{
if ($interval instanceof DateTime\Duration) {
$interval = $interval->getTotalSeconds();
}
/** @var non-empty-string */
return EventLoop::repeat($interval, $callback);
return EventLoop::repeat($interval->getTotalSeconds(), $callback);
}


Expand Down
11 changes: 5 additions & 6 deletions src/Psl/Async/sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
/**
* Non-blocking sleep for the specified number of seconds.
*/
function sleep(DateTime\Duration|float $seconds): void
function sleep(DateTime\Duration $duration): void
{
if ($seconds instanceof DateTime\Duration) {
$seconds = $seconds->getTotalSeconds();
}

$suspension = EventLoop::getSuspension();
$watcher = EventLoop::delay($seconds, static fn () => $suspension->resume());
$watcher = EventLoop::delay(
$duration->getTotalSeconds(),
static fn () => $suspension->resume(),
);

try {
$suspension->suspend();
Expand Down
3 changes: 2 additions & 1 deletion src/Psl/DateTime/DatePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ enum DatePattern: string
case Iso8601 = 'yyyy-MM-dd\'T\'HH:mm:ssXXX';
case Http = 'EEE, dd MMM yyyy HH:mm:ss zzz';
case Cookie = 'EEEE, dd-MMM-yyyy HH:mm:ss zzz';
case Sql = 'yyyy-MM-dd HH:mm:ss';
case SqlDate = 'yyyy-MM-dd';
case SqlDateTime = 'yyyy-MM-dd HH:mm:ss';
case XmlRpc = 'yyyyMMdd\'T\'HH:mm:ss';
case IsoWeekDate = 'Y-ww-E';
case IsoOrdinalDate = 'yyyy-DDD';
Expand Down

0 comments on commit 3df87a0

Please sign in to comment.