Skip to content

Commit e5e8eb8

Browse files
committed
refactor: restructure scheduler result handling and Outcome class
1 parent 851eca0 commit e5e8eb8

11 files changed

Lines changed: 98 additions & 72 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.idea
22
ripple-kernel.iml
33
/temp/
4-
4+
/docs/
55
/debug/
66
/logs/
77
/vendor/

src/Net/Http/Server/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ private function onRequest(array $reqInfo): void
255255

256256
try {
257257
// call_user_func($this->httpServer->onRequest, $req);
258-
Scheduler::resume($this->httpServer->acquireCoroutine(), $req)->throw();
258+
Scheduler::resume($this->httpServer->acquireCoroutine(), $req)->rethrow();
259259
} catch (ConnectionException $exception) {
260260
throw $exception;
261261
} catch (Throwable $exception) {

src/Process.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private static function dispatchExit(int $pid, int $exitCode): void
262262
$hasSubscribers = !empty(self::$watchers[$pid]);
263263
if ($hasSubscribers) {
264264
foreach (self::$watchers[$pid] as $coroutine) {
265-
Scheduler::resume($coroutine, $exitCode)->resolve(CoroutineStateException::class);
265+
Scheduler::resume($coroutine, $exitCode)->unwrap(CoroutineStateException::class);
266266
}
267267
unset(self::$watchers[$pid]);
268268
} else {

src/Runtime/HotCoroutinePool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function clear(): void
9696
{
9797
while (!$this->isEmpty()) {
9898
$coroutine = $this->acquire();
99-
Scheduler::terminate($coroutine)->resolve();
99+
Scheduler::terminate($coroutine)->unwrap();
100100
}
101101

102102
$this->coroutines = new SplQueue();

src/Runtime/Scheduler.php

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Ripple\Runtime\Exception\CoroutineStateException;
2020
use Ripple\Runtime\Exception\TerminateException;
2121
use Ripple\Coroutine;
22-
use Ripple\Runtime\Scheduler\ControlResult;
22+
use Ripple\Runtime\Scheduler\Outcome;
2323
use Ripple\Runtime\Support\Display;
2424
use Ripple\Runtime\Support\Stdin;
2525
use SplObjectStorage;
@@ -51,9 +51,9 @@ final class Scheduler
5151

5252
/**
5353
* 异常控制结果队列
54-
* @var ControlResult[]
54+
* @var Outcome[]
5555
*/
56-
private static array $excepts = [];
56+
private static array $unresolvedErrors = [];
5757

5858
/**
5959
* 下一个事件循环中需要执行的回调队列
@@ -164,25 +164,29 @@ public static function dispatcher(): void
164164
}
165165
}
166166

167-
while ($report = array_shift(self::$excepts)) {
168-
if ($report->isResolve()) {
167+
$unresolve = false;
168+
while ($report = array_shift(self::$unresolvedErrors)) {
169+
if ($report->isResolved()) {
169170
continue;
170171
}
171172

172-
$previous = $report->exception();
173+
$unresolve = true;
174+
$exception = $report->exception();
173175

174-
Stdin::println("When self::{$report->action()} is executed, the internal Coroutine throws an unresolved exception:");
175-
Stdin::print(Display::exception($previous));
176+
Stdin::println("When Scheduler::{$report->action()} is executed, the internal Coroutine throws an unresolved exception:");
177+
Stdin::print(Display::exception($exception));
176178

177-
Stdin::println(str_repeat('-', 60));
178-
179-
Stdin::println("An uncaught exception occurs within the coroutine:");
180-
Stdin::print(Display::traces($report->coroutine()->debugTrace()));
179+
if ($coroutine = $report->coroutine()) {
180+
Stdin::println(str_repeat('-', 60));
181+
Stdin::println("An uncaught exception occurs within the coroutine:");
182+
Stdin::print(Display::traces($coroutine->debugTrace()));
183+
}
181184

182185
Stdin::println(str_repeat('-', 60));
183-
184-
Stdin::print(Display::trace($report->debugTrace()));
186+
Stdin::print(Display::trace($report->trace()));
185187
}
188+
189+
$unresolve && exit(1);
186190
}
187191

188192
/**
@@ -227,12 +231,12 @@ public static function remove(object $key): void
227231

228232
/**
229233
* 报告异常控制结果
230-
* @param ControlResult $controlException 异常控制结果
231-
* @return ControlResult 返回传入的异常控制结果
234+
* @param Outcome $controlException 异常控制结果
235+
* @return Outcome 返回传入的异常控制结果
232236
*/
233-
public static function reportException(ControlResult $controlException): ControlResult
237+
public static function auditFailure(Outcome $controlException): Outcome
234238
{
235-
self::$excepts[] = $controlException;
239+
self::$unresolvedErrors[] = $controlException;
236240
return $controlException;
237241
}
238242

@@ -286,7 +290,7 @@ public static function start(Coroutine $coroutine): void
286290
try {
287291
$coroutine->start();
288292
} catch (Throwable $exception) {
289-
new ControlResult('start', false, $coroutine, $exception);
293+
new Outcome('start', false, $coroutine, $exception);
290294
} finally {
291295
if ($coroutine->isTerminated()) {
292296
Scheduler::remove($coroutine->key());
@@ -299,14 +303,14 @@ public static function start(Coroutine $coroutine): void
299303
* 恢复协程
300304
* @param Coroutine $coroutine 要恢复的协程
301305
* @param ?mixed $value 恢复时传入的值
302-
* @return ControlResult 控制结果, 包含恢复操作的结果和异常信息
306+
* @return Outcome 控制结果, 包含恢复操作的结果和异常信息
303307
*/
304-
public static function resume(Coroutine $coroutine, mixed $value = null): ControlResult
308+
public static function resume(Coroutine $coroutine, mixed $value = null): Outcome
305309
{
306310
try {
307-
return new ControlResult('resume', $coroutine->resume($value), $coroutine);
311+
return new Outcome('resume', $coroutine->resume($value), $coroutine);
308312
} catch (Throwable $exception) {
309-
return new ControlResult('resume', null, $coroutine, $exception);
313+
return new Outcome('resume', null, $coroutine, $exception);
310314
} finally {
311315
if ($coroutine->isTerminated()) {
312316
Scheduler::remove($coroutine->key());
@@ -319,14 +323,14 @@ public static function resume(Coroutine $coroutine, mixed $value = null): Contro
319323
* 向协程抛出异常
320324
* @param Coroutine $coroutine 要抛出异常的协程
321325
* @param Throwable $exception 要抛出的异常对象
322-
* @return ControlResult 控制结果, 包含异常抛出操作的结果和异常信息
326+
* @return Outcome 控制结果, 包含异常抛出操作的结果和异常信息
323327
*/
324-
public static function throw(Coroutine $coroutine, Throwable $exception): ControlResult
328+
public static function throw(Coroutine $coroutine, Throwable $exception): Outcome
325329
{
326330
try {
327-
return new ControlResult('throw', $coroutine->throw($exception), $coroutine);
331+
return new Outcome('throw', $coroutine->throw($exception), $coroutine);
328332
} catch (Throwable $exception) {
329-
return new ControlResult('resume', null, $coroutine, $exception);
333+
return new Outcome('resume', null, $coroutine, $exception);
330334
} finally {
331335
if ($coroutine->isTerminated()) {
332336
Scheduler::remove($coroutine->key());
@@ -338,9 +342,9 @@ public static function throw(Coroutine $coroutine, Throwable $exception): Contro
338342
/**
339343
* 终止协程
340344
* @param Coroutine $coroutine 要终止的协程
341-
* @return ControlResult 控制结果, 包含终止操作的结果
345+
* @return Outcome 控制结果, 包含终止操作的结果
342346
*/
343-
public static function terminate(Coroutine $coroutine): ControlResult
347+
public static function terminate(Coroutine $coroutine): Outcome
344348
{
345349
if ($coroutine->state() === Coroutine::STATE_RUNNING) {
346350
$coroutine->onState(
@@ -349,7 +353,7 @@ public static function terminate(Coroutine $coroutine): ControlResult
349353
prepare: true
350354
);
351355

352-
return new ControlResult('terminate', null, $coroutine);
356+
return new Outcome('terminate', null, $coroutine);
353357
}
354358

355359
return self::throw($coroutine, new TerminateException());
Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,33 @@
1818

1919
use function debug_backtrace;
2020

21-
class ControlResult
21+
class Outcome
2222
{
2323
/**
2424
* @var bool
2525
*/
26-
private bool $resolve = false;
26+
private bool $resolved = false;
2727

2828
/**
2929
* @var array
3030
*/
31-
private array $debugTrace = [];
31+
private array $trace = [];
3232

3333
/**
3434
* @param string $action
3535
* @param mixed $value
36-
* @param Coroutine $coroutine
36+
* @param ?Coroutine $coroutine
3737
* @param Throwable|null $exception
3838
*/
3939
public function __construct(
4040
private readonly string $action,
4141
private readonly mixed $value,
42-
private readonly Coroutine $coroutine,
42+
private readonly ?Coroutine $coroutine,
4343
private readonly ?Throwable $exception = null
4444
) {
4545
if ($exception) {
46-
Scheduler::reportException($this);
47-
$this->debugTrace = debug_backtrace();
46+
Scheduler::auditFailure($this);
47+
$this->trace = debug_backtrace();
4848
}
4949
}
5050

@@ -67,9 +67,9 @@ public function isError(): bool
6767
/**
6868
* @return bool
6969
*/
70-
public function isResolve(): bool
70+
public function isResolved(): bool
7171
{
72-
return $this->resolve;
72+
return $this->resolved;
7373
}
7474

7575
/**
@@ -99,15 +99,15 @@ public function exception(): ?Throwable
9999
/**
100100
* @return array
101101
*/
102-
public function debugTrace(): array
102+
public function trace(): array
103103
{
104-
return $this->debugTrace;
104+
return $this->trace;
105105
}
106106

107107
/**
108-
* @return Coroutine
108+
* @return ?Coroutine
109109
*/
110-
public function coroutine(): Coroutine
110+
public function coroutine(): ?Coroutine
111111
{
112112
return $this->coroutine;
113113
}
@@ -116,11 +116,11 @@ public function coroutine(): Coroutine
116116
* @param string|null $type
117117
* @return mixed
118118
*/
119-
public function resolve(?string $type = null): mixed
119+
public function unwrap(?string $type = null): mixed
120120
{
121121
if ($this->exception) {
122122
if (!$type || $this->exception instanceof $type) {
123-
$this->resolve = true;
123+
$this->resolved = true;
124124
}
125125
return $this->exception;
126126
}
@@ -132,10 +132,32 @@ public function resolve(?string $type = null): mixed
132132
* @return void
133133
* @throws Throwable
134134
*/
135-
public function throw(): void
135+
public function rethrow(): void
136136
{
137137
if ($this->exception) {
138-
throw $this->resolve();
138+
throw $this->unwrap();
139139
}
140140
}
141+
142+
/**
143+
* @param string $action
144+
* @param mixed $value
145+
* @param Coroutine|null $coro
146+
* @return self
147+
*/
148+
public static function success(string $action, mixed $value, ?Coroutine $coro = null): self
149+
{
150+
return new self($action, $value, $coro, null);
151+
}
152+
153+
/**
154+
* @param string $action
155+
* @param Throwable $e
156+
* @param Coroutine|null $coro
157+
* @return self
158+
*/
159+
public static function failure(string $action, Throwable $e, ?Coroutine $coro = null): self
160+
{
161+
return new self($action, null, $coro, $e);
162+
}
141163
}

src/Stream.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,10 @@ public function flush(float|int|null $timeout = null): void
243243

244244
$this->flushOnce();
245245
if ($this->writeBuf->isEmpty()) {
246-
Scheduler::resume($this->wco)->resolve(CoroutineStateException::class);
246+
Scheduler::resume($this->wco)->unwrap(CoroutineStateException::class);
247247
}
248248
} catch (Throwable $exception) {
249-
Scheduler::throw($this->wco, $exception)->resolve(CoroutineStateException::class);
249+
Scheduler::throw($this->wco, $exception)->unwrap(CoroutineStateException::class);
250250
}
251251
});
252252

@@ -342,7 +342,7 @@ public function close(): void
342342

343343
if ($this->wco) {
344344
Scheduler::throw($this->wco, new ConnectionException('Stream is closed or disconnected'))
345-
->resolve(CoroutineStateException::class);
345+
->unwrap(CoroutineStateException::class);
346346
}
347347
}
348348

src/Time.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function sleep(float $seconds): void
3939
Event::timer(
4040
$seconds,
4141
0,
42-
static fn () => Scheduler::resume($owner)->resolve(CoroutineStateException::class)
42+
static fn () => Scheduler::resume($owner)->unwrap(CoroutineStateException::class)
4343
);
4444

4545
try {

src/Watch/ExtEvWatcher.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
namespace Ripple\Watch;
1414

1515
use Ripple\Runtime\Interface\EventDriverInterface;
16-
use Ripple\Runtime\Support\Display;
17-
use Ripple\Runtime\Support\Stdin;
16+
use Ripple\Runtime\Scheduler;
17+
use Ripple\Runtime\Scheduler\Outcome;
1818
use Ripple\Watch\Interface\WatchAbstract;
1919
use Ripple\Watch\Interface\WatcherInterface;
2020
use Closure;
@@ -101,7 +101,7 @@ public function watchRead(mixed $resource, Closure $callback): int
101101
try {
102102
$callback($watchId, $w->data);
103103
} catch (Throwable $exception) {
104-
Stdin::println(Display::exception($exception));
104+
Scheduler::auditFailure(new Outcome('Event::watchRead', null, null, $exception));
105105
}
106106
}, $resource);
107107

@@ -120,7 +120,7 @@ public function watchWrite(mixed $resource, Closure $callback): int
120120
try {
121121
$callback($watchId, $w->data);
122122
} catch (Throwable $exception) {
123-
Stdin::println(Display::exception($exception));
123+
Scheduler::auditFailure(new Outcome('Event::watchWrite', null, null, $exception));
124124
}
125125
}, $resource);
126126

@@ -145,7 +145,7 @@ public function timer(float $after, float $repeat, Closure $callback): int
145145
try {
146146
$callback($watchId);
147147
} catch (Throwable $exception) {
148-
Stdin::println(Display::exception($exception));
148+
Scheduler::auditFailure(new Outcome('Event::timer', null, null, $exception));
149149
}
150150
});
151151

@@ -164,7 +164,7 @@ public function watchSignal(int $signal, Closure $callback): int
164164
try {
165165
$callback($watchId, $w->signum);
166166
} catch (Throwable $exception) {
167-
Stdin::println(Display::exception($exception));
167+
Scheduler::auditFailure(new Outcome('Event::Signal', null, null, $exception));
168168
}
169169
});
170170

0 commit comments

Comments
 (0)