Browse Source

js-executor: scriptMap refactored from Object to the Map()

pull/4667/head
Sergey Matvienko 5 years ago
parent
commit
0c88665654
  1. 18
      msa/js-executor/api/jsInvokeMessageProcessor.js

18
msa/js-executor/api/jsInvokeMessageProcessor.js

@ -34,10 +34,9 @@ const slowQueryLogBody = config.get('script.slow_query_log_body') === 'true';
const {performance} = require('perf_hooks'); const {performance} = require('perf_hooks');
function JsInvokeMessageProcessor(producer) { function JsInvokeMessageProcessor(producer) {
console.log("Producer:", producer);
this.producer = producer; this.producer = producer;
this.executor = new JsExecutor(useSandbox); this.executor = new JsExecutor(useSandbox);
this.scriptMap = {}; this.scriptMap = new Map();
this.scriptIds = []; this.scriptIds = [];
this.executedScriptsCounter = 0; this.executedScriptsCounter = 0;
} }
@ -157,12 +156,12 @@ JsInvokeMessageProcessor.prototype.processInvokeRequest = function(requestId, re
JsInvokeMessageProcessor.prototype.processReleaseRequest = function(requestId, responseTopic, headers, releaseRequest) { JsInvokeMessageProcessor.prototype.processReleaseRequest = function(requestId, responseTopic, headers, releaseRequest) {
var scriptId = getScriptId(releaseRequest); var scriptId = getScriptId(releaseRequest);
logger.debug('[%s] Processing release request, scriptId: [%s]', requestId, scriptId); logger.debug('[%s] Processing release request, scriptId: [%s]', requestId, scriptId);
if (this.scriptMap[scriptId]) { if (this.scriptMap.has(scriptId)) {
var index = this.scriptIds.indexOf(scriptId); var index = this.scriptIds.indexOf(scriptId);
if (index > -1) { if (index > -1) {
this.scriptIds.splice(index, 1); this.scriptIds.splice(index, 1);
} }
delete this.scriptMap[scriptId]; this.scriptMap.delete(scriptId);
} }
var releaseResponse = createReleaseResponse(scriptId, true); var releaseResponse = createReleaseResponse(scriptId, true);
logger.debug('[%s] Sending success release response, scriptId: [%s]', requestId, scriptId); logger.debug('[%s] Sending success release response, scriptId: [%s]', requestId, scriptId);
@ -189,8 +188,8 @@ JsInvokeMessageProcessor.prototype.sendResponse = function (requestId, responseT
JsInvokeMessageProcessor.prototype.getOrCompileScript = function(scriptId, scriptBody) { JsInvokeMessageProcessor.prototype.getOrCompileScript = function(scriptId, scriptBody) {
var self = this; var self = this;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
if (self.scriptMap[scriptId]) { if (self.scriptMap.has(scriptId)) {
resolve(self.scriptMap[scriptId]); resolve(self.scriptMap.get(scriptId));
} else { } else {
self.executor.compileScript(scriptBody).then( self.executor.compileScript(scriptBody).then(
(script) => { (script) => {
@ -206,16 +205,17 @@ JsInvokeMessageProcessor.prototype.getOrCompileScript = function(scriptId, scrip
} }
JsInvokeMessageProcessor.prototype.cacheScript = function(scriptId, script) { JsInvokeMessageProcessor.prototype.cacheScript = function(scriptId, script) {
if (!this.scriptMap[scriptId]) { if (!this.scriptMap.has(scriptId)) {
this.scriptIds.push(scriptId); this.scriptIds.push(scriptId);
while (this.scriptIds.length > maxActiveScripts) { while (this.scriptIds.length > maxActiveScripts) {
logger.info('Active scripts count [%s] exceeds maximum limit [%s]', this.scriptIds.length, maxActiveScripts); logger.info('Active scripts count [%s] exceeds maximum limit [%s]', this.scriptIds.length, maxActiveScripts);
const prevScriptId = this.scriptIds.shift(); const prevScriptId = this.scriptIds.shift();
logger.info('Removing active script with id [%s]', prevScriptId); logger.info('Removing active script with id [%s]', prevScriptId);
delete this.scriptMap[prevScriptId]; this.scriptMap.delete(prevScriptId);
} }
} }
this.scriptMap[scriptId] = script; this.scriptMap.set(scriptId, script);
logger.info("scriptMap size is [%s]", this.scriptMap.size);
} }
function createRemoteResponse(requestId, compileResponse, invokeResponse, releaseResponse) { function createRemoteResponse(requestId, compileResponse, invokeResponse, releaseResponse) {

Loading…
Cancel
Save