Comparing version 1.0.0-beta.10 to 1.0.0-beta.11
# Change Log | ||
## [1.0.0-beta.11] - 03-08-2017 | ||
### Fixed | ||
- Use of `CHECK_TOKENS` server flag to reduce overhead of pulling token when not needed | ||
- Change ping to touch | ||
- Re-estabilish connection if remote disconnected and dispatch all the logs | ||
- Updated touch threshold to 2 minutes | ||
## [1.0.0-beta.10] | ||
@@ -4,0 +11,0 @@ ### Fixed |
{ | ||
"name": "residue", | ||
"version": "1.0.0-beta.10", | ||
"version": "1.0.0-beta.11", | ||
"description": "Logging client for real-time centralized logging server.", | ||
@@ -5,0 +5,0 @@ "main": "src/residue.js", |
@@ -39,11 +39,15 @@ // | ||
function namedFunc() { | ||
logger.info('this is from named func'); | ||
logger.debug('this is from named func'); | ||
} | ||
app.get('/blah', function (req, res) { | ||
app.get('/sample-app', function (req, res) { | ||
logger.info('using sample-app logger'); | ||
defaultLogger.info('using default logger'); | ||
res.send('Hello Blah!'); | ||
res.send('Hello sample-app logger!'); | ||
}); | ||
app.get('/default', function (req, res) { | ||
defaultLogger.info('using default logger'); | ||
res.send('Hello default logger!'); | ||
}); | ||
app.get('/', function (req, res) { | ||
@@ -50,0 +54,0 @@ defaultLogger.info('using default logger'); |
@@ -7,3 +7,3 @@ // | ||
// takes care of expired tokens and clients and keep itself updated | ||
// with latest tokens and ping server when needed to stay alive. | ||
// with latest tokens and touch server when needed to stay alive. | ||
// | ||
@@ -39,5 +39,2 @@ // https://github.com/muflihun/residue | ||
// whether connection is being made (enabled at connection_socket, disabled at logging_socket) | ||
connecting: false, | ||
// list of tokens currently available | ||
@@ -53,3 +50,3 @@ tokens: [], | ||
debugging: false, | ||
verboseLevel: 8, | ||
verboseLevel: 6, | ||
@@ -80,3 +77,3 @@ // Status for sockets | ||
Acknowledgement: 2, | ||
Ping: 3 | ||
Touch: 3 | ||
}; | ||
@@ -98,2 +95,3 @@ | ||
ALLOW_UNKNOWN_LOGGERS: 1, | ||
CHECK_TOKENS: 2, | ||
ALLOW_DEFAULT_ACCESS_CODE: 4, | ||
@@ -107,3 +105,3 @@ ALLOW_PLAIN_LOG_REQUEST: 8, | ||
const DEFAULT_ACCESS_CODE = 'default'; | ||
const PING_THRESHOLD = 60; | ||
const TOUCH_THRESHOLD = 120; // should always be min(client_age) | ||
@@ -246,3 +244,4 @@ // Utility static functions | ||
} catch (err) { | ||
Utils.debugLog(err); | ||
Utils.vLog(9, 'decrypt-error: '); | ||
Utils.vLog(9, err); | ||
} | ||
@@ -300,6 +299,10 @@ | ||
const dataJson = JSON.parse(decryptedData.toString()); | ||
Utils.debugLog('Connection: '); | ||
Utils.debugLog(dataJson); | ||
Utils.vLog(8, 'Connection: '); | ||
Utils.vLog(8, dataJson); | ||
if (dataJson.status === 0 && typeof dataJson.key !== 'undefined' && dataJson.ack === 0) { | ||
Utils.debugLog('Connecting to Residue Server...(step 2)'); | ||
Utils.debugLog('Connecting to Residue Server...(ack)'); | ||
// connection re-estabilished | ||
Params.disconnected_by_remote = false; | ||
Params.connection = dataJson; | ||
@@ -314,3 +317,3 @@ // Need to acknowledge | ||
} else if (dataJson.status === 0 && typeof dataJson.key !== 'undefined' && dataJson.ack === 1) { | ||
Utils.debugLog('Connecting to Residue Server...(step 3)'); | ||
Utils.debugLog('Estabilising full connection...'); | ||
Params.connection = dataJson; | ||
@@ -324,6 +327,8 @@ Params.connected = true; | ||
Utils.vLog(8, `Token socket: ${Params.token_socket.address().port}`); | ||
Utils.debugLog('Obtaining tokens...'); | ||
Params.options.access_codes.forEach(function(item) { | ||
obtainToken(item.logger_id, item.code); | ||
}); | ||
if (Utils.hasFlag(Flag.CHECK_TOKENS)) { | ||
Utils.debugLog('Obtaining tokens...'); | ||
Params.options.access_codes.forEach(function(item) { | ||
obtainToken(item.logger_id, item.code); | ||
}); | ||
} | ||
}); | ||
@@ -357,2 +362,3 @@ } | ||
Utils.log(dataJson); | ||
Params.connecting = false; | ||
} | ||
@@ -363,2 +369,7 @@ }); | ||
Params.connection_socket.on('close', function() { | ||
Utils.log('Remote connection closed!'); | ||
if (Params.connected) { | ||
Params.disconnected_by_remote = true; | ||
} | ||
disconnect(); | ||
}); | ||
@@ -382,3 +393,3 @@ | ||
const dataJson = JSON.parse(decryptedData.toString()); | ||
Utils.debugLog('Decoded json successfully'); | ||
Utils.vLog(7, 'Decoded json successfully'); | ||
if (dataJson.status === 0) { | ||
@@ -391,4 +402,4 @@ dataJson.dateCreated = Utils.now(); | ||
} | ||
Utils.debugLog('New token: '); | ||
Utils.debugLog(dataJson); | ||
Utils.vLog(8, 'New token: '); | ||
Utils.vLog(8, dataJson); | ||
const callbacksCount = Params.token_socket_callbacks.length; | ||
@@ -413,2 +424,3 @@ Utils.debugLog('Token callbacks: ' + callbacksCount); | ||
Params.token_socket.on('close', function() { | ||
Params.token_socket_connected = false; | ||
}); | ||
@@ -418,2 +430,3 @@ | ||
Params.logging_socket.on('close', function() { | ||
Params.logging_socket_connected = false; | ||
}); | ||
@@ -433,9 +446,3 @@ | ||
} | ||
/* | ||
// after implementing token_request_queue lock check here shouldn't be needed! | ||
if (Params.locks[Params.token_socket.address().port]) { | ||
Utils.debugLog('Already locked'); | ||
return; | ||
} | ||
*/ | ||
if (Params.token_request_queue.indexOf(loggerId) !== -1) { | ||
@@ -493,5 +500,5 @@ Utils.debugLog('Token already requested for [' + loggerId + ']'); | ||
shouldSendPing = function() { | ||
shouldTouch = function() { | ||
if (!Params.connected || Params.connecting) { | ||
// Can't send ping | ||
// Can't touch | ||
return false; | ||
@@ -503,12 +510,12 @@ } | ||
} | ||
return Params.connection.age - (Utils.now() - Params.connection.date_created) < PING_THRESHOLD; | ||
return Params.connection.age - (Utils.now() - Params.connection.date_created) < TOUCH_THRESHOLD; | ||
} | ||
sendPing = function() { | ||
touch = function() { | ||
if (Params.connected) { | ||
if (isClientValid()) { | ||
Utils.debugLog('Pinging...'); | ||
Utils.debugLog('Touching...'); | ||
const request = { | ||
_t: Utils.getTimestamp(), | ||
type: ConnectType.Ping, | ||
type: ConnectType.Touch, | ||
client_id: Params.connection.client_id | ||
@@ -518,3 +525,3 @@ }; | ||
} else { | ||
Utils.log('Could not ping, client already dead ' + (Params.connection.date_created + Params.connection.age) + ' < ' + Utils.now()); | ||
Utils.log('Could not touch, client already dead ' + (Params.connection.date_created + Params.connection.age) + ' < ' + Utils.now()); | ||
} | ||
@@ -539,2 +546,5 @@ } | ||
hasValidToken = function(loggerId) { | ||
if (!Utils.hasFlag(Flag.CHECK_TOKENS)) { | ||
return true; | ||
} | ||
let t = Params.tokens[loggerId]; | ||
@@ -551,6 +561,14 @@ return typeof t !== 'undefined' && (t.life === 0 || Utils.now() - t.dateCreated < t.life); | ||
// Send log request to the server. No response is expected | ||
sendLogRequest = function(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel, callbackDepth) { | ||
sendLogRequest = function(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel, logDatetime) { | ||
let datetime = logDatetime; | ||
if (typeof datetime === 'undefined') { | ||
datetime = Params.options.utc_time ? getCurrentTimeUTC() : new Date().getTime(); | ||
if (Params.options.time_offset) { | ||
datetime += (1000 * Params.options.time_offset); // offset is in seconds | ||
} | ||
} | ||
if (Params.connecting) { | ||
Utils.debugLog('Still connecting...'); | ||
Params.logging_socket_callbacks.push(function() { | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel); | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel, datetime); | ||
}); | ||
@@ -562,2 +580,16 @@ return; | ||
Utils.log('Not connected to the server yet'); | ||
if (Params.disconnected_by_remote) { | ||
Utils.debugLog('Queueing...'); | ||
Params.logging_socket_callbacks.push(function() { | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel, datetime); | ||
}); | ||
const totalListener = Params.connection_socket.listenerCount('connect'); | ||
if (totalListener >= 1) { | ||
Utils.log('Checking for connection...' + totalListener); | ||
Params.connection_socket.emit('connect'); | ||
} else { | ||
Utils.log('Retrying to connect...'); | ||
connect(Params.options); | ||
} | ||
} | ||
return; | ||
@@ -570,15 +602,6 @@ } | ||
Utils.debugLog('Sending log from requeued token callback... [' + loggerId + ']'); | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel, 1); | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel, datetime); | ||
}); | ||
return; | ||
} | ||
/* | ||
if (typeof callbackDepth === 'undefined') { | ||
callbackDepth = 1; | ||
} | ||
if (callbackDepth > 2) { | ||
Utils.log('Ignoring log request from callback #' + callbackDepth); | ||
return; | ||
}*/ | ||
@@ -591,3 +614,3 @@ Utils.debugLog('Checking health...[' + loggerId + ']'); | ||
Utils.debugLog('Sending log from log callback... [' + loggerId + ']'); | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel/*, ++callbackDepth*/); | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel, datetime); | ||
}); | ||
@@ -600,9 +623,9 @@ Params.connection_socket.destroy(); | ||
if (shouldSendPing()) { | ||
Utils.debugLog('Pinging first...'); | ||
if (shouldTouch()) { | ||
Utils.debugLog('Touching first...'); | ||
Params.logging_socket_callbacks.push(function() { | ||
Utils.debugLog('Sending log from ping callback... [' + loggerId + ']'); | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel/*, ++callbackDepth*/); | ||
Utils.debugLog('Sending log from touch callback... [' + loggerId + ']'); | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel, datetime); | ||
}); | ||
sendPing(); | ||
touch(); | ||
return; | ||
@@ -615,3 +638,3 @@ } | ||
Utils.debugLog('Sending log from token callback... [' + loggerId + ']'); | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel/*, ++callbackDepth*/); | ||
sendLogRequest(logMessage, level, loggerId, sourceFile, sourceLine, sourceFunc, verboseLevel, datetime); | ||
}); | ||
@@ -624,8 +647,3 @@ obtainToken(loggerId, null /* means resolve in function */); | ||
let datetime = Params.options.utc_time ? getCurrentTimeUTC() : new Date().getTime(); | ||
if (Params.options.time_offset) { | ||
datetime += (1000 * Params.options.time_offset); // offset is in seconds | ||
} | ||
const request = { | ||
token: getToken(loggerId), | ||
datetime: datetime, | ||
@@ -640,2 +658,5 @@ logger: loggerId, | ||
}; | ||
if (Utils.hasFlag(Flag.CHECK_TOKENS)) { | ||
request.token = getToken(loggerId); | ||
} | ||
if (typeof verboseLevel !== 'undefined') { | ||
@@ -669,3 +690,2 @@ request.vlevel = verboseLevel; | ||
Params.connecting = true; | ||
let client = Params.connection_socket; | ||
try { | ||
@@ -699,3 +719,3 @@ Params.options = typeof options === 'undefined' ? Params.options : options; | ||
}; | ||
Utils.log('Key generated'); | ||
Utils.debugLog('Key generated'); | ||
} else { | ||
@@ -722,3 +742,3 @@ Params.rsa_key = { | ||
} | ||
Utils.log('Known client...'); | ||
Utils.vLog(8, 'Known client...'); | ||
} | ||
@@ -733,4 +753,4 @@ if (typeof Params.options.server_public_key !== 'undefined') { | ||
} | ||
Utils.log('Connecting to the Residue server...'); | ||
client.connect(Params.options.connect_port, Params.options.host, function() { | ||
Utils.log('Intializing connection...'); | ||
Params.connection_socket.connect(Params.options.connect_port, Params.options.host, function() { | ||
let request = { | ||
@@ -750,3 +770,3 @@ _t: Utils.getTimestamp(), | ||
const fullReq = r + PACKET_DELIMITER; | ||
client.write(fullReq); | ||
Params.connection_socket.write(fullReq); | ||
}); | ||
@@ -762,16 +782,22 @@ } catch (e) { | ||
disconnect = function() { | ||
Params.tokens = []; | ||
Params.token_request_queue = []; | ||
Params.connected = false; | ||
Params.connecting = false; | ||
Params.connection = null; | ||
Params.token_socket_connected = false; | ||
Params.logging_socket_connected = false; | ||
if (Params.connected) { | ||
if (Params.connection_socket.destroyed) { | ||
Utils.log('Disconnecting gracefully...'); | ||
Params.token_socket.end(); | ||
Params.logging_socket.end(); | ||
Params.tokens = []; | ||
Params.connected = false; | ||
Params.connection = null; | ||
Params.token_socket_connected = false; | ||
Params.logging_socket_connected = false; | ||
} else { | ||
Utils.log('Disconnecting...'); | ||
// Following will call 'close' -> disconnect -> gracefully close | ||
Params.connection_socket.end(); | ||
try { | ||
if (Params.connection_socket.destroyed) { | ||
Utils.log('Disconnecting gracefully...'); | ||
Params.token_socket.end(); | ||
Params.logging_socket.end(); | ||
} else { | ||
Utils.log('Disconnecting...'); | ||
// Following will call 'close' -> disconnect -> gracefully close | ||
Params.connection_socket.end(); | ||
} | ||
} catch (err) { | ||
} | ||
@@ -778,0 +804,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50754
906