Skip to content

Commit

Permalink
修改配置方式
Browse files Browse the repository at this point in the history
  • Loading branch information
JanHuang committed Jul 8, 2016
1 parent 70161e9 commit 909f315
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 78 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Created by .ignore support plugin (hsz.mobi)
/.idea
*.pid
6 changes: 6 additions & 0 deletions dora
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env php
<?php

$config = parse_ini_file(__DIR__ . '/dora.ini', true);

print_r($config);
13 changes: 13 additions & 0 deletions dora.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[tcp]
host = 127.0.0.1
port = 9566

[http]
host = 127.0.0.1
port = 9567

[group]
host[] = tcp://127.0.01:9577
host[] = tcp://127.0.01:9578

[report]
118 changes: 67 additions & 51 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
abstract class Server
{
const MASTER_PID = './dorarpc.pid';
const MANAGER_PID = './dorarpcmanager.pid';

private $tcpserver = null;
private $server = null;
Expand All @@ -22,70 +24,56 @@ abstract class Server

private $groupConfig;

//for extends class overwrite default config
//用于继承类覆盖默认配置
protected $externalConfig = array();
protected $externalHttpConfig = array();
protected $httpConfig = [
'dispatch_mode' => 3,

abstract public function initServer($server);
'package_max_length' => 1024 * 1024 * 2,
'buffer_output_size' => 1024 * 1024 * 3,
'pipe_buffer_size' => 1024 * 1024 * 32,
'open_tcp_nodelay' => 1,

final public function __construct($ip = "0.0.0.0", $port = 9567, $httpport = 9566, $groupConfig = array(), $reportConfig = array())
{
$this->server = new \swoole_http_server($ip, $httpport);
$this->tcpserver = $this->server->addListener($ip, $port, \SWOOLE_TCP);
$httpconfig = array(
'dispatch_mode' => 3,

'package_max_length' => 1024 * 1024 * 2,
'buffer_output_size' => 1024 * 1024 * 3,
'pipe_buffer_size' => 1024 * 1024 * 32,
'open_tcp_nodelay' => 1,
'heartbeat_check_interval' => 5,
'heartbeat_idle_time' => 10,
'open_cpu_affinity' => 1,

'heartbeat_check_interval' => 5,
'heartbeat_idle_time' => 10,
'open_cpu_affinity' => 1,
'reactor_num' => 32,
'worker_num' => 40,
'task_worker_num' => 20,

'reactor_num' => 32,
'worker_num' => 40,
'task_worker_num' => 20,
'max_request' => 0, //必须设置为0否则并发任务容易丢,don't change this number
'task_max_request' => 4000,

'max_request' => 0, //必须设置为0否则并发任务容易丢,don't change this number
'task_max_request' => 4000,
'backlog' => 3000,
'log_file' => '/tmp/sw_server.log',
'task_tmpdir' => '/tmp/swtasktmp/',

'backlog' => 3000,
'log_file' => '/tmp/sw_server.log',
'task_tmpdir' => '/tmp/swtasktmp/',
'daemonize' => 1,
];

'daemonize' => 1,
);
protected $tcpConfig = [
'open_length_check' => 1,
'package_length_type' => 'N',
'package_length_offset' => 0,
'package_body_offset' => 4,

$tcpconfig = array(
'open_length_check' => 1,
'package_length_type' => 'N',
'package_length_offset' => 0,
'package_body_offset' => 4,
'package_max_length' => 1024 * 1024 * 2,
'buffer_output_size' => 1024 * 1024 * 3,
'pipe_buffer_size' => 1024 * 1024 * 32,

'package_max_length' => 1024 * 1024 * 2,
'buffer_output_size' => 1024 * 1024 * 3,
'pipe_buffer_size' => 1024 * 1024 * 32,
'open_tcp_nodelay' => 1,

'open_tcp_nodelay' => 1,
'backlog' => 3000,
];

'backlog' => 3000,
);
abstract public function initServer($server);

//merge config
if (!empty($this->externalConfig)) {
$httpconfig = array_merge($httpconfig, $this->externalHttpConfig);
$tcpconfig = array_merge($tcpconfig, $this->externalConfig);
}
final public function __construct($ip = "0.0.0.0", $port = 9567, $httpport = 9566, $groupConfig = array(), $reportConfig = array())
{
$this->server = new \swoole_http_server($ip, $httpport);
$this->tcpserver = $this->server->addListener($ip, $port, \SWOOLE_TCP);

//init tcp server
$this->tcpserver->set($tcpconfig);
$this->tcpserver->on('Receive', array($this, 'onReceive'));

//init http server
$this->server->set($httpconfig);
$this->server->on('Start', array($this, 'onStart'));
$this->server->on('ManagerStart', array($this, 'onManagerStart'));

Expand All @@ -112,6 +100,34 @@ final public function __construct($ip = "0.0.0.0", $port = 9567, $httpport = 956
$this->monitorProcess = new \swoole_process(array($this, "monitorReport"));
$this->server->addProcess($this->monitorProcess);
}
}

/**
* Configuration Server.
*
* @param array $config
* @return $this
*/
public function configure(array $config)
{
if (isset($config['http'])) {
$this->httpConfig = array_merge($this->httpConfig, $config['http']);
}

if (isset($config['tcp'])) {
$this->tcpConfig = array_merge($this->tcpConfig, $config['tcp']);
}
return $this;
}

/**
* Start Server.
* @return void;
*/
public function start()
{
$this->server->set($this->httpConfig);
$this->tcpserver->set($this->tcpConfig);

$this->server->start();
}
Expand Down Expand Up @@ -251,8 +267,8 @@ final public function onStart(\swoole_server $serv)
echo "ManagerPid={$serv->master_pid}\n";
echo "Server: start.Swoole version is [" . SWOOLE_VERSION . "]\n";

file_put_contents("./dorarpc.pid", $serv->master_pid);
file_put_contents("./dorarpcmanager.pid", $serv->manager_pid);
file_put_contents(static::MASTER_PID, $serv->master_pid);
file_put_contents(static::MANAGER_PID, $serv->manager_pid);

}

Expand Down
54 changes: 27 additions & 27 deletions test/demoserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,6 @@
class Server extends \DoraRPC\Server
{

//all of this config for optimize performance
//以下配置为优化服务性能用,请实际压测调试
protected $externalConfig = array(

//to improve the accept performance ,suggest the number of cpu X 2
//如果想提高请求接收能力,更改这个,推荐cpu个数x2
'reactor_num' => 16,

//packet decode process,change by condition
//包处理进程,根据情况调整数量
'worker_num' => 30,

//the number of task logical process progcessor run you business code
//实际业务处理进程,根据需要进行调整
'task_worker_num' => 200,

'daemonize' => false,

'log_file' => '/tmp/sw_server.log',

'task_tmpdir' => '/tmp/swtasktmp/',
);

protected $externalHttpConfig = array(
'daemonize' => false,
);

function initServer($server)
{
//the callback of the server init 附加服务初始化
Expand Down Expand Up @@ -77,3 +50,30 @@ function initTask($server, $worker_id)
);
//ok start server
$res = new Server("0.0.0.0", 9567, 9566, $groupConfig, $redisconfig);

$res->configure([
'tcp' => [
//to improve the accept performance ,suggest the number of cpu X 2
//如果想提高请求接收能力,更改这个,推荐cpu个数x2
'reactor_num' => 16,

//packet decode process,change by condition
//包处理进程,根据情况调整数量
'worker_num' => 30,

//the number of task logical process progcessor run you business code
//实际业务处理进程,根据需要进行调整
'task_worker_num' => 200,

'daemonize' => false,

'log_file' => '/tmp/sw_server.log',

'task_tmpdir' => '/tmp/swtasktmp/',
],
'http' => [
'daemonize' => false,
]
]);

$res->start();

0 comments on commit 909f315

Please sign in to comment.