Skip to content

Commit

Permalink
gateway/worker redis support
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed May 7, 2015
1 parent dedc378 commit e2531f1
Show file tree
Hide file tree
Showing 26 changed files with 443 additions and 87 deletions.
18 changes: 18 additions & 0 deletions GatewayWorker/BusinessWorker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker;

use Workerman\Connection\TcpConnection;

use \Workerman\Worker;
use \Workerman\Connection\AsyncTcpConnection;
use \Workerman\Protocols\GatewayProtocol;
Expand Down Expand Up @@ -185,6 +199,10 @@ public function checkGatewayConnections()
$gateway_connection->onMessage = array($this, 'onGatewayMessage');
$gateway_connection->onClose = array($this, 'onClose');
$gateway_connection->onError = array($this, 'onError');
if(TcpConnection::$defaultMaxSendBufferSize == $gateway_connection->maxSendBufferSize)
{
$gateway_connection->maxSendBufferSize = 10*1024*1024;
}
$gateway_connection->connect();
$this->_connectingGatewayAddress[$addr] = 1;
}
Expand Down
35 changes: 28 additions & 7 deletions GatewayWorker/Gateway.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker;

use Workerman\Connection\TcpConnection;

use \Workerman\Worker;
use \Workerman\Lib\Timer;
use \Workerman\Protocols\GatewayProtocol;
Expand Down Expand Up @@ -171,7 +185,7 @@ public function run()
*/
public function onClientMessage($connection, $data)
{
$connection->pingNotResponseCount = 0;
$connection->pingNotResponseCount = -1;
$this->sendToWorker(GatewayProtocol::CMD_ON_MESSAGE, $connection, $data);
}

Expand All @@ -195,7 +209,7 @@ public function onClientConnect($connection)
// 连接的session
$connection->session = '';
// 该连接的心跳参数
$connection->pingNotResponseCount = 0;
$connection->pingNotResponseCount = -1;
// 保存客户端连接connection对象
$this->_clientConnections[$connection->globalClientId] = $connection;
// 保存该连接的内部gateway通讯地址
Expand Down Expand Up @@ -353,7 +367,7 @@ protected function createGlobalClientId()
public function onWorkerStart()
{
// 分配一个内部通讯端口
$this->lanPort = $this->startPort - posix_getppid() + posix_getpid();
$this->lanPort = function_exists('posix_getppid') ? $this->startPort - posix_getppid() + posix_getpid() : $this->startPort;
if($this->lanPort<0 || $this->lanPort >=65535)
{
$this->lanPort = rand($this->startPort, 65535);
Expand Down Expand Up @@ -403,6 +417,10 @@ public function onWorkerStart()
public function onWorkerConnect($connection)
{
$connection->remoteAddress = $connection->getRemoteIp().':'.$connection->getRemotePort();
if(TcpConnection::$defaultMaxSendBufferSize == $connection->maxSendBufferSize)
{
$connection->maxSendBufferSize = 10*1024*1024;
}
$this->_workerConnections[$connection->remoteAddress] = $connection;
}

Expand Down Expand Up @@ -518,7 +536,7 @@ protected function registerAddress()
Lock::release();
if(get_class($store) == 'Memcached')
{
$msg = " registerAddress fail : " . $store->getResultMessage();
$msg = " registerAddress fail : Memcache Error " . $store->getResultMessage();
}
$this->log($msg);
return false;
Expand Down Expand Up @@ -583,10 +601,13 @@ public function ping()
$connection->destroy();
continue;
}
$connection->pingNotResponseCount++;
if($this->pingData)
// $connection->pingNotResponseCount为-1说明最近客户端有发来消息,则不给客户端发送心跳
if($connection->pingNotResponseCount++ >= 0)
{
$connection->send($this->pingData);
if($this->pingData)
{
$connection->send($this->pingData);
}
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion GatewayWorker/Lib/Context.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker\Lib;

/**
* 上下文 包含当前用户uid, 内部通信local_ip local_port socket_id ,以及客户端client_ip client_port
* @author walkor
*/
class Context
{
Expand Down
14 changes: 13 additions & 1 deletion GatewayWorker/Lib/Db.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker\Lib;

/**
* 数据库类
* @author walkor <[email protected]>
*/
class Db
{
Expand Down
16 changes: 13 additions & 3 deletions GatewayWorker/Lib/Gateway.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker\Lib;

/**
*
* 数据发送相关
* @author walkor <[email protected]>
*
*/
use \Workerman\Protocols\GatewayProtocol;
use \GatewayWorker\Lib\Store;
Expand Down
12 changes: 12 additions & 0 deletions GatewayWorker/Lib/Lock.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker\Lib;
/**
* 锁
Expand Down
34 changes: 33 additions & 1 deletion GatewayWorker/Lib/Store.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker\Lib;

/**
* 存储类
* 这里用memcache实现
* @author walkor <[email protected]>
*/
class Store
{
Expand Down Expand Up @@ -52,6 +64,26 @@ public static function instance($config_name)
}
return self::$instance[$config_name];
}
// redis 驱动
elseif(\Config\Store::$driver == \Config\Store::DRIVER_REDIS)
{
if(!isset(\Config\Store::$$config_name))
{
echo "\\Config\\Store::$config_name not set\n";
throw new \Exception("\\Config\\Store::$config_name not set\n");
}
if(!isset(self::$instance[$config_name]))
{
self::$instance[$config_name] = new \GatewayWorker\Lib\StoreDriver\Redis();
// 只选择第一个ip作为服务端
$address = current(\Config\Store::$$config_name);
list($ip, $port) = explode(':', $address);
$timeout = 1;
self::$instance[$config_name]->connect($ip, $port, $timeout);
self::$instance[$config_name]->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_IGBINARY);
}
return self::$instance[$config_name];
}
// 文件驱动
else
{
Expand Down
15 changes: 12 additions & 3 deletions GatewayWorker/Lib/StoreDriver/File.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker\Lib\StoreDriver;

/**
*
* 这里用php数组文件来存储数据,
* 为了获取高性能需要用类似memcache的存储
* @author walkor <[email protected]>
*
*/

class File
Expand Down
26 changes: 26 additions & 0 deletions GatewayWorker/Lib/StoreDriver/Redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker\Lib\StoreDriver;

/**
* Redis
*/

class Redis extends \Redis
{
public function increment($key)
{
return parent::incr($key);
}
}
12 changes: 12 additions & 0 deletions Workerman/Autoloader.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Workerman;

// 定义Workerman根目录
Expand Down
14 changes: 13 additions & 1 deletion Workerman/Connection/AsyncTcpConnection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Workerman\Connection;

use Workerman\Events\Libevent;
Expand All @@ -9,7 +21,6 @@

/**
* 异步tcp连接类
* @author walkor<[email protected]>
*/
class AsyncTcpConnection extends TcpConnection
{
Expand Down Expand Up @@ -52,6 +63,7 @@ public function __construct($remote_address)
$this->id = self::$_idRecorder++;
// 统计数据
self::$statistics['connection_count']++;
$this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
}

public function connect()
Expand Down
14 changes: 13 additions & 1 deletion Workerman/Connection/ConnectionInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Workerman\Connection;

use Workerman\Events\Libevent;
use Workerman\Events\Select;
use Workerman\Events\EventInterface;
Expand All @@ -8,7 +21,6 @@

/**
* connection类的接口
* @author walkor<[email protected]>
*/
abstract class ConnectionInterface
{
Expand Down
Loading

0 comments on commit e2531f1

Please sign in to comment.