Skip to content

Commit

Permalink
Project: Update logo and .tarignore
Browse files Browse the repository at this point in the history
Update: stream::waitForxxable timeout supports float
Update: Refactor parallel && for Windows compatibility
Feat: Worker support metadata
  • Loading branch information
cclilshy committed Feb 8, 2025
1 parent b283daf commit 0302832
Show file tree
Hide file tree
Showing 32 changed files with 979 additions and 986 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ composer.lock
.php-cs-fixer.cache
.phpunit.cache
.phpunit.result.cache
.cursor
4 changes: 4 additions & 0 deletions .tarignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/assets/
/docs/
/examples/
/tests/
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="https://raw.githubusercontent.com/cloudtay/ripple/refs/heads/main/assets/images/logo.png" width="420" alt="Logo">
<img src="assets/images/logo.png" width="420" alt="Logo">
</p>
<p align="center">
<a href="#"><img src="https://img.shields.io/badge/PHP-%3E%3D%208.1-blue" alt="Build Status"></a>
Expand Down Expand Up @@ -35,19 +35,22 @@ visit [Quick Deployment](https://ripple.cloudtay.com/docs/install/server)
> We allow users to choose applicable component libraries by themselves. All components can be used as described in the
> document without additional configuration.
**🚀 [Guzzle](https://docs.guzzlephp.org/en/stable/)**
**🚀 [Guzzle](https://docs.guzzlephp.org/en/stable/)**
PHP is the most widely used HTTP client

**🔥 [AmPHP](https://amphp.org/)**
**🔥 [AmPHP](https://amphp.org/)**
Provides rich PHP asynchronous components for users to encapsulate by themselves

**🚀 [Driver](https://github.com/cloudtay/ripple-driver)**
**🚀 [Laravel-ripple](https://github.com/cloudtay/laravel-ripple)**
The official high-performance driver library provides seamless access to your traditional applications.

**🚀 [Webman-coroutine](https://github.com/workbunny/webman-coroutine)**
**🚀 [Workerman-ripple](https://github.com/cloudtay/workerman-ripple)**
The official high-performance driver library provides seamless access to your traditional applications.

**🚀 [Webman-coroutine](https://github.com/workbunny/webman-coroutine)**
The workbunny team's integrated webman coroutine extension provides coroutine support for Webman.

**🟢[ripple](https://github.com/cloudtay/ripple)**
**🟢 [ripple](https://github.com/cloudtay/ripple)**
Provides standard coroutine architecture and tools for rapid development or packaging of traditional applications

### Event Library Guide
Expand Down
31 changes: 17 additions & 14 deletions README.zh_cn.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="https://raw.githubusercontent.com/cloudtay/ripple/refs/heads/main/assets/images/logo.png" width="420" alt="Logo">
<img src="assets/images/logo.png" width="420" alt="Logo">
</p>
<p align="center">
<a href="#"><img src="https://img.shields.io/badge/PHP-%3E%3D%208.1-blue" alt="Build Status"></a>
Expand All @@ -18,9 +18,9 @@ ripple是一个现代化的、高性能的原生PHP协程引擎, 旨在解决PHP

**`🎉 加入方式`** 通过以下方式添加作者微信即可加入交流群

| 微信二维码 |
|----------------------------------------------------------------------------------------------------------------------|
| <img src="https://raw.githubusercontent.com/cloudtay/ripple/refs/heads/main/assets/images/wechat.jpg" width="380" /> |
| 微信二维码 |
|----------------------------------------------------|
| <img src="assets/images/wechat.jpg" width="380" /> |

## 安装

Expand All @@ -42,20 +42,23 @@ composer require cloudtay/ripple

> 我们允许用户自行选择适用的组件库, 所有组件只需像文档中描述的方式即可无需额外配置
**🚀 [Guzzle](https://docs.guzzlephp.org/en/stable/)**
PHP应用最为广泛的HTTP客户端
**🚀 [Guzzle]https://docs.guzzlephp.org/en/stable/**
PHP 是使用最广泛的 HTTP 客户端

**🔥 [AmPHP](https://amphp.org/)**
提供丰富的PHP异步组件供用户自行封装
**🔥 [AmPHP]https://amphp.org/**
提供丰富的 PHP 异步组件供用户自行封装

**🚀 [Driver](https://github.com/cloudtay/ripple-driver)**
官方提供的高性能驱动库,无缝接入你的传统应用
**🚀 [Laravel-ripple]https://github.com/cloudtay/laravel-ripple)**
官方高性能驱动程序库提供对传统应用程序的无缝访问。

**🚀 [Webman-coroutine](https://github.com/workbunny/webman-coroutine)**
workbunny团队体统的webman协程扩展, 为Webman提供了协程支持
**🚀 [Workerman-ripple]https://github.com/cloudtay/workerman-ripple)**
官方高性能驱动程序库提供对传统应用程序的无缝访问。

**🟢 [ripple](https://github.com/cloudtay/ripple)**
提供标准的协程架构与工具用于迅速开发或封装传统应用
**🚀 [webman-coroutine]https://github.com/workbunny/webman-coroutine)**
workbunny 团队的集成 webman 协程扩展为 Webman 提供协程支持。

**🟢[ripple]https://github.com/cloudtay/ripple)**
提供标准协程架构和工具,用于快速开发或打包传统应用程序

### 事件库指南

Expand Down
Binary file modified assets/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions example/parallel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);
/**
* Copyright © 2024 cclilshy
* Email: [email protected]
*
* This software is licensed under the MIT License.
* For full license details, please visit: https://opensource.org/licenses/MIT
*
* By using this software, you agree to the terms of the license.
* Contributions, suggestions, and feedback are always welcome!
*/

use Ripple\Parallel\Parallel;

use function Co\wait;

include 'vendor/autoload.php';

$parallel = Parallel::getInstance();
$function = function ($input) {
\sleep(1);
return $input;
};

$futures = [];
for ($i = 0; $i < 100; $i++) {
$futures[] = $future = $parallel->run($function, ['name']);
}

foreach ($futures as $future) {
echo \microtime(true) , ':', $future->value() , \PHP_EOL;
}

wait();
59 changes: 25 additions & 34 deletions example/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,23 @@

$manager = new Manager();
$worker = new class () extends Ripple\Worker\Worker {
/**
* @var string
*/
/*** @var string */
protected string $name = 'abc';

/**
* @var int
*/
/*** @var int */
protected int $count = 4;

/**
* @return void
*/
protected function boot(): void
/*** @return void */
public function boot(): void
{
\var_dump($this->getIndex());
\Co\sleep(10);
}

/**
* @param \Ripple\Worker\Manager $manager
*
* @return void
*/
protected function register(Manager $manager): void
public function register(Manager $manager): void
{
// TODO: Implement register() method.
}
Expand All @@ -46,31 +38,38 @@ protected function register(Manager $manager): void
*/
protected function onCommand(Command $command): void
{
\var_dump($command, $this->getIndex());
}
};

$worker2 = new class () extends Ripple\Worker\Worker {
/**
* @var string
*/
/*** @var string */
protected string $name = 'def';

/**
* @var int
*/
/*** @var int */
protected int $count = 4;

/**
* @return void
*/
protected function boot(): void
/*** @return void */
public function boot(): void
{
\Co\sleep(6);
$this->forwardCommand(Command::make('test'), 'abc', 2);
if ($this->getIndex() === 1) {
async(function () {
while (1) {
echo \json_encode($this->getManagerMateData()), \PHP_EOL;
\Co\sleep(1);
}
})->except(function () {
\var_dump('error');
});
}
}

protected function register(Manager $manager): void
/**
* @param \Ripple\Worker\Manager $manager
*
* @return void
*/
public function register(Manager $manager): void
{
// TODO: Implement register() method.
}
Expand All @@ -79,12 +78,4 @@ protected function register(Manager $manager): void
$manager->add($worker);
$manager->add($worker2);
$manager->run();

async(function () use ($manager) {
\Co\sleep(1);
$manager->sendCommand(Command::make('test'), 'abc', 2);

\Co\sleep(1);
$manager->reload();
});
wait();
118 changes: 4 additions & 114 deletions src/Coroutine/WaitGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,119 +12,9 @@

namespace Ripple\Coroutine;

use LogicException;
use Ripple\Utils\Output;
use RuntimeException;
use Throwable;

use function array_shift;
use function Co\cancel;
use function Co\delay;
use function Co\getSuspension;
use function spl_object_hash;

class WaitGroup
/**
* @deprecated use \Ripple\WaitGroup instead
*/
class WaitGroup extends \Ripple\WaitGroup
{
/*** @var bool */
protected bool $done = true;

/*** @var \Revolt\EventLoop\Suspension[] */
protected array $waiters = [];

/*** @var int */
protected int $count = 0;

/*** @param int $count */
public function __construct(int $count = 0)
{
$this->add($count);
}

/**
* @param int $delta
*
* @return void
*/
public function add(int $delta = 1): void
{
if ($delta > 0) {
$this->count += $delta;
$this->done = false;
} elseif ($delta < 0) {
throw new LogicException('delta must be greater than or equal to 0');
}

// For the case where $delta is 0, no operation is performed
}

/**
* @return void
*/
public function done(): void
{
if ($this->count <= 0) {
throw new LogicException('No tasks to mark as done');
}

$this->count--;
if ($this->count === 0) {
$this->done = true;
while ($suspension = array_shift($this->waiters)) {
try {
Coroutine::resume($suspension);
} catch (Throwable $exception) {
Output::warning($exception->getMessage());
continue;
}
}
}
}

/**
* @param int|float $timeout
*
* @return void
*/
public function wait(int|float $timeout = 0): void
{
if ($this->done) {
return;
}

$suspension = getSuspension();
$this->waiters[$hash = spl_object_hash($suspension)] = $suspension;

if ($timeout > 0) {
$timeoutOID = delay(function () use ($hash) {
$suspension = $this->waiters[$hash];
unset($this->waiters[$hash]);
Coroutine::throw($suspension, new RuntimeException('WaitGroup timeout'));
}, $timeout);
}

try {
Coroutine::suspend($suspension);
if (isset($timeoutOID)) {
cancel($timeoutOID);
}
} catch (Throwable $exception) {
throw new RuntimeException($exception->getMessage(), $exception->getCode());
}
}

/**
* @return int
*/
public function getCount(): int
{
return $this->count;
}

/**
* @return bool
*/
public function isDone(): bool
{
return $this->done;
}
}
12 changes: 11 additions & 1 deletion src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
use function intval;
use function preg_match;
use function shell_exec;
use function getmygid;
use function posix_getpid;

use const PHP_OS_FAMILY;

Expand Down Expand Up @@ -327,6 +329,14 @@ public function getMemorySize(): int
break;
}

return $this->memorySize = 0;
return $this->memorySize = -1;
}

/**
* @return int
*/
public function getProcessId(): int
{
return $this->supportProcessControl() ? posix_getpid() : getmygid();
}
}
Loading

0 comments on commit 0302832

Please sign in to comment.