Comments (7)
👀 Sooo this is why functions are timing out all the time?
from swoole-src.
<?php
require "./vendor/autoload.php";
use GuzzleHttp\Client;
$http = new Swoole\Http\Server('127.0.0.1', 9501);
$http->set([
'worker_num' => 1,
'enable_coroutine' => true,
'hook_flags' => SWOOLE_HOOK_ALL
]);
$http->on('request', function ($request, $response) {
if ($request->server['request_uri'] == '/test') {
$response->end('Hello World');
echo 456;
} else {
$client = new Client(['base_uri' => 'http://127.0.0.1:9501/']);
$client->request('GET', 'test');
$response->end('Hello World');
}
});
$http->start();
You can use coroutines to solve the problem of dependent requests.
from swoole-src.
<?php
require "./vendor/autoload.php";
use GuzzleHttp\Client;
$http = new Swoole\Http\Server('127.0.0.1', 9501, SWOOLE_PROCESS);
$http->set([
'worker_num' => 4,
'enable_coroutine' => false,
'dispatch_func' => function ($server, $fd, $type, $data = null) {
// The last process only handles the /health request.
if ($data && str_starts_with($data, 'GET /health HTTP/1.1')) {
return 3;
}
return rand(0, 2);
}
]);
$http->on('request', function ($request, $response) {
if ($request->server['request_uri'] == '/health') {
$response->end('Hello World');
} else {
$client = new Client(['base_uri' => 'http://127.0.0.1:9501/']);
$client->request('GET', 'health');
$response->end('Hello World');
}
});
$http->start();
In SWOOLE_PROCESS mode, by configuring the dispatch_func, the last process only handles the /health request and does not handle the /version request. The remaining processes only handle the /version request and do not handle the /health request.
from swoole-src.
@NathanFreeman Thanks for your insights 🙌
Sadly we cannot easily rewrite our server to coroutine-style due to the use of some stateful variables.
I will take a look at dispatch_func
and dispatch_mode
, that could be great solution ✨
from swoole-src.
<?php
$http = new Swoole\Http\Server('127.0.0.1', 9501);
$http->set([
'worker_num' => 4,
'task_worker_num' => 4,
'enable_coroutine' => false
]);
$http->on('request', function ($request, $response) use ($http) {
$http->task('health');
$response->end('Hello World');
});
$http->on('task', function ($serv, $task_id, $reactor_id, $data) {
echo "receive {$data}";
});
$http->start();
Or you can use the task event to handle the business logic related to /health
request in the task process. @Meldiron
from swoole-src.
@NathanFreeman Thanks for all the inisghts 🙌
I first tried all the dispatch modes blindly, without knowing how they behave. By running the same benchmarks, I only got worse results.
Next, I tried dispatch_func
which separates calls to /versions
and /health
, which solved the issue fully but made the server overall slower. I believe it became slower since our usual use case doesn't split those dependent and in-dependent requests in half, so some workers were idling for long, while others were working nonstop.
I had an idea of some "auto-scaling" solution that would analyze incoming requests, and instead of doing it half-and-half, it would make an informed decision to adjust the split. Sadly, I could not find easy metric to track so I decided to avoid this solution as it felt like overkill.
Finally, I decided to continue with dispatch_func
, but I need to make it smarter. Sadly, from incoming request information, I cant decide if a request will cause dependency or not. If I knew that, I could keep track of the state of each worker and their job to avoid workers with possibly dependent requests. Thankfully, I know the request is about to become dependent before sending the internal second request, which is a key to my solution here. I believe keeping the state on the master process is the right path, but the worker needs to have the ability to report to the worker saying Hey, I will now be a risky worker, please don't send requests to me
. Later when the worker finishes that request, he will report I am available for all requests again
. This will likely slow down the server but only by a small margin. If I only do this logic when there are 0 idle workers, I keep the effect very, very low, while fully avoiding freezing issues.
Considering the above solution, is there some channel for sending a message from the worker process to the master process in Swoole?
from swoole-src.
Ill also take a look at Swoole tasks and see if they can help in my use-case. Ill keep you posted on this topic as well 🙏
from swoole-src.
Related Issues (20)
- A bug occurred in Swoole-v5.1.1, please report it. HOT 19
- Websocket connection failed. SSL related Issue? HOT 1
- Swoole not supporting partitioned cookie parameter HOT 1
- Can allow_redirects be supported on the HTTP/WebSocket? HOT 7
- Alpine + Swoole + PgSQL on the host = `could not send SSL negotiation packet: Resource temporarily unavailable` HOT 15
- 已解决
- 开启一键协程化之后, `proc_open()` 似乎会受到先前空方法体的 `run(function() {});` 的影响。 HOT 1
- Worker exits without much information HOT 3
- Websocket服務,調用Task做訊息發送,出現session does not exits HOT 5
- \Swoole\Server::shutdown never gets called and signals don't seem to work with \Swoole\Process::signal() / pcntl_signal() HOT 4
- A bug occurred in Swoole-v5.1.2, please report it HOT 25
- PHP Startup Unable to load dynamic library 'swoole' HOT 2
- guzzlehttp在stream=true时,设置超时无效 HOT 5
- 协程被长时间挂起会导致内存大量增加 HOT 49
- windows swoole-cli-v4.8.13-cygwin-x64.zip 在windows上 run(function{}) 内的curl 问题 HOT 2
- 在 Swoole\Coroutine\Http\Server 中的 http server 使用 sendfile 在高并发时会导致 PHP 假死并且内存泄露 HOT 57
- 🪶 在 C++ 中,有提供了Socket sendfile的方法(zerocopy),可以不通过CPU去调度资源,避免重复拷贝两次DMZ(或者更多) HOT 15
- windows swoole-cli-v4.8.13-cygwin-x64 使用协程监听的Channel数据然后插入数据库,插入没几条就报错了! HOT 5
- syntax error near unexpected token `SQLITE,' HOT 4
- @swoole/library/ext/sockets.php(217) E_DEPRECATED:Creation of dynamic property Swoole\Coroutine\Socket::$__ext_sockets_nonblock is deprecated HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swoole-src.