Commit 5df13214 authored by mercury233's avatar mercury233

fix memory usage

parent 6e57e1f5
......@@ -2,12 +2,26 @@ _ = require 'underscore'
_.str = require 'underscore.string'
_.mixin(_.str.exports());
spawn = require('child_process').spawn
spawnSync = require('child_process').spawnSync
ygopro = require './ygopro.js'
bunyan = require 'bunyan'
settings = require './config.json'
log = bunyan.createLogger name: "mycard-room"
#获取可用内存
get_memory_usage = ()->
prc_free = spawnSync("free", [])
lines = prc_free.stdout.toString().split(/\n/g)
line = lines[1].split(/\s+/)
total = parseInt(line[1], 10)
free = parseInt(line[3], 10)
buffers = parseInt(line[5], 10)
cached = parseInt(line[6], 10)
actualFree = free + buffers + cached
percentUsed = parseFloat(((1 - (actualFree / total)) * 100).toFixed(2))
return percentUsed
class Room
#name
#port
......@@ -19,7 +33,12 @@ class Room
@all = []
@find_or_create_by_name: (name)->
@find_by_name(name) ? new Room(name)
if room = @find_by_name(name)
return room
else if get_memory_usage()>=90
return null
else
return new Room(name)
@find_by_name: (name)->
result = _.find @all, (room)->
......
// Generated by CoffeeScript 1.9.3
(function() {
var Room, _, bunyan, log, settings, spawn, ygopro;
var Room, _, bunyan, get_memory_usage, log, settings, spawn, spawnSync, ygopro;
_ = require('underscore');
......@@ -10,6 +10,8 @@
spawn = require('child_process').spawn;
spawnSync = require('child_process').spawnSync;
ygopro = require('./ygopro.js');
bunyan = require('bunyan');
......@@ -20,12 +22,32 @@
name: "mycard-room"
});
get_memory_usage = function() {
var actualFree, buffers, cached, free, line, lines, percentUsed, prc_free, total;
prc_free = spawnSync("free", []);
lines = prc_free.stdout.toString().split(/\n/g);
line = lines[1].split(/\s+/);
total = parseInt(line[1], 10);
free = parseInt(line[3], 10);
buffers = parseInt(line[5], 10);
cached = parseInt(line[6], 10);
actualFree = free + buffers + cached;
percentUsed = parseFloat(((1 - (actualFree / total)) * 100).toFixed(2));
return percentUsed;
};
Room = (function() {
Room.all = [];
Room.find_or_create_by_name = function(name) {
var ref;
return (ref = this.find_by_name(name)) != null ? ref : new Room(name);
var room;
if (room = this.find_by_name(name)) {
return room;
} else if (get_memory_usage() >= 90) {
return null;
} else {
return new Room(name);
}
};
Room.find_by_name = function(name) {
......
......@@ -16,7 +16,7 @@ request = require 'request'
bunyan = require 'bunyan'
heapdump = require 'heapdump'
#heapdump = require 'heapdump'
#配置文件
settings = require './config.json'
......@@ -221,15 +221,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
code: 2
}
client.end()
else if (os.freemem() / os.totalmem())<=0.1
ygopro.stoc_send_chat(client,"服务器已经爆满,请稍候再试")
ygopro.stoc_send client, 'ERROR_MSG',{
msg: 1
code: 2
}
client.end()
else if !Room.validate(info.pass)
#ygopro.stoc_send client, 'ERROR_MSG',{
# msg: 1
......@@ -252,7 +244,14 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
else
#log.info 'join_game',info.pass, client.name
client.room = Room.find_or_create_by_name(info.pass)
if client.room.started
if !client.room
ygopro.stoc_send_chat(client,"服务器已经爆满,请稍候再试")
ygopro.stoc_send client, 'ERROR_MSG',{
msg: 1
code: 2
}
client.end()
else if client.room.started
if settings.modules.post_start_watching
client.is_post_watcher = true
ygopro.stoc_send_chat_to_room client.room, "#{client.name} 加入了观战"
......
// Generated by CoffeeScript 1.9.3
(function() {
var Room, _, bunyan, debug, dialogues, execFile, fs, heapdump, http, http_server, log, net, os, path, request, settings, tips, url, ygopro;
var Room, _, bunyan, debug, dialogues, execFile, fs, http, http_server, log, net, os, path, request, settings, tips, url, ygopro;
net = require('net');
......@@ -26,8 +26,6 @@
bunyan = require('bunyan');
heapdump = require('heapdump');
settings = require('./config.json');
ygopro = require('./ygopro.js');
......@@ -231,13 +229,6 @@
code: 2
});
client.end();
} else if ((os.freemem() / os.totalmem()) <= 0.1) {
ygopro.stoc_send_chat(client, "服务器已经爆满,请稍候再试");
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
client.end();
} else if (!Room.validate(info.pass)) {
ygopro.stoc_send_chat(client, "房间密码不正确");
ygopro.stoc_send(client, 'ERROR_MSG', {
......@@ -253,7 +244,14 @@
client.end();
} else {
client.room = Room.find_or_create_by_name(info.pass);
if (client.room.started) {
if (!client.room) {
ygopro.stoc_send_chat(client, "服务器已经爆满,请稍候再试");
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
client.end();
} else if (client.room.started) {
if (settings.modules.post_start_watching) {
client.is_post_watcher = true;
ygopro.stoc_send_chat_to_room(client.room, client.name + " 加入了观战");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment