Skip to content

Commit

Permalink
update logic bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaodong.li committed Jun 14, 2018
1 parent 918cdc9 commit 3d971c7
Show file tree
Hide file tree
Showing 23 changed files with 540 additions and 586 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -67,7 +67,8 @@ Colorbox is our team's another open source project. It is a client side game eng
* or you can visit [github:lordofpomelo](http://github.com/NetEase/lordofpomelo)to download the source and install it on your local machine.

## 注意事项
在log4js.json中,将所有log都关掉 否则connector-server 内存会一直飙升

在log4js.json中,将所有log都关掉或者级别设置为ERROR 否则内存会因为日志大量I/O写入一直飙升

```
log4js.json配置
Expand Down
234 changes: 118 additions & 116 deletions game-server/app.js
Expand Up @@ -8,156 +8,158 @@ var routeUtil = require('./app/util/routeUtil');
var playerFilter = require('./app/servers/area/filter/playerFilter');
var ChatService = require('./app/services/chatService');
var sync = require('pomelo-sync-plugin');
var masterhaPlugin = require('pomelo-masterha-plugin');
// var masterhaPlugin = require('pomelo-masterha-plugin');

/**
* Init app for client
*/
var app = pomelo.createApp();
app.set('name', 'lord of pomelo');

app.configure('production|development', function () {
app.before(pomelo.filters.toobusy());
app.enable('systemMonitor');

app.filter(pomelo.filters.time()); //开启conn日志,对应pomelo-admin模块下conn request
// app.rpcFilter(pomelo.rpcFilters.rpcLog());//开启rpc日志,对应pomelo-admin模块下rpc request

// var sceneInfo = require('./app/modules/sceneInfo');
var onlineUser = require('./app/modules/onlineUser');
if (typeof app.registerAdmin === 'function') {
// app.registerAdmin(sceneInfo, {app: app});
app.registerAdmin(onlineUser, {app: app});
}
});

// configure for global
app.configure('production|development', function () {
require('./app/util/httpServer');

//Set areasIdMap, a map from area id to serverId.
if (app.serverType !== 'master') {
var areas = app.get('servers').area;
var areaIdMap = {};
for (var id in areas) {
areaIdMap[areas[id].area] = areas[id].id;
}
app.set('areaIdMap', areaIdMap);
app.configure('production|development', function() {
app.before(pomelo.filters.toobusy());
app.enable('systemMonitor');
require('./app/util/httpServer');

//var sceneInfo = require('./app/modules/sceneInfo');
var onlineUser = require('./app/modules/onlineUser');
if(typeof app.registerAdmin === 'function'){
//app.registerAdmin(sceneInfo, {app: app});
app.registerAdmin(onlineUser, {app: app});
}
//Set areasIdMap, a map from area id to serverId.
if (app.serverType !== 'master') {
var areas = app.get('servers').area;
var areaIdMap = {};
for(var id in areas){
areaIdMap[areas[id].area] = areas[id].id;
}
app.set('areaIdMap', areaIdMap);
}
// proxy configures
app.set('proxyConfig', {
cacheMsg: true,
interval: 30,
lazyConnection: true
});

// remote configures
app.set('remoteConfig', {
cacheMsg: true,
interval: 30
});

// route configures
app.route('area', routeUtil.area);
app.route('connector', routeUtil.connector);

app.loadConfig('mysql', app.getBase() + '/../shared/config/mysql.json');
app.filter(pomelo.filters.timeout());

/*
// master high availability
app.use(masterhaPlugin, {
zookeeper: {
server: '127.0.0.1:2181',
path: '/pomelo/master'
}
// proxy configures
app.set('proxyConfig', {
cacheMsg: true,
interval: 30,
lazyConnection: true,
enableRpcLog: true
});

// remote configures
app.set('remoteConfig', {
cacheMsg: true,
interval: 30
});

// route configures
app.route('area', routeUtil.area);
app.route('connector', routeUtil.connector);

app.loadConfig('mysql', app.getBase() + '/../shared/config/mysql.json');
app.filter(pomelo.filters.timeout());

/*高可用插件*/
// master high availability
app.use(masterhaPlugin, {
zookeeper:
{
server: 'localhost:2181',
path: '/pomelo/master',
username: "pomelo",
password: "pomelo"
}
});
});
*/
});

// Configure for auth server
app.configure('production|development', 'auth', function () {
// load session congfigures
app.set('session', require('./config/session.json'));
app.configure('production|development', 'auth', function() {
// load session congfigures
app.set('session', require('./config/session.json'));
});

// Configure for area server
app.configure('production|development', 'area', function () {
app.filter(pomelo.filters.serial());
app.before(playerFilter());

//Load scene server and instance server
var server = app.curServer;
if (server.instance) {
instancePool.init(require('./config/instance.json'));
app.areaManager = instancePool;
} else {
scene.init(dataApi.area.findById(server.area));
app.areaManager = scene;
app.configure('production|development', 'area', function(){
app.filter(pomelo.filters.serial());
app.before(playerFilter());

//Load scene server and instance server
var server = app.curServer;
if(server.instance){
instancePool.init(require('./config/instance.json'));
app.areaManager = instancePool;
}else{
scene.init(dataApi.area.findById(server.area));
app.areaManager = scene;
/*
kill -SIGUSR2 <pid>
http://localhost:3272/inspector.html?host=localhost:9999&page=0
*/
/*
// disable webkit-devtools-agent
var areaId = parseInt(server.area);
if(areaId === 3) { // area-server-3
require('webkit-devtools-agent');
var express = require('express');
var expressSvr = express.createServer();
expressSvr.use(express.static(__dirname + '/devtools_agent_page'));
var tmpPort = 3270 + areaId - 1;
expressSvr.listen(tmpPort);
}
*/
}

//Init areaService
areaService.init();
//Init areaService
areaService.init();
});

app.configure('production|development', 'manager', function () {
var events = pomelo.events;
app.configure('production|development', 'manager', function(){
var events = pomelo.events;

app.event.on(events.ADD_SERVERS, instanceManager.addServers);
app.event.on(events.ADD_SERVERS, instanceManager.addServers);

app.event.on(events.REMOVE_SERVERS, instanceManager.removeServers);
app.event.on(events.REMOVE_SERVERS, instanceManager.removeServers);
});

// Configure database
app.configure('production|development', 'area|auth|connector|master', function () {
var dbclient = require('./app/dao/mysql/mysql').init(app);
app.set('dbclient', dbclient);
// app.load(pomelo.sync, {path:__dirname + '/app/dao/mapping', dbclient: dbclient});
app.use(sync, {sync: {path: __dirname + '/app/dao/mapping', dbclient: dbclient}});
app.configure('production|development', 'area|auth|connector|master', function() {
var dbclient = require('./app/dao/mysql/mysql').init(app);
app.set('dbclient', dbclient);
// app.load(pomelo.sync, {path:__dirname + '/app/dao/mapping', dbclient: dbclient});
app.use(sync, {sync: {path:__dirname + '/app/dao/mapping', dbclient: dbclient}});
});

app.configure('production|development', 'connector', function () {
var dictionary = app.components['__dictionary__'];
var dict = null;
if (!!dictionary) {
dict = dictionary.getDict();
}

app.set('connectorConfig',
{
connector: pomelo.connectors.hybridconnector,
heartbeat: 30,
useDict: true,
useProtobuf: true,
handshake: function (msg, cb) {
cb(null, {});
}
});
app.configure('production|development', 'connector', function(){
var dictionary = app.components['__dictionary__'];
var dict = null;
if(!!dictionary){
dict = dictionary.getDict();
}

app.set('connectorConfig',
{
connector : pomelo.connectors.hybridconnector,
heartbeat : 30,
useDict : true,
useProtobuf : true,
handshake : function(msg, cb){
cb(null, {});
}
});
});

app.configure('production|development', 'gate', function () {
app.set('connectorConfig',
{
connector: pomelo.connectors.hybridconnector,
useProtobuf: true,
handshake:function(msg,cb){
console.warn("handshake: ", msg);
cb(null,{status:200});
}
});
app.configure('production|development', 'gate', function(){
app.set('connectorConfig',
{
connector : pomelo.connectors.hybridconnector,
useProtobuf : true
});
});
// Configure for chat server
app.configure('production|development', 'chat', function () {
app.set('chatService', new ChatService(app));
app.configure('production|development', 'chat', function() {
app.set('chatService', new ChatService(app));
});

//start
app.start();

// Uncaught exception handler
process.on('uncaughtException', function (err) {
console.error(' Caught exception: ' + err.stack);
process.on('uncaughtException', function(err) {
console.error(' Caught exception: ' + err.stack);
});
80 changes: 48 additions & 32 deletions game-server/app/dao/mysql/dao-pool.js
@@ -1,33 +1,49 @@
var _poolModule = require('generic-pool');
var mysql = require('mysql');
var _poolModule = require('generic-pool');
var mysql = require('mysql');
/*
* Create mysql connection pool.
*/
var createMysqlPool = function (app) {
var mysqlConfig = app.get('mysql');

/*
* Create mysql connection pool.
*/
var createMysqlPool = function(app){
var mysqlConfig = app.get('mysql');
const factory = {
create: function(){
return new Promise(function(resolve, reject){
var client = mysql.createConnection({
host: mysqlConfig.host,
user: mysqlConfig.user,
password: mysqlConfig.password,
database: mysqlConfig.database
});
resolve(client);
});
},
destroy: function(client){
return new Promise(function(resolve){
client.on('end', function(){
resolve()
});
client.disconnect()
});
}
}
return _poolModule.createPool(factory, {max:10, min:2});
};

exports.createMysqlPool = createMysqlPool;
const factory = {
create: function () {
return new Promise(function (resolve) {
var client = mysql.createConnection({
host: mysqlConfig.host,
user: mysqlConfig.user,
password: mysqlConfig.password,
database: mysqlConfig.database,
port: mysqlConfig.port
});
client.on('error', function () {
client.connect();
});
client.connect(function (error) {
if (error) {
console.log('mysql connect error', error);
}
resolve(client)
});
})
},
destroy: function (client) {
return new Promise(function (resolve) {
client.on('end', function () {
resolve()
})
client.end()
})
}
}

var opts = {
min: 2,
max: 10,
idleTimeoutMillis: 30000,
log: true
};

return _poolModule.createPool(factory, opts);
};
exports.createMysqlPool = createMysqlPool;

0 comments on commit 3d971c7

Please sign in to comment.