@@ -189,6 +189,7 @@ public function adaptiveTimerCreate(Closure $func, mixed ...$args): string
189189 {
190190 // 获取毫秒时间戳
191191 $ nowMilliTimestamp = self ::getMilliTime ();
192+ $ lastMessageMilliTimestamp = self ::getLastMessageMilliTimestamp ();
192193 // 是否开启自适应
193194 $ enable = (
194195 // 设置了退避指数
@@ -208,18 +209,23 @@ public function adaptiveTimerCreate(Closure $func, mixed ...$args): string
208209 }
209210 // 如果自适应开启
210211 if ($ enable ) {
212+ $ setTimer = false ;
211213 // 正反馈
212214 if ($ result ) {
213215 // 归零
214216 self ::$ isMaxTimerInterval = false ;
215- // 重新设置定时器
216- $ setTimer = true ;
217- // 定时器初始化
218- $ this ->setTimerInterval ($ this ->getTimerInitialInterval ());
217+ // 执行时长+消息等待时长
218+ $ processTimer = self ::getMilliTime () - $ lastMessageMilliTimestamp ;
219+ // 如果processTimer小于当前定时器间隔,则重置时间间隔为processTimer,否则保持当前定时器间隔
220+ if ($ processTimer < $ this ->getTimerInterval ()) {
221+ // 重新设置定时器
222+ $ setTimer = true ;
223+ // 定时器设置
224+ $ this ->setTimerInterval (min ($ processTimer , $ this ->getMaxTimerInterval ()));
225+ }
219226 }
220227 // 负反馈
221228 else {
222- $ setTimer = false ;
223229 if (
224230 $ nowMilliTimestamp - self ::getLastMessageMilliTimestamp () > $ this ->getIdleThreshold () and // 闲置超过闲置阈值
225231 !self ::isMaxTimerInterval () // 非最大间隔
@@ -239,7 +245,7 @@ public function adaptiveTimerCreate(Closure $func, mixed ...$args): string
239245 self ::adaptiveTimerDelete ($ id );
240246 // 创建新定时器
241247 self ::$ timerIdMap [$ id ] = (is_worker_version_5 () and method_exists (Worker::$ globalEvent , 'repeat ' ))
242- ? Worker::$ globalEvent ->repeat (floatval ($ this ->getTimerInitialInterval () / 1000 ), $ callback , $ args )
248+ ? Worker::$ globalEvent ->repeat (floatval ($ this ->getTimerInterval () / 1000 ), $ callback , $ args )
243249 : Worker::$ globalEvent ->add (floatval ($ this ->getTimerInterval () / 1000 ), EventInterface::EV_TIMER , $ callback );
244250 }
245251 }
0 commit comments