3
3
namespace Rcalicdan \FiberAsync \EventLoop ;
4
4
5
5
use Fiber ;
6
+ use Rcalicdan \FiberAsync \EventLoop \Detectors \UvDetector ;
7
+ use Rcalicdan \FiberAsync \EventLoop \Factories \EventLoopComponentFactory ;
6
8
use Rcalicdan \FiberAsync \EventLoop \Handlers \ActivityHandler ;
7
9
use Rcalicdan \FiberAsync \EventLoop \Handlers \SleepHandler ;
8
10
use Rcalicdan \FiberAsync \EventLoop \Handlers \StateHandler ;
@@ -81,27 +83,21 @@ class EventLoop implements EventLoopInterface
81
83
private int $ iterationCount = 0 ;
82
84
private float $ lastOptimizationCheck = 0 ;
83
85
private const OPTIMIZATION_INTERVAL = 1.0 ;
84
- private const MAX_ITERATIONS = 1000000 ; // Reset counter at 1M iterations
86
+ private const MAX_ITERATIONS = 1000000 ;
85
87
86
- /**
87
- * Initialize the event loop with all required managers and handlers.
88
- *
89
- * Private constructor to enforce singleton pattern. Sets up all managers
90
- * and handlers with proper dependency injection.
91
- */
92
88
private function __construct ()
93
89
{
94
- $ this ->timerManager = new TimerManager ;
90
+ $ this ->timerManager = EventLoopComponentFactory:: createTimerManager () ;
95
91
$ this ->httpRequestManager = new HttpRequestManager ;
96
- $ this ->streamManager = new StreamManager ;
92
+ $ this ->streamManager = EventLoopComponentFactory:: createStreamManager () ;
97
93
$ this ->fiberManager = new FiberManager ;
98
94
$ this ->tickHandler = new TickHandler ;
99
95
$ this ->activityHandler = new ActivityHandler ;
100
96
$ this ->stateHandler = new StateHandler ;
101
97
$ this ->fileManager = new FileManager ;
102
- $ this ->socketManager = new SocketManager ;
98
+ $ this ->socketManager = EventLoopComponentFactory:: createSocketManager () ;
103
99
104
- $ this ->workHandler = new WorkHandler (
100
+ $ this ->workHandler = EventLoopComponentFactory:: createWorkHandler (
105
101
timerManager: $ this ->timerManager ,
106
102
httpRequestManager: $ this ->httpRequestManager ,
107
103
streamManager: $ this ->streamManager ,
@@ -111,12 +107,17 @@ private function __construct()
111
107
socketManager: $ this ->socketManager ,
112
108
);
113
109
114
- $ this ->sleepHandler = new SleepHandler (
110
+ $ this ->sleepHandler = EventLoopComponentFactory:: createSleepHandler (
115
111
$ this ->timerManager ,
116
112
$ this ->fiberManager
117
113
);
118
114
}
119
115
116
+ public function isUsingUv (): bool
117
+ {
118
+ return UvDetector::isUvAvailable ();
119
+ }
120
+
120
121
public function getSocketManager (): SocketManager
121
122
{
122
123
return $ this ->socketManager ;
@@ -248,6 +249,8 @@ public function defer(callable $callback): void
248
249
*/
249
250
public function run (): void
250
251
{
252
+ $ isUsingUv = UvDetector::isUvAvailable ();
253
+
251
254
while ($ this ->stateHandler ->isRunning () && $ this ->workHandler ->hasWork ()) {
252
255
$ this ->iterationCount ++;
253
256
$ hasImmediateWork = $ this ->tick ();
@@ -256,7 +259,7 @@ public function run(): void
256
259
$ this ->optimizeLoop ();
257
260
}
258
261
259
- if ($ this ->sleepHandler ->shouldSleep ($ hasImmediateWork )) {
262
+ if (! $ isUsingUv && $ this ->sleepHandler ->shouldSleep ($ hasImmediateWork )) {
260
263
$ sleepTime = $ this ->sleepHandler ->calculateOptimalSleep ();
261
264
$ this ->sleepHandler ->sleep ($ sleepTime );
262
265
}
0 commit comments