4
4
5
5
use Amp \Cache \LocalCache ;
6
6
use Amp \Cancellation ;
7
- use Amp \CancelledException ;
7
+ use Amp \DeferredFuture ;
8
8
use Amp \ForbidCloning ;
9
9
use Amp \ForbidSerialization ;
10
10
use Amp \NullCancellation ;
@@ -25,14 +25,15 @@ final class SocketIpcHub implements IpcHub
25
25
/** @var non-empty-string */
26
26
private readonly string $ uri ;
27
27
28
- /** @var array<string, EventLoop\Suspension > */
28
+ /** @var array<string, DeferredFuture > */
29
29
private array $ waitingByKey = [];
30
30
31
31
/** @var \Closure(): void */
32
32
private readonly \Closure $ accept ;
33
33
34
34
private bool $ queued = false ;
35
35
36
+ /** @var LocalCache<ResourceSocket> */
36
37
private LocalCache $ clientsByKey ;
37
38
38
39
/**
@@ -68,8 +69,8 @@ public function __construct(
68
69
if (!$ client ) {
69
70
$ queued = false ;
70
71
$ exception = new Socket \SocketException ('IPC socket closed before the client connected ' );
71
- foreach ($ waitingByKey as $ suspension ) {
72
- $ suspension -> throw ($ exception );
72
+ foreach ($ waitingByKey as $ deferred ) {
73
+ $ deferred -> error ($ exception );
73
74
}
74
75
return ;
75
76
}
@@ -82,7 +83,7 @@ public function __construct(
82
83
}
83
84
84
85
if (isset ($ waitingByKey [$ received ])) {
85
- $ waitingByKey [$ received ]->resume ($ client );
86
+ $ waitingByKey [$ received ]->complete ($ client );
86
87
unset($ waitingByKey [$ received ]);
87
88
} else {
88
89
$ clientsByKey ->set ($ received , $ client );
@@ -112,8 +113,8 @@ public function close(): void
112
113
}
113
114
114
115
$ exception = new Socket \SocketException ('IPC socket closed before the client connected ' );
115
- foreach ($ this ->waitingByKey as $ suspension ) {
116
- $ suspension -> throw ($ exception );
116
+ foreach ($ this ->waitingByKey as $ deferred ) {
117
+ $ deferred -> error ($ exception );
117
118
}
118
119
}
119
120
@@ -165,19 +166,19 @@ public function accept(string $key, ?Cancellation $cancellation = null): Resourc
165
166
$ this ->queued = true ;
166
167
}
167
168
168
- $ cancellation = $ cancellation ?? new NullCancellation ();
169
- $ cancellation ->throwIfRequested ();
169
+ $ cancellation ??= new NullCancellation ();
170
170
171
- $ this ->waitingByKey [$ key ] = $ suspension = EventLoop::getSuspension ();
172
- $ cancellationId = $ cancellation ->subscribe (function (CancelledException $ exception ) use ($ suspension ) {
173
- $ suspension ->throw ($ exception );
171
+ $ this ->waitingByKey [$ key ] = $ deferred = new DeferredFuture ();
172
+
173
+ $ waitingByKey = &$ this ->waitingByKey ;
174
+ $ cancellationId = $ cancellation ->subscribe (static function () use (&$ waitingByKey , $ key ): void {
175
+ unset($ waitingByKey [$ key ]);
174
176
});
175
177
176
178
try {
177
- $ client = $ suspension -> suspend ( );
179
+ $ client = $ deferred -> getFuture ()-> await ( $ cancellation );
178
180
} finally {
179
181
$ cancellation ->unsubscribe ($ cancellationId );
180
- unset($ this ->waitingByKey [$ key ]);
181
182
}
182
183
183
184
return $ client ;
0 commit comments