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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay