DEV Community

Cover image for OpenSwoole and Doctrine
sebk69
sebk69

Posted on

3 1

OpenSwoole and Doctrine

Connection issue

The doctrinal issue is that coroutines use the same connection.

On a service with many connections, users end up waiting for SQL queries to finish one after the other.

Solutions

1) Create a connection for each request

This solution has been adopted in the package diego-ninja/swoole-mysql-doctrine-driver : diego-ninja/swoole-mysql-doctrine-driver

Two issues arise:

  • We are in a long-time process, and connections consume RAM.
  • Under heavy load, database server may block due to a large number of open connections.

2) Use a connection pool

The connection pool is a class replacing native driver that store and reuse connections. You can define the max number of concurent connections, determined by the size of your database server.

The process is simple :

For example : we use a pool of 10 connections.

If there is a free connection, we get the connection. Send the connection and release the connection.

If there is no free connections, we sleep a short time and retry.

small/swoole-pdo-doctrine-driver

This package use small/swoole-patterns abstract pool's class.

Here is an example to configure it with symfony :

doctrine:
    dbal:
        driver: Small\SwooleDbalPdo\PDODriver
        url: '%env(resolve:DATABASE_URL)%'
Enter fullscreen mode Exit fullscreen mode

The source code is available on https://git.small-project.dev/lib/small-swoole-doctine-dbal-pdo

The composer package is available on https://packagist.org/packages/small/small-swoole-doctine-dbal-pdo

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more