coroutine-postgresql.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. include '../vendor/autoload.php';
  3. use Smf\ConnectionPool\ConnectionPool;
  4. use Smf\ConnectionPool\Connectors\CoroutinePostgreSQLConnector;
  5. use Swoole\Coroutine\PostgreSQL;
  6. go(function () {
  7. // All PostgreSQL connections: [10, 30]
  8. $pool = new ConnectionPool(
  9. [
  10. 'minActive' => 10,
  11. 'maxActive' => 30,
  12. 'maxWaitTime' => 5,
  13. 'maxIdleTime' => 20,
  14. 'idleCheckInterval' => 10,
  15. ],
  16. new CoroutinePostgreSQLConnector,
  17. [
  18. 'connection_strings' => 'host=127.0.0.1 port=5432 dbname=postgres user=postgres password=xy123456',
  19. ]
  20. );
  21. echo "Initializing connection pool\n";
  22. $pool->init();
  23. defer(function () use ($pool) {
  24. echo "Closing connection pool\n";
  25. $pool->close();
  26. });
  27. echo "Borrowing the connection from pool\n";
  28. /**@var PostgreSQL $connection */
  29. $connection = $pool->borrow();
  30. $result = $connection->query("SELECT * FROM pg_stat_database where datname='postgres';");
  31. $stat = $connection->fetchAssoc($result);
  32. echo "Return the connection to pool as soon as possible\n";
  33. $pool->return($connection);
  34. var_dump($stat);
  35. });