swooletw / laravel-swoole

High performance HTTP server based on Swoole. Speed up your Laravel or Lumen applications.
MIT License
4.04k stars 390 forks source link

Coroutine/PDO reusing sockets problem and one question #468

Closed LanKing closed 3 years ago

LanKing commented 3 years ago
  1. Please provide your PHP and Swoole version. (php -v and php --ri swoole)

    PHP 7.2.34-8+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct 31 2020 16:57:15) ( NTS )
    Swoole  4.5.7 (Nov 17 2020 13:35:41)
  2. Please provide your Laravel/Lumen version. Laravel "v7.29.3

  3. Which release version of this package are you using? v2.6.68

  4. What did you do? If possible, provide a recipe for reproducing the error.

I am trying to work with mysql in Laravel out-of-the-box (lol). My way was the next:

First of all I tried to set 'mysql-coroutine' driver as default mysql driver in config/database.php ('mysql' section). But this does not works, because co-routine must be started in the event-loop ⭕

So, then I created middleware for all my api routes (they really need to be async) with the next instruction: DB::setDefaultConnection('mysql-coroutine'); and it worked!

My main question: Is this is a correct way?

Now it works but sometimes I got: Uncaught Swoole\Error: Socket#19 has already been bound to another coroutine#3026, reading of the same socket in coroutine#3027 at the same time is not allowed in /home/dontmatters/www/vendor/swooletw/laravel-swoole/src/Coroutine/PDO.php:262

As I understand, to fix this I need mysql connection pool? Please, give me an advice 🙏

  1. What did you expect to see? Stable async database work.

  2. What did you see instead? Sometimes crashes.

Arkanius commented 3 years ago

Hi!

Well, we don't encourage you to join coroutine with laravel way. In our experience you might get some conflicts (as the one that you're experiencing) at this moment.

This package was created to improve your app loading (by pre-loading it in memory). Of course you can use coroutines (and I personally think that database pool is the best example to use coroutines) but you'll need understand a lot of concepts of concurrency.

Try to check:

https://www.swoole.co.uk/docs/modules/pdo-connection-pool https://www.swoole.co.uk/docs/modules/swoole-async-mysql-client

The best book of swoole with a lot of concurrency examples