@@ -64,47 +64,12 @@ class Swoole
6464
6565 protected ?Resolver $ resolver ;
6666
67- /**
68- * @param Resolver|string|null $resolver Resolver instance, or host string for named-param style
69- * @param Config|array<string, mixed>|null $config Config object or array of settings
70- * @param string|null $host Host address (named-param style)
71- * @param array<int, int>|null $ports Port list (named-param style)
72- * @param int|null $workers Worker count (named-param style)
73- */
7467 public function __construct (
75- Resolver |string |null $ resolver = null ,
76- Config |array |null $ config = null ,
77- ?string $ host = null ,
78- ?array $ ports = null ,
79- ?int $ workers = null ,
68+ ?Resolver $ resolver = null ,
69+ ?Config $ config = null ,
8070 ) {
81- // Detect named-param style: first arg is a string host, not a Resolver
82- if (\is_string ($ resolver )) {
83- $ host = $ resolver ;
84- $ this ->resolver = null ;
85- } else {
86- $ this ->resolver = $ resolver ;
87- }
88-
89- // Build Config from array or named params
90- if (\is_array ($ config )) {
91- $ this ->config = self ::buildConfig ($ config , $ host , $ ports , $ workers );
92- } elseif ($ config instanceof Config) {
93- $ this ->config = $ config ;
94- } else {
95- // Build from named params with defaults
96- $ args = [];
97- if ($ host !== null ) {
98- $ args ['host ' ] = $ host ;
99- }
100- if ($ ports !== null ) {
101- $ args ['ports ' ] = $ ports ;
102- }
103- if ($ workers !== null ) {
104- $ args ['workers ' ] = $ workers ;
105- }
106- $ this ->config = new Config (...$ args );
107- }
71+ $ this ->resolver = $ resolver ;
72+ $ this ->config = $ config ?? new Config ();
10873
10974 if ($ this ->config ->isTlsEnabled ()) {
11075 /** @var TLS $tls */
@@ -137,46 +102,6 @@ public function __construct(
137102 $ this ->configure ();
138103 }
139104
140- /**
141- * Build a Config object from an associative array of settings
142- *
143- * @param array<string, mixed> $settings
144- */
145- protected static function buildConfig (
146- array $ settings ,
147- ?string $ host = null ,
148- ?array $ ports = null ,
149- ?int $ workers = null ,
150- ): Config {
151- return new Config (
152- host: $ host ?? ($ settings ['host ' ] ?? '0.0.0.0 ' ),
153- ports: $ ports ?? ($ settings ['ports ' ] ?? [5432 , 3306 , 27017 ]),
154- workers: $ workers ?? ($ settings ['workers ' ] ?? 16 ),
155- maxConnections: $ settings ['max_connections ' ] ?? 200_000 ,
156- maxCoroutine: $ settings ['max_coroutine ' ] ?? 200_000 ,
157- socketBufferSize: $ settings ['socket_buffer_size ' ] ?? 16 * 1024 * 1024 ,
158- bufferOutputSize: $ settings ['buffer_output_size ' ] ?? 16 * 1024 * 1024 ,
159- reactorNum: $ settings ['reactor_num ' ] ?? null ,
160- dispatchMode: $ settings ['dispatch_mode ' ] ?? 2 ,
161- enableReusePort: $ settings ['enable_reuse_port ' ] ?? true ,
162- backlog: $ settings ['backlog ' ] ?? 65535 ,
163- packageMaxLength: $ settings ['package_max_length ' ] ?? 32 * 1024 * 1024 ,
164- tcpKeepidle: $ settings ['tcp_keepidle ' ] ?? 30 ,
165- tcpKeepinterval: $ settings ['tcp_keepinterval ' ] ?? 10 ,
166- tcpKeepcount: $ settings ['tcp_keepcount ' ] ?? 3 ,
167- enableCoroutine: $ settings ['enable_coroutine ' ] ?? true ,
168- maxWaitTime: $ settings ['max_wait_time ' ] ?? 60 ,
169- logLevel: $ settings ['log_level ' ] ?? SWOOLE_LOG_ERROR ,
170- logConnections: $ settings ['log_connections ' ] ?? false ,
171- recvBufferSize: $ settings ['recv_buffer_size ' ] ?? 131072 ,
172- connectTimeout: $ settings ['backend_connect_timeout ' ] ?? 5.0 ,
173- skipValidation: $ settings ['skip_validation ' ] ?? false ,
174- readWriteSplit: $ settings ['read_write_split ' ] ?? false ,
175- tls: $ settings ['tls ' ] ?? null ,
176- adapterFactory: $ settings ['adapter_factory ' ] ?? null ,
177- );
178- }
179-
180105 protected function configure (): void
181106 {
182107 $ settings = [
@@ -244,6 +169,7 @@ public function onWorkerStart(Server $server, int $workerId): void
244169 // Initialize TCP adapter per worker per port
245170 foreach ($ this ->config ->ports as $ port ) {
246171 if ($ this ->config ->adapterFactory !== null ) {
172+ /** @var TCPAdapter $adapter */
247173 $ adapter = ($ this ->config ->adapterFactory )($ port );
248174 } else {
249175 $ adapter = new TCPAdapter ($ this ->resolver , port: $ port );
0 commit comments