forked from shenzhe/zproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.php
70 lines (61 loc) · 2 KB
/
proxy.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
<?php
$opt = getopt("d", [
"ip::",
"port::",
"pool::"
]);
if (empty($opt['ip']) || empty($opt['port']) || empty($opt['pool'])) {
echo "examples: php proxy.php --ip=0.0.0.0 --port=9501 --pool=4 -d" . PHP_EOL;
return;
}
if(!extension_loaded('swoole')) {
echo 'pls install swoole extension, url: https://github.com/swoole/swoole-src'.PHP_EOL;
return;
}
$config = null;
$pdo = null;
$serv = new swoole_server($opt['ip'], $opt['port']);
$daemonize = 0;
if(isset($opt['d'])) {
$daemonize = 1;
}
$serv->set([
'worker_num'=>4,
'task_worker_num'=>$opt['pool'],
'daemonize' => $daemonize
]);
$serv->on('start', function($serv) {
global $opt;
swoole_set_process_name("mysql proxy runing tcp://{$opt['ip']}:{$opt['port']}, start:".date("Y-m-d H:i:s").", pid:".$serv->master_pid);
});
$serv->on('managerStart', function($serv) {
swoole_set_process_name("mysql proxy manager process, pid:".$serv->manager_pid);
});
$serv->on('workerStart', function($serv, $workerId) {
if($workerId <4) { //worker id
swoole_set_process_name("mysql proxy worker process, pid:".$serv->worker_id);
} else {
swoole_set_process_name("mysql proxy pool process, pid:".$serv->worker_id);
global $config;
$config = include_once(__DIR__.DIRECTORY_SEPARATOR.'config.php');
include_once(__DIR__.DIRECTORY_SEPARATOR.'zpdo.php');
global $pdo;
$pdo = new zpdo($config);
}
});
$serv->on('receive', function($serv, $fd, $fromId, $data) {
if (empty($data)) {
return;
}
$serv->task([$fd, $data]); //数据转到task进行处理
});
$serv->on('task', function($serv, $taskId, $fromId, $_data) {
list($fd, $sql) = $_data;
swoole_set_process_name("mysql proxy pool process, pid:".$serv->worker_id." sql:{$sql}");
global $pdo;
$ret = $pdo->fetchBySql($sql);
$serv->send($fd, json_encode($ret));
swoole_set_process_name("mysql proxy pool process, pid:".$serv->worker_id);
});
$serv->on('finish', function() {});
$serv->start();