Skip to content

Commit

Permalink
fix a memory leak & a serious server render bug
Browse files Browse the repository at this point in the history
  • Loading branch information
brandnewera committed Nov 20, 2013
1 parent e3d9346 commit 4c94658
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 143 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sumeru",
"version": "0.8.18",
"version": "0.8.19",
"description": "A Realtime Javascript RIA Framework For Mobile WebApp",
"main": "app.js",
"scripts": {
Expand Down
18 changes: 10 additions & 8 deletions sumeru/build/baeConfig/bae.js.template
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/*$
Do not make any changes, otherwise it will lead to build failure for bae.
$*/
if(typeof process != 'undefined' && process.BAE){
sumeru.config.database({
dbname : 'your_bae_db_name'
});
sumeru.config({
site_url : 'http://yourapp.duapp.com/'
});
}
if(sumeru.BAE_VERSION){
sumeru.config.database({
dbname : 'your_bae_db_name',
user: 'your_bae_pk',//bae 3.0 required
password: 'your_bae_sk',//bae 3.0 required
});
sumeru.config({
site_url : 'http://yourapp.duapp.com', //with tailing slash
});
}
33 changes: 6 additions & 27 deletions sumeru/server/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ var path = require('path'),
res.end();
} else {
//进行view partial的拼装,处理include逻辑
if(!view_from_cache && (filePath.indexOf('/view/') != -1 || filePath.indexOf('/viewEngine/') != -1)){
if(filePath.indexOf('/view/') != -1 || filePath.indexOf('/viewEngine/') != -1){
if (view_from_cache){//不拼装,直接返回
res.writeHead(200, {'Content-Type' : contentType});
res.end(entireContent, 'utf-8');
return ;
}
var asyncPartialMap = {},
//partialCount = 0,
//检测循环引用
Expand All @@ -209,32 +214,6 @@ var path = require('path'),
var commentRegExp = /(<!--([\s\S]*?)-->)/mg; // means <!--xxx-->
fw.dev('view remove comment', content.match(commentRegExp));

/*
var tplRoleOpenPart = /<!--([\s]*tpl-role[\s]*=[\s]*[\s\S]*?)-->/g,
tplRoleClosePart = /<!--([\s]*\/tpl-role[\s]*)-->/g;
entireContent = entireContent.replace(tplRoleRegExp, function($0){
//先替换close的
$0 = $0.replace(tplRoleClosePart, function($0, $1){
return '!!--' + $1 + '--!!';
});
$0 = $0.replace(tplRoleOpenPart,function($0, $1){
return '!!--' + $1 + '--!!';
});
return $0;
});
content = content.replace(tplRoleRegExp, function($0){
$0 = $0.replace(tplRoleClosePart, function($0, $1){
return '!!--' + $1 + '--!!';
});
$0 = $0.replace(tplRoleOpenPart,function($0, $1){
return '!!--' + $1 + '--!!';
});
return $0;
});
*/

entireContent = entireContent.replace(commentRegExp, '');
content = content.replace(commentRegExp, '');

Expand Down
11 changes: 9 additions & 2 deletions sumeru/server/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -1201,9 +1201,16 @@ var runStub = function(db) {
version : dataVersion
};

var cmd = deltaFlag ? 'data_write_from_server_delta' : 'data_write_from_server';

if(pubRecord.extPublish){
var config = externalConfig && externalConfig[pubname];
if(config){
params.external = {
uniqueColumn : config.uniqueColumn || config.keyColume || "",
};
}
}

var cmd = deltaFlag ? 'data_write_from_server_delta' : 'data_write_from_server';

netMessage.sendMessage(params, cmd, socketId, function(err){
fw.log('send data_write_from_server fail ' + err , socketId);
Expand Down
176 changes: 106 additions & 70 deletions sumeru/src/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ var runnable = function(fw, findDiff, publishBaseDir, externalConfig, http, serv
}

//在各种post成功后,更新本地数据
function _updateLocalData(modelName, pubName, url, type, data){
/* function _updateLocalData(modelName, pubName, url, type, data){
var localData = localDataMgr[url];
Expand All @@ -239,7 +239,7 @@ var runnable = function(fw, findDiff, publishBaseDir, externalConfig, http, serv
//抓取回来以后会自动update, 这里不用做localUpdate
}
}
} */

/**
* @method _resolve: resolve fetched originData to Array. 处理抓取的原始数据
Expand Down Expand Up @@ -316,6 +316,8 @@ var runnable = function(fw, findDiff, publishBaseDir, externalConfig, http, serv
* @param {String} modelName : name of model
* @param {String} pubName : publish name
* @param {String} url : external data source url
* @param {Function} callback : publish callback
* @param {Function} afterSync : after _doGet response from 3rd-party server.
*/
function _sync(modelName, pubName, url, callback, afterSync){

Expand Down Expand Up @@ -441,7 +443,7 @@ var runnable = function(fw, findDiff, publishBaseDir, externalConfig, http, serv

var suffix = 'Url';
var opts;

args = args.concat(); //copy args
if(config.postUrl){
Array.prototype.unshift.call(args, type);
opts = config.postUrl.apply(null, args);
Expand Down Expand Up @@ -497,8 +499,6 @@ var runnable = function(fw, findDiff, publishBaseDir, externalConfig, http, serv
data = buffer ? data : data.toString();
fw.netMessage.sendMessage(data,cbn,conn._sumeru_socket_id);
});


}
}
});
Expand Down Expand Up @@ -592,6 +592,7 @@ var runnable = function(fw, findDiff, publishBaseDir, externalConfig, http, serv

//generate postData and options by developers' config.
var config = externalConfig[pubName];
args = args.concat(); //copy args
Array.prototype.pop.call(args); //remove callback
var d = _getPostData(config, type, smrdata, modelName, pubName),
opt = _getPostOptions(config, type, args);
Expand All @@ -612,7 +613,7 @@ var runnable = function(fw, findDiff, publishBaseDir, externalConfig, http, serv
_doPost(opts, postData, function(data){
//成功的情况下,重新拉取数据
urlMgr[modelName].forEach(function(refetchurl){
_updateLocalData(modelName, pubName, refetchurl, type, smrdata);
//_updateLocalData(modelName, pubName, refetchurl, type, smrdata);
_sync(modelName, pubName, refetchurl, function(){}); //POST完成后重新抓取三方数据,trigger_push不用主动callback
});

Expand All @@ -631,26 +632,33 @@ var runnable = function(fw, findDiff, publishBaseDir, externalConfig, http, serv
* @param {Function} cb: get callback, result for getData;
*/
function sendGetRequest(url, cb, buffer){

if(!url || !cb){ fw.log('Please specify url and callback for sumeru.external.get!');}
var cbn = "WAITING_EXTERNAL_GET_CALLBACK_" + fw.utils.randomStr(8);

fw.netMessage.setReceiver({
onMessage : {
target : cbn,
overwrite: true,
once:true,
handle : function(data){
cb(data);
}
}
});

fw.netMessage.sendMessage({
cbn : cbn,
url : url,
buffer : buffer
}, "SEND_EXTERNAL_GET");
//server
if(fw.IS_SUMERU_SERVER){
_doGet(url, function(data){
data = buffer ? data : data.toString();
cb(data);
});
} else { //client
if(!url || !cb){ fw.log('Please specify url and callback for sumeru.external.get!');}
var cbn = "WAITING_EXTERNAL_GET_CALLBACK_" + fw.utils.randomStr(8);

fw.netMessage.setReceiver({
onMessage : {
target : cbn,
overwrite: true,
once:true,
handle : function(data){
cb(data);
}
}
});

fw.netMessage.sendMessage({
cbn : cbn,
url : url,
buffer : buffer
}, "SEND_EXTERNAL_GET");
}

}

Expand All @@ -663,28 +671,46 @@ var runnable = function(fw, findDiff, publishBaseDir, externalConfig, http, serv
* @param {Function} cb: post callback, result for;
*/
function sendPostRequest(options, postData, cb){
//server
if(fw.IS_SUMERU_SERVER){
postData = encodeURIComponent(JSON.stringify(postData));
var defaultOptions = {
method : 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};

if(!options || !postData){fw.log("please specify options or postData for sumeru.external.post");return false;}
cb = cb || function(){};

var cbn = "WAITING_EXTERNAL_POST_CALLBACK_" + fw.utils.randomStr(8);

fw.netMessage.setReceiver({
onMessage : {
target : cbn,
overwrite: true,
once:true,
handle : function(data){
cb(data);
}
}
});

fw.netMessage.sendMessage({
cbn : cbn,
options : options,
postData : postData
}, "SEND_EXTERNAL_POST");
var opts = Library.objUtils.extend(true, defaultOptions, options);

_doPost(opts, postData, function(data){
cb(data);
});

} else { //client
if(!options || !postData){fw.log("please specify options or postData for sumeru.external.post");return false;}
cb = cb || function(){};

var cbn = "WAITING_EXTERNAL_POST_CALLBACK_" + fw.utils.randomStr(8);

fw.netMessage.setReceiver({
onMessage : {
target : cbn,
overwrite: true,
once:true,
handle : function(data){
cb(data);
}
}
});

fw.netMessage.sendMessage({
cbn : cbn,
options : options,
postData : postData
}, "SEND_EXTERNAL_POST");
}

}

Expand All @@ -699,30 +725,40 @@ var runnable = function(fw, findDiff, publishBaseDir, externalConfig, http, serv
*/
function synchronize(modelName, pubName, url, cb){

if(arguments.length < 3){
fw.log("please sepecify modelName, pubName and url in order.");
return false;
}

var cbn = "WAITING_SYNC_CALLBACK_" + fw.utils.randomStr(8);
if(fw.IS_SUMERU_SERVER){
var urls = urlMgr[modelName];
var config = externalConfig[pubName];

_sync(modelName, pubName, url, function(){}, function(){
cb({msg:"ok"});
});
} else { //client
if(arguments.length < 3){
fw.log("please sepecify modelName, pubName and url in order.");
return false;
}

fw.netMessage.setReceiver({
onMessage : {
target : cbn,
overwrite: true,
once:true,
handle : function(data){
cb && cb(data);
}
}
});

fw.netMessage.sendMessage({
cbn : cbn,
modelName : modelName,
pubName : pubName,
url : url
}, "SEND_SYNC_REQUEST");
var cbn = "WAITING_SYNC_CALLBACK_" + fw.utils.randomStr(8);

fw.netMessage.setReceiver({
onMessage : {
target : cbn,
overwrite: true,
once:true,
handle : function(data){
cb && cb(data);
}
}
});

fw.netMessage.sendMessage({
cbn : cbn,
modelName : modelName,
pubName : pubName,
url : url
}, "SEND_SYNC_REQUEST");
}


}

Expand Down
23 changes: 23 additions & 0 deletions sumeru/src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ var runnable = function(sumeru){
sumeru.__reg('dev', log.dev);
sumeru.__reg('ConfigLogTrace', configLogTrace);

var logtime_container = {};
sumeru.__reg('logtimeclear', function(uniqueid){
delete logtime_container[uniqueid];
});
sumeru.__reg('logtime', function(uniqueid,word){
if (typeof uniqueid != 'string' || uniqueid.match(/\w{11}/) === null) {
console.log(uniqueid,word);
return ;
}
var timediff = 0;
if (logtime_container[uniqueid]){
timediff = (new Date()).getTime() - logtime_container[uniqueid];
}
logtime_container[uniqueid] = (new Date()).getTime();

_log("[" + uniqueid+'] [' + timediff + ']' + word);
});
sumeru.__reg('logerror', function(){
arguments[0] = formatDate() + arguments[0];
console && console.error.apply(this, arguments);
});


return log;
};

Expand Down
Loading

0 comments on commit 4c94658

Please sign in to comment.