forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_proxy_server.php
126 lines (112 loc) · 3.7 KB
/
mysql_proxy_server.php
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
class DBServer
{
protected $pool_size = 20;
protected $idle_pool = array(); //空闲连接
protected $busy_pool = array(); //工作连接
protected $wait_queue = array(); //等待的请求
protected $wait_queue_max = 100; //等待队列的最大长度,超过后将拒绝新的请求
/**
* @var swoole_server
*/
protected $serv;
function run()
{
$serv = new swoole_server("127.0.0.1", 9509);
$serv->set(array(
'worker_num' => 1,
'max_request' => 0,
));
$serv->on('WorkerStart', array($this, 'onStart'));
//$serv->on('Connect', array($this, 'onConnect'));
$serv->on('Receive', array($this, 'onReceive'));
//$serv->on('Close', array($this, 'onClose'));
$serv->start();
}
function onStart($serv)
{
$this->serv = $serv;
for ($i = 0; $i < $this->pool_size; $i++) {
$db = new mysqli;
$db->connect('127.0.0.1', 'root', 'root', 'www4swoole');
$db_sock = swoole_get_mysqli_sock($db);
swoole_event_add($db_sock, array($this, 'onSQLReady'));
$this->idle_pool[] = array(
'mysqli' => $db,
'db_sock' => $db_sock,
'fd' => 0,
);
}
echo "Server: start.Swoole version is [" . SWOOLE_VERSION . "]\n";
}
function onSQLReady($db_sock)
{
$db_res = $this->busy_pool[$db_sock];
$mysqli = $db_res['mysqli'];
$fd = $db_res['fd'];
echo __METHOD__ . ": client_sock=$fd|db_sock=$db_sock\n";
if ($result = $mysqli->reap_async_query()) {
$ret = var_export($result->fetch_all(MYSQLI_ASSOC), true) . "\n";
$this->serv->send($fd, $ret);
if (is_object($result)) {
mysqli_free_result($result);
}
} else {
$this->serv->send($fd, sprintf("MySQLi Error: %s\n", mysqli_error($mysqli)));
}
//release mysqli object
$this->idle_pool[] = $db_res;
unset($this->busy_pool[$db_sock]);
//这里可以取出一个等待请求
if (count($this->wait_queue) > 0) {
$idle_n = count($this->idle_pool);
for ($i = 0; $i < $idle_n; $i++) {
$req = array_shift($this->wait_queue);
$this->doQuery($req['fd'], $req['sql']);
}
}
}
function onReceive($serv, $fd, $reactor_id, $data)
{
echo "Received: $data\n";
//没有空闲的数据库连接
if (count($this->idle_pool) == 0) {
//等待队列未满
if (count($this->wait_queue) < $this->wait_queue_max) {
$this->wait_queue[] = array(
'fd' => $fd,
'sql' => $data,
);
} else {
$this->serv->send($fd, "request too many, Please try again later.");
}
} else {
$this->doQuery($fd, $data);
}
}
function doQuery($fd, $sql)
{
//从空闲池中移除
$db = array_pop($this->idle_pool);
/**
* @var mysqli
*/
$mysqli = $db['mysqli'];
for ($i = 0; $i < 2; $i++) {
$result = $mysqli->query($sql, MYSQLI_ASYNC);
if ($result === false) {
if ($mysqli->errno == 2013 or $mysqli->errno == 2006) {
$mysqli->close();
$r = $mysqli->connect();
if ($r === true) continue;
}
}
break;
}
$db['fd'] = $fd;
//加入工作池中
$this->busy_pool[$db['db_sock']] = $db;
}
}
$server = new DBServer();
$server->run();