Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Question about Swoole and Container for parallel requests #632

Open
yoh opened this issue Apr 22, 2021 · 2 comments
Open

Question about Swoole and Container for parallel requests #632

yoh opened this issue Apr 22, 2021 · 2 comments
Assignees

Comments

@yoh
Copy link

yoh commented Apr 22, 2021

Hi @leocavalcante !
I'm happy with Siler on PHP-FPM and I try to use it with Swoole now :)

I have probably a silly question about the mix of Siler Swoole and the Siler Container :

  • Siler\Container use singleton with static ::getInstance()
  • Siler\Swoole use Container\set|get to store data about Routing/Request/Response
    When I add a counter with Siler\Container\set('count', Siler\Container\get('count', 0) + 1), the count value is incremented requests after requests. I deduce from this behavior that the container values are shared between requests.

When parallel requests income (req A and req B), are we sure that there is no conflict on Container\get(SWOOLE_HTTP_REQUEST) ? I don't find doc about this on swoole website...
How to store informations by request (logged user, config, etc.) and be sure there is no data leak between parallel (and/or one after the other) requests ?

Thanks !

@leocavalcante
Copy link
Owner

Hi @yoh
Good question BTW, actually I'm not sure how it will behave, never thought about this before.
I will try to make some tests.
Thanks for bringing this up.

@yoh
Copy link
Author

yoh commented Apr 22, 2021

Hi @leocavalcante , thanks for the reply !

I use Siler v1.7.8 and Swoole v4.6.5
I made a simple route test (code below) :

  • for each incoming requests :
    • create a random key
    • put it on Swoole\request()
    • Swoole coroutine sleep 2s
    • get Swoole\request() again
    • compare randoms
<?php declare(strict_types=1);

require_once 'vendor/autoload.php';

use Siler\Swoole;
use Siler\Route;

$handler = function ($req, $res) {
    Route\get('/random', function (array $params) {
        $random = bin2hex(random_bytes(8));
        $req = Swoole\request();
        $req->random = $random;

        \Swoole\Coroutine\System::sleep(2);
        $req = Swoole\request();
        $code = $req->random === $random ? 200 : 500;

        return Swoole\json([
            'random' => $random,
            'req_random' => $req->random,
            'code' => $code,
        ], $code);
    });

    // None of the above short-circuited the response with Swoole\emit().
    Swoole\emit('Not found', 404);
};

Swoole\http($handler)->start();

When I call my route one by one, it's OK :

{
"random": "3dbbfb5d231e87ab",
"req_random": "3dbbfb5d231e87ab",
"code": 200
}

When I launch wrk and call my route at the same time, it's not OK :
wrk -t4 -c100 -d5s http://0.0.0.0:9501/random

{
"random": "6cd213e83b72710a",
"req_random": "b6dbdc5b74d6a8ab",
"code": 500
}

After wrk terminate, when I call my route, it's OK.

I think using Swoole\request() is currently dangerous.
What do you think about that ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants