Skip to content

Instantly share code, notes, and snippets.

@bxshi
Created April 19, 2013 05:46
Show Gist options
  • Save bxshi/5418370 to your computer and use it in GitHub Desktop.
Save bxshi/5418370 to your computer and use it in GitHub Desktop.
This is used for set timeout for resources
/**
* Created with JetBrains WebStorm.
* User: Baoxu Shi
* Date: 13-4-19
* Time: PM12:09
* To change this template use File | Settings | File Templates.
*/
var uuidGen = require('./uuidGenerator.js');
var resourceTimeoutManager = function(pool, timeoutThreshold, type){
this.resource = {};
this.timeoutThreshold = timeoutThreshold ? timeoutThreshold : 10000; //default timeout is 10 second
this.type = type;
this.pool = pool;
this.intervalId = setInterval(function(rTM){
for(var key in rTM.resource){
if(new Date() - rTM.resource[key].time >= 10000){
switch(rTM.type){
case 'mysql' :
console.log('exceed timeout, delete it');
rTM.pool.destroy(rTM.resource[key].obj);
rTM.resource[key].obj.destroy();
delete rTM.resource[key];
break;
}
}else{
console.log('not exceed timeout');
}
}
}, this.timeoutThreshold, this);
};
var p = resourceTimeoutManager.prototype;
p.setResource = function(obj){
var uid = uuidGen();
if(this.resource[uid] != null){
return -1;
}
this.resource[uid] = {
'obj' : obj,
'time' : new Date()
};
return uid;
};
p.delResource = function(uid){
if(this.resource[uid] != null){
delete this.resource[uid];
return true;
}else{
return false;
}
};
module.exports = resourceTimeoutManager;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment