Code Monkey home page Code Monkey logo

Comments (7)

gewenyu99 avatar gewenyu99 commented on June 11, 2024 6

👀 Sooo this is why functions are timing out all the time?

from swoole-src.

NathanFreeman avatar NathanFreeman commented on June 11, 2024
<?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.

NathanFreeman avatar NathanFreeman commented on June 11, 2024
<?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.

Meldiron avatar Meldiron commented on June 11, 2024

@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.

NathanFreeman avatar NathanFreeman commented on June 11, 2024
<?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.

Meldiron avatar Meldiron commented on June 11, 2024

@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.

Meldiron avatar Meldiron commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.