Simple and universal connection pool for ReactPHP applications.

Overview

szado/reactphp-connection-pool

Async and flexible pool for any type of connections built on top of ReactPHP.

Connection pooling allows you to easily manage range of connections with some remote service (e.g. a database server). You can define how many connections your app can estabilish or how to react when all connections are busy at the same time.

  • State monitoring - each adapter maintains its connection state ("Ready", "Busy" or "Disconnected"). Based on this, the connection selector can determine if the specific connection is currently ready to use. This is especially useful when you use stateful operations (e.g. database transactions).
  • Flexible - manage any type of connections by implementing your own connection adapter and specify how connections are to be selected for use by passing proper connection selector.
  • Lightweight and simple - in assumptions it is a simple component that can be freely extended according to your preferences.

Requirements

  • PHP >= 8.1 (fibers, enums)

Examples

class MyConnectionAdapter implements Szado\React\ConnectionPool\ConnectionAdapters\ConnectionAdapterInterface
{
  // Implementation of adapter for your connection.
}

$pool = new Szado\React\ConnectionPool\ConnectionPool(fn () => new MyConnectionAdapter());
$adapter = React\Async\await($pool->get());
$connection = $adapter->getConnection();
// `$connection` is ready to use :)

Additional Configuration

You can pass additional parameters to pool constructor:

  • connectionSelectorClass - define algorithm used for selecting connections (by default simple UsageConnectionSelector is used).
  • connectionsLimit - maximum number of connections that can be created (null for unlimited).
  • retryLimit - how many times try to search for an active connection before rejecting (null for unlimited, 0 for immediately rejection if none at the moment).
  • retryEverySec - check for available connections every how many seconds (only if $retryLimit is enabled).
  • loop - instance of React\EventLoop\LoopInterface to use.

Todo

  • Built-in adapters (clue/reactphp-redis, friends-of-reactphp/mysql).
  • Connection selector based on Round Robin algorithm.
  • Reconnection.

At the end...

  • Run tests: ./vendor/bin/phpunit
  • Feel free to submit your PR
  • Licence: MIT
You might also like...
Mind is the PHP code framework designed for developers. It offers a variety of solutions for creating design patterns, applications and code frameworks.
Mind is the PHP code framework designed for developers. It offers a variety of solutions for creating design patterns, applications and code frameworks.

Mind Mind is the PHP code framework designed for developers. It offers a variety of solutions for creating design patterns, applications and code fram

This component may look complex, weird and full of hacks but it is a game changer for how we run PHP applications.
This component may look complex, weird and full of hacks but it is a game changer for how we run PHP applications.

PHP Runtimes In early 2021, Symfony created a "Runtime component". This component may look complex, weird and full of hacks but it is a game changer f

🍃Termwind allows you to build unique and beautiful PHP command-line applications, using the Tailwind CSS API
🍃Termwind allows you to build unique and beautiful PHP command-line applications, using the Tailwind CSS API

Termwind allows you to build unique and beautiful PHP command-line applications, using the Tailwind CSS API. In short, it's like Tailwind CSS, but for the PHP command-line applications.

TrailLamp is a lightweight, easy-to-use Php MVC framework that can be used to build web applications and REST APIs.

TrailLamp Introduction TrailLamp is a lightweight, easy-to-use Php MVC framework that can be used to build web applications and REST APIs. Installatio

Asynchronous & Fault-tolerant PHP Framework for Distributed Applications.
Asynchronous & Fault-tolerant PHP Framework for Distributed Applications.

Kraken PHP Framework ~ Release the Kraken! Note: This repository contains the core of the Kraken Framework. If you want to start developing new applic

Asynchronous server-side framework for network applications implemented in PHP using libevent

phpDaemon https://github.com/kakserpom/phpdaemon Asynchronous framework in PHP. It has a huge number of features. Designed for highload. Each worker i

An OPCache Preloader for CakePHP 4.x applications

CakePHP Preloader An OPCache preloader for CakePHP. Reference: https://www.php.net/manual/en/opcache.preloading.php This package is meant to provide a

The Runtime Component enables decoupling applications from global state.

Runtime Component Symfony Runtime enables decoupling applications from global state. This Component is experimental. Experimental features are not cov

Biny is a tiny, high-performance PHP framework for web applications

Biny is high performance. Framework comes default with response time of less than 1ms. Stand-alone QPS easily up to 3000.

Comments
  • release connection

    release connection

    hi, how can i decrease the connection usage-counter check out ???

    is it possible to block an connection to handle an SQL transactions? so no other SELECT or something else with an implicit COMMIT will break an transaction?

    regards, volker.

    opened by skydiablo 2
  • Updates in ConnectionPool in order to use React\Async\await function

    Updates in ConnectionPool in order to use React\Async\await function

    According to some issues when ConnectionState enum (of $connectionFactory Closure/Class, which creates connection adapter) isn't Ready and retryWithDelay() function is called repeatedly.

    I used to debug calls that used in your test.php file.

    If you want better code readability you can use this variation of retryWithDelay() function (notice additional $selectConnectionDeferred):

    protected function retryWithDelay(Deferred $deferred = null): \React\Promise\Promise
    {
          if ($this->retryLimit !== null && $this->retryLimit < 1) {
              throw new ConnectionPoolException('No available connection to use');
          }
    
          $deferred ??= new Deferred();
    
          if (!$this->awaiting->contains($deferred)) {
              $this->awaiting->attach($deferred, 0);
          }
    
          if ($this->awaiting[$deferred] === $this->retryLimit) {
              $this->awaiting->detach($deferred);
              throw new ConnectionPoolException("No available connection to use; $this->retryLimit attempts were made");
          }
    
          $selectConnectionDeferred = new Deferred();
    
          $this->loop->addTimer($this->retryEverySec, function () use ($deferred, $selectConnectionDeferred) {
              $connectionAdapter = $this->selectConnection();
    
              if ($connectionAdapter) {
                  $this->awaiting->detach($deferred);
                  $selectConnectionDeferred->resolve(true);
                  $deferred->resolve($connectionAdapter);
                  return;
              }
    
              $this->awaiting[$deferred] = $this->awaiting[$deferred] + 1;
              $selectConnectionDeferred->resolve(false);
          });
    
          return $selectConnectionDeferred->promise()->then(function($connectionSelected) use ($deferred) {
              return $connectionSelected ? $deferred->promise() : $this->retryWithDelay($deferred);
          });
    }
    
    opened by iorsa 2
Owner
shado
shado
Simple, async SOAP webservice client, built on top of ReactPHP.

clue/reactphp-soap Simple, async SOAP web service client library, built on top of ReactPHP. Most notably, SOAP is often used for invoking Remote proce

Christian Lück 62 Jul 5, 2022
PHPUnit assertions for testing ReactPHP promises

ReactPHP Promises Testing A library that provides a set of convenient assertions for testing ReactPHP promises. Under the hood uses clue/php-block-rea

Sergey Zhuk 30 Dec 8, 2022
Async HTTP proxy connector, tunnel any TCP/IP-based protocol through an HTTP CONNECT proxy server, built on top of ReactPHP.

clue/reactphp-http-proxy Async HTTP proxy connector, tunnel any TCP/IP-based protocol through an HTTP CONNECT proxy server, built on top of ReactPHP.

Christian Lück 43 Dec 25, 2022
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

Slim Framework Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs. Installation It's recommended

Slim Framework 11.5k Jan 4, 2023
Woski is a fast and simple lightweight PHP Framework for building applications in the realm of the web.

Woski is a simple fast PHP framework for the Realm The Project Installation Clone the repository $ composer create-project clintonnzedimma/woski myApp

Clinton Nzedimma 19 Aug 15, 2022
Framework X – the simple and fast micro framework for building reactive web applications that run anywhere.

Framework X Framework X – the simple and fast micro framework for building reactive web applications that run anywhere. Quickstart Documentation Tests

Christian Lück 620 Jan 7, 2023
LODSPeaKr is a framework for creating Linked Data applications in a simple and easy way

LODSPeaKr is a framework for creating Linked Data applications in a simple and easy way. You can see several applications created using LODSPeaKr.

Alvaro Graves 31 Jun 23, 2020
FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Ruby on Rails.

FlyCubePHP FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Rub

Anton 1 Dec 21, 2021
PPM is a process manager, supercharger and load balancer for modern PHP applications.

PPM - PHP Process Manager PHP-PM is a process manager, supercharger and load balancer for PHP applications. It's based on ReactPHP and works best with

PPM - PHP Process Manager 6.5k Dec 27, 2022
A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast! Condensed in a single ~65KB file

Bong Cosca 2.6k Dec 30, 2022