@@ -106,11 +106,11 @@ function loadDashboardData(string $userId): array
106106 async $dashboardScope {
107107
108108 try {
109- [$profile, $notifications, $activity] = await [
109+ [$profile, $notifications, $activity] = await Async\all( [
110110 spawn fetchUserProfile($userId),
111111 spawn fetchUserNotifications($userId),
112112 spawn fetchRecentActivity($userId)
113- ] until timeout(5000);
113+ ]) until timeout(5000);
114114
115115 return [
116116 'profile' => $profile,
@@ -2058,8 +2058,10 @@ function task(): void
20582058}
20592059```
20602060
2061- Using a coroutine's local context can be useful for associating objects with a coroutine that ** MUST** be unique to each coroutine.
2062- For example, a database connection.
2061+ Using a coroutine's local context can be useful for associating objects
2062+ with a coroutine that ** MUST** be unique to each coroutine.
2063+
2064+ For example, a database connection:
20632065
20642066``` php
20652067<?php
@@ -2080,12 +2082,23 @@ class ConnectionProxy
20802082
20812083 public function __destruct()
20822084 {
2083- getGlobalConnectionPool ()->releaseConnection($this->connection);
2085+ ConnectionPool::default ()->releaseConnection($this->connection);
20842086 }
20852087}
20862088
2087- class ConnectionPool
2089+ final class ConnectionPool
20882090{
2091+ static private $pool = null;
2092+
2093+ public static function default(): ConnectionPool
2094+ {
2095+ if (self::$pool === null) {
2096+ self::$pool = new ConnectionPool();
2097+ }
2098+
2099+ return self::$pool;
2100+ }
2101+
20892102 private array $pool = [];
20902103 private int $maxConnections = 10;
20912104
@@ -2118,23 +2131,13 @@ function getDb(): ConnectionProxy
21182131 return $context->get($key);
21192132 }
21202133
2121- $pool = getGlobalConnectionPool();
2122- $connection = $pool->getConnection();
2134+ $connection = ConnectionPool::default()->getConnection();
21232135
21242136 $context->set($key, $connection);
21252137
21262138 return $connection;
21272139}
21282140
2129- function getGlobalConnectionPool(): ConnectionPool
2130- {
2131- static $pool = null;
2132- if ($pool === null) {
2133- $pool = new ConnectionPool();
2134- }
2135- return $pool;
2136- }
2137-
21382141function printUser(int $id): void
21392142{
21402143 $db = getDb();
0 commit comments