Skip to content

Commit

Permalink
🔨 determine the existing ✨ asynchronous mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Oct 17, 2024
1 parent 7c495e5 commit d3a997d
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 465 deletions.
60 changes: 30 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
</p>
<p align="center">
<a href="#"><img src="https://img.shields.io/badge/PHP-%3E%3D%208.1-blue" alt="Build Status"></a>
<a href="https://packagist.org/packages/cloudtay/ripple"><img src="https://img.shields.io/packagist/dt/cloudtay/ripple" alt="Download statistics"></a>
<a href="https://packagist.org/packages/cloudtay/ripple"><img src="https://img.shields.io/packagist/v/cloudtay/ripple" alt="Stable version"></a>
<a href="https://packagist.org/packages/cloudtay/ripple"><img src="https://img.shields.io/packagist/dt/cloudtay/ripple" alt="Download statistics"> </a>
<a href="https://packagist.org/packages/cloudtay/ripple"><img src="https://img.shields.io/packagist/v/cloudtay/ripple" alt="Stable version"> </a>
<a href="https://packagist.org/packages/cloudtay/ripple"><img src="https://img.shields.io/packagist/l/cloudtay/ripple" alt="License"></a>
</p>
<p>
ripple is a modern, high-performance native PHP coroutine engine designed to solve PHP's challenges in high concurrency, complex network communication and data operations.
Ripple is a modern, high-performance native PHP coroutine engine designed to solve PHP's challenges in high concurrency, complex network communication and data operations.
The engine uses an innovative architecture and efficient programming model to provide powerful and flexible backend support for modern web and web applications.
By using ripple, you will experience the advantages of managing tasks from a global view of the system and efficiently handling network traffic and data. </p>

## Design Philosophy

Extreme performance is not our leading direction

It is the `Event` mechanism that gives PHP rocket-like performance, and we provide best practices for `Event`

With the introduction of `PHP8`, the more lightweight `Fiber` replaces the `Generator` coroutine model,

The `EventLoop` mechanism gives PHP rocket-like performance, and we provide best practices for `Event`
With the introduction of `PHP8`, the more lightweight `Fiber` replaces the `Generator` coroutine model.
Our design concept can be realized through PHP bootstrapping. At the same time, we use `revolt` as the underlying driver
library of ripple, making ripple perfectly compatible with the original PHP ecosystem.

Completely free the hands of PHPer and seamlessly embrace the new era of PHP coroutines

### 🌟 群聊已开放加入~ 🌟
Expand All @@ -41,20 +36,20 @@ Completely free the hands of PHPer and seamlessly embrace the new era of PHP cor
composer require cloudtay/ripple
````

## Basic usage
## Latest documentation

ripple strictly follows the latest strongly typed programming standards and is very friendly to IDEs
The following reproduction process is perfectly supported and explained in any IDE
You can visit `ripple`’s [documentation](https://ripple.cloudtay.com/) to start reading

### Latest documentation
We recommend that you start with [Manual Installation](https://ripple.cloudtay.com/docs/install/professional) to better
understand the workflow of ripple

You can visit ripple’s [Documentation](https://ripple.cloudtay.com/) to start reading
If you want to quickly deploy and use `ripple` services, you can directly
visit [Quick Deployment](https://ripple.cloudtay.com/docs/install/server)

We recommend that you start with [Manual Installation](https://ripple.cloudtay.com/docs/install/professional) to
better understand ripple’s workflow
## Basic usage

If you want to quickly deploy and use ripple's services, you can directly
visit [Quick Deployment](https://ripple.cloudtay.com/docs/install/server)
ripple strictly follows the latest strongly typed programming standards and is very IDE-friendly
The following reproduction process is perfectly supported and explained in any IDE

### Coroutine

Expand Down Expand Up @@ -205,7 +200,7 @@ Co\wait();
> Want to know about WebSocket server and client, TCP server and client, UDP server and client, Unix server and client,
> etc...
You can visit ripple’s [Documentation](https://ripple.cloudtay.com/) to start reading
You can visit ripple’s [documentation](https://ripple.cloudtay.com/) to start reading

## Appendix

Expand All @@ -217,25 +212,26 @@ You can visit ripple’s [Documentation](https://ripple.cloudtay.com/) to start
**🚀 [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)**
**🚀[Driver](https://github.com/cloudtay/ripple-driver)**
The official high-performance driver library provides seamless access to your traditional applications.

**🚀 [webman-coroutine](https://github.com/workbunny/webman-coroutine)**
**🚀[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

| Extension Types | Recommended Use | Compatibility | Description |
|:---------------:|:---------------:|:-------------:|:--------------------------------------------------------------------------------------------------------------------:|
| `libev` | 🏅️ | 🟢️ | `Ev` is a more efficient event extension that performs consistently in various systems and is recommended to be used |
| `Native` || 🟢 | Support the use of PHP's built-in select mechanism |
| `event` | | 🌗 | The event characteristics under different systems are not uniform, and their use is not recommended |
| Extension Types | Recommended Use | Compatibility | Description |
|:----------------------:|:---------------:|:-------------:|:-------------------------------------------------------------------------------------------------:|
| `libev` | 🏅️ | 🟢️ | `Ev` is a more efficient event extension that performs consistently in various systems and is |
| recommended to be used | | | |
| `Native` || 🟢 | Support the use of PHP's built-in select mechanism |
| `event` | | 🌗 | The event characteristics under different systems are not uniform, and its use is not recommended |

### Ev extension installation

Expand All @@ -246,11 +242,15 @@ pecl install ev
## Special thanks

<a href="https://www.jetbrains.com/?from=ripple" target="__blank">
<img src="https://www.jetbrains.com/company/brand/img/jetbrains_logo.png" width="200">
<img src="https://www.jetbrains.com/company/brand/img/jetbrains_logo.png" width="200" alt="jetbrains">
</a>

[Jetbrains](https://www.jetbrains.com/?from=ripple) provides free development tools for this project

### Contact information

`Email` [email protected]

`WeChat` jingnigg

---
27 changes: 11 additions & 16 deletions README.zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ ripple是一个现代化的、高性能的原生PHP协程引擎, 旨在解决PHP

## 设计哲学

极致的性能不是我们的主导方向

`Event`机制赋予了PHP火箭般的性能, 我们则为`Event`提供了最佳实践规范

随着`PHP8`引入更轻量级的`Fiber`取代了`Generator`的协程模型,

我们的设计理念得以通过PHP自举的方式实现,同时我们使用了`revolt`作为ripple的底层驱动库, 使得ripple完美兼容原有的PHP生态

`EventLoop`机制赋予了PHP火箭般的性能, 我们则为`Event`提供了最佳实践规范
随着`PHP8`引入更轻量级的`Fiber`取代了`Generator`的协程模型
我们的设计理念得以通过PHP自举的方式实现,同时我们使用了`revolt`作为ripple的底层驱动库, 使得ripple完美兼容原有的PHP生态
彻底解放PHPer的双手, 无缝拥抱全新的PHP协程时代

### 🌟 群聊已开放加入~ 🌟
Expand All @@ -40,18 +35,18 @@ ripple是一个现代化的、高性能的原生PHP协程引擎, 旨在解决PHP
composer require cloudtay/ripple
````

## 基础用法
## 最新文档

ripple严格遵循最新强类型的编程规范, 对IDE非常友好
下述的复现过程在任何IDE中都能得到完美的支持和解释
你可以访问`ripple`[文档](https://ripple.cloudtay.com/)开始阅读

### 最新文档
我们建议你从[手动安装](https://ripple.cloudtay.com/docs/install/professional)开始, 便于更好地理解ripple的工作流程

你可以访问ripple的[文档](https://ripple.cloudtay.com/)开始阅读
如果你想快速部署并使用`ripple`的服务, 你可以直接访问[快速部署](https://ripple.cloudtay.com/docs/install/server)

我们建议你从[手动安装](https://ripple.cloudtay.com/docs/install/professional)开始, 便于更好地理解ripple的工作流程
## 基础用法

如果你想快速部署并使用ripple的服务, 你可以直接访问[快速部署](https://ripple.cloudtay.com/docs/install/server)
ripple严格遵循最新强类型的编程规范, 对IDE非常友好
下述的复现过程在任何IDE中都能得到完美的支持和解释

### 协程

Expand Down Expand Up @@ -239,7 +234,7 @@ pecl install ev
## 特别致谢

<a href="https://www.jetbrains.com/?from=ripple" target="__blank">
<img src="https://www.jetbrains.com/company/brand/img/jetbrains_logo.png" width="200">
<img src="https://www.jetbrains.com/company/brand/img/jetbrains_logo.png" width="200" alt="jetbrains">
</a>

[Jetbrains](https://www.jetbrains.com/?from=ripple) 为本项目提供了免费的开发工具
Expand Down
16 changes: 0 additions & 16 deletions ripple.iml

This file was deleted.

44 changes: 13 additions & 31 deletions src/Core/Coroutine/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

namespace Psc\Core\Coroutine;

use BadFunctionCallException;
use Closure;
use Fiber;
use FiberError;
Expand Down Expand Up @@ -170,18 +169,13 @@ public function await(Promise $promise): mixed
* To determine your own control over preparing Fiber, you must be responsible for the subsequent status of Fiber.
*/
// When the status of the awaited Promise is completed
$promise->then(
static function (mixed $result) use ($suspension) {
// Try to resume Fiber operation
Coroutine::resume($suspension, $result);
},
static function (mixed $result) use ($suspension) {
// Try to notice Fiber: An exception occurred in the awaited Promise
$promise
->then(static fn (mixed $result) => Coroutine::resume($suspension, $result))
->except(static function (mixed $result) use ($suspension) {
$result instanceof Throwable
? $suspension->throw($result)
: $suspension->throw(new PromiseRejectException($result));
}
);
});

// Confirm that you have prepared to handle Fiber recovery and take over control of Fiber by suspending it
$result = Coroutine::suspend($suspension);
Expand Down Expand Up @@ -244,26 +238,14 @@ public function async(Closure $closure): Promise
/**
* @param int|float $second
*
* @return int
* @return int|float
* @throws Throwable
*/
public function sleep(int|float $second): int
public function sleep(int|float $second): int|float
{
$suspension = getSuspension();
if (!$suspension instanceof Suspension) {
delay(static fn () => Coroutine::resume($suspension, 0), $second);
return Coroutine::suspend($suspension);
} else {
delay(static function () use ($suspension, $second) {
Coroutine::resume($suspension, 0);
}, $second);

try {
return Coroutine::suspend($suspension);
} catch (Throwable $e) {
throw new BadFunctionCallException("An unexpected error occurred: {$e->getMessage()}");
}
}
delay(static fn () => Coroutine::resume($suspension, $second), $second);
return Coroutine::suspend($suspension);
}

/**
Expand All @@ -289,6 +271,9 @@ public function getContainer(): Container
}

/**
*
* The coroutine that cannot be restored can only throw an exception.
* If it is a ripple type exception, it will be caught and the contract will be rejected.
*
* This method attempts to resume a suspended coroutine and take over the coroutine context.
* When the recovery fails or an exception occurs within the coroutine, an exception will be thrown.
Expand Down Expand Up @@ -336,12 +321,9 @@ public static function suspend(EventLoop\Suspension $suspension): mixed
return $suspension->suspend();
} catch (EscapeException $exception) {
Coroutine::getInstance()->handleEscapeException($exception);
} catch (FiberError $exception) {
throw $exception;
} catch (Throwable $exception) {
$suspension instanceof Suspension || throw $exception;
$suspension->reject($exception);
$suspension instanceof Suspension && $suspension->reject($exception);
throw $exception;
}
return null;
}
}
22 changes: 8 additions & 14 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
use Co\System;
use Psc\Core\Coroutine\Promise;
use Psc\Core\Coroutine\Suspension;
use Psc\Utils\Output;
use Revolt\EventLoop;
use Revolt\EventLoop\UnsupportedFeatureException;
use Symfony\Component\DependencyInjection\Container;
Expand Down Expand Up @@ -80,7 +79,7 @@ class Kernel
private bool $processControl;

/*** @var bool */
private bool $running = true;
private bool $mainRunning = true;

/*** @var Container */
private Container $container;
Expand Down Expand Up @@ -166,11 +165,7 @@ public function promise(Closure $closure): Promise
public function delay(Closure $closure, int|float $second): string
{
return EventLoop::delay($second, static function () use ($closure) {
try {
$closure();
} catch (Throwable $exception) {
Output::exception($exception);
}
async($closure);
});
}

Expand Down Expand Up @@ -257,25 +252,24 @@ public function wait(Closure|null $result = null): bool
$this->mainSuspension = getSuspension();
}

if (!$this->running) {
Core\Coroutine\Coroutine::resume($this->mainSuspension, $result);
if (!$this->mainRunning) {
try {
Core\Coroutine\Coroutine::suspend(getSuspension());
Core\Coroutine\Coroutine::resume($this->mainSuspension, $result);
} catch (Throwable) {
exit(1);
}
}

try {
$this->running = false;
$result = Core\Coroutine\Coroutine::suspend($this->mainSuspension);
$this->running = true;
$this->mainRunning = false;
$result = Core\Coroutine\Coroutine::suspend($this->mainSuspension);
$this->mainRunning = true;
if ($result instanceof Closure) {
$result();
}

/**
* The Event object may be reset during the running of $result, so mainSuspension needs to be reacquired.
* The Event object may be reset during the mainRunning of $result, so mainSuspension needs to be reacquired.
*/
$this->mainSuspension = getSuspension();
return $this->wait();
Expand Down
Loading

0 comments on commit d3a997d

Please sign in to comment.