@@ -20,21 +20,24 @@ trait ChannelMethods
2020 /** @var string 通道前缀 */
2121 protected static string $ _CHANNEL = '#Channel# ' ;
2222
23+ /** @var string 通道pid列表 */
24+ protected static string $ _CHANNEL_PID_LIST = '#ChannelPidList# ' ;
25+
2326 /**
2427 * @var array = [channelKey => futureId]
2528 */
2629 protected static array $ _listeners = [];
2730
2831 /**
29- * @var float|int|null
32+ * @var float|int
3033 */
31- protected static float |int | null $ interval = null ;
34+ protected static float |int $ interval = 0 ;
3235
3336 /**
34- * @param float|int|null $interval
37+ * @param float|int $interval
3538 * @return void
3639 */
37- public static function SetChannelListenerInterval (float |int | null $ interval ): void
40+ public static function SetChannelListenerInterval (float |int $ interval ): void
3841 {
3942 self ::$ interval = $ interval ;
4043 }
@@ -48,6 +51,27 @@ public static function GetChannelKey(string $key): string
4851 return self ::$ _CHANNEL . $ key ;
4952 }
5053
54+ /**
55+ * 通道全局开启使用信号监听
56+ *
57+ * @param bool $enable
58+ * @return void
59+ */
60+ public static function channelUseSignalEnable (bool $ enable = true ): void
61+ {
62+ Future::$ useSignal = $ enable ;
63+ }
64+
65+ /**
66+ * 通道是否使用信号监听
67+ *
68+ * @return bool
69+ */
70+ public static function isChannelUseSignal (): bool
71+ {
72+ return Future::$ useSignal ;
73+ }
74+
5175 /**
5276 * 通道获取
5377 *
@@ -124,6 +148,13 @@ protected static function _ChPublish(string $key, mixed $message, bool $store =
124148 }
125149
126150 self ::_Set ($ channelName , $ channel );
151+ // 使用信号监听
152+ if (self ::isChannelUseSignal ()) {
153+ $ list = self ::_Get (self ::$ _CHANNEL_PID_LIST , []);
154+ foreach ($ list as $ pid ) {
155+ @posix_kill ($ pid , Future::$ signal );
156+ }
157+ }
127158 return [
128159 'timestamp ' => microtime (true ),
129160 'method ' => $ func ,
@@ -156,6 +187,12 @@ protected static function _ChCreateListener(string $key, string|int $workerId, C
156187 self ::_Atomic ($ key , function () use (
157188 $ key , $ workerId , $ func , $ params , $ listener , &$ result
158189 ) {
190+ // 信号监听则注册pid
191+ if (self ::isChannelUseSignal ()) {
192+ $ channelPidList = self ::_Get (self ::$ _CHANNEL_PID_LIST , []);
193+ $ channelPidList [$ pid = posix_getpid ()] = $ pid ;
194+ self ::_Set (self ::$ _CHANNEL_PID_LIST , $ channelPidList );
195+ }
159196 /**
160197 * [
161198 * workerId = [
@@ -165,11 +202,7 @@ protected static function _ChCreateListener(string $key, string|int $workerId, C
165202 * ]
166203 */
167204 $ channel = self ::_Get ($ channelName = self ::GetChannelKey ($ key ), []);
168-
169- // 设置回调
170- $ channel [$ workerId ]['futureId ' ] =
171- self ::$ _listeners [$ key ] =
172- $ result = Future::add (function () use ($ key , $ workerId , $ listener ) {
205+ $ callback = function () use ($ key , $ workerId , $ listener ) {
173206 // 原子性执行
174207 self ::_Atomic ($ key , function () use ($ key , $ workerId , $ listener ) {
175208 $ channel = self ::_Get ($ channelName = self ::GetChannelKey ($ key ), []);
@@ -182,7 +215,9 @@ protected static function _ChCreateListener(string $key, string|int $workerId, C
182215 }
183216
184217 });
185- }, interval: self ::$ interval );
218+ };
219+ // 设置回调
220+ $ channel [$ workerId ]['futureId ' ] = self ::$ _listeners [$ key ] = $ result = Future::add ($ callback , interval: self ::$ interval );
186221 $ channel [$ workerId ]['value ' ] = [];
187222 // 如果存在默认数据
188223 if ($ default = $ channel ['--default-- ' ]['value ' ] ?? []) {
@@ -218,7 +253,16 @@ protected static function _ChRemoveListener(string $key, string|int $workerId, b
218253 $ key , $ workerId , $ func , $ params , $ remove
219254 ) {
220255 if ($ id = self ::$ _listeners [$ key ] ?? null ) {
256+ // 移除future
221257 Future::del ($ id );
258+ // 信号监听则注册pid
259+ if (self ::isChannelUseSignal ()) {
260+ $ channelPidList = self ::_Get (self ::$ _CHANNEL_PID_LIST , []);
261+ if ($ channelPidList [$ pid = posix_getpid ()] ?? null ) {
262+ unset($ channelPidList [$ pid ]);
263+ self ::_Set (self ::$ _CHANNEL_PID_LIST , $ channelPidList );
264+ }
265+ }
222266 if ($ remove ) {
223267 /**
224268 * [
@@ -234,7 +278,6 @@ protected static function _ChRemoveListener(string $key, string|int $workerId, b
234278 }
235279 unset(self ::$ _listeners [$ key ]);
236280 }
237-
238281 return [
239282 'timestamp ' => microtime (true ),
240283 'method ' => $ func ,
0 commit comments