实现一个HTTP服务器时,每个申请开一个协程解决,解决完申请后销毁,让咱们优化这段逻辑,咱们很容易联想到FASTCGI比照CGI的改良。无非就是事后生成几个worker过程,解决实现后不销毁复用嘛。
那协程也是同理,为了不便演示,只开一个协程,便于了解。
一个申请一个协程:
<?php
Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]);
use function Co\run;
run(function () {
$socket = stream_socket_server(
'tcp://0.0.0.0:9998',
$errno,
$errstr
);
if (!$socket) {
echo "$errstr ($errno)" . PHP_EOL;
exit(1);
}
while (true) {
$conn = stream_socket_accept($socket);
if ($conn) {
go(function() use ($conn) {
fwrite($conn, "HTTP/1.1 200 OK\r\nContent-Length:11\r\n\r\nHello!world");
});
}
}
});
协程复用:
<?php
Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]);
use Co\Channel;
use function Co\run;
run(function () {
$channel = new Channel(1);
go(function() use ($channel) {
while (($conn = $channel->pop()) !== false) {
fwrite($conn, "HTTP/1.1 200 OK\r\nContent-Length:11\r\n\r\nHello!world");
}
});
$socket = stream_socket_server(
'tcp://0.0.0.0:9999',
$errno,
$errstr
);
if (!$socket) {
echo "$errstr ($errno)" . PHP_EOL;
exit(1);
}
while (true) {
$conn = stream_socket_accept($socket);
if ($conn) {
$channel->push($conn);
}
}
});
事实中,咱们不会只开一个协程,下次有工夫再来撸一个协程池,实现getWorker
和putWorker
,反对协程池动静扩容。
感激https://github.com/panjf2000/ants
我的项目的启发。
发表回复