forked from Luccifer/FB-Source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionManagerHack.txt
46 lines (42 loc) · 1.27 KB
/
ConnectionManagerHack.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?hh
class ConnectionManager {
protected Vector<AsyncMysqlConnection> $freeConnections = Vector {};
public function getFreeConnection():AsyncMysqlConnection {
try {
return $this->freeConnections->pop();
} catch (Exception $e) {
//Create new Connection
throw $e;
}
}
public function free(AsyncMysqlConnection $connection) {
$this->freeConnections->add($connection);
}
}
class Connection {
protected ?AsyncMysqlConnection $connection;
public function __construct(protected ConnectionManager $manager) {
}
public function queryf($query):Awaitable<AsyncMysqlResult> {
$this->connection = $this->manager->getFreeConnection();
return $this->manager->getFreeConnection()->queryf($query);
}
public function close():void {
if ($this->connection) {
$this->manager->free($this->connection);
}
}
}
class ConnectionPool {
protected ConnectionManager $manager;
public function __construct() {
$this->manager = new ConnectionManager();
}
public function getConnection():Connection {
return new Connection($this->manager);
}
}
$pool = new ConnectionPool();
for ($i = 0; $i < 100; $i++) {
$pool->getConnection();
}