Skip to content

Commit

Permalink
Prevent CorrelationIdManager to block process termination (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorhdzg authored Oct 26, 2021
1 parent ebb35e0 commit 577354c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
37 changes: 26 additions & 11 deletions Library/CorrelationIdManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import Config = require("./Config");

class CorrelationIdManager {
private static TAG = "CorrelationIdManager";
private static _handle: NodeJS.Timer;
public static correlationIdPrefix = "cid-v1:";

public static w3cEnabled = true;

// To avoid extraneous HTTP requests, we maintain a queue of callbacks waiting on a particular appId lookup,
// as well as a cache of completed lookups so future requests can be resolved immediately.
private static pendingLookups: {[key: string]: Function[]} = {};
private static completedLookups: {[key: string]: string} = {};
private static pendingLookups: { [key: string]: Function[] } = {};
private static completedLookups: { [key: string]: string } = {};

private static requestIdMaxLength = 1024;
private static currentRootId = Util.randomu32();
Expand Down Expand Up @@ -71,21 +71,36 @@ class CorrelationIdManager {
// Not found, probably a bad key. Do not try again.
CorrelationIdManager.completedLookups[appIdUrlString] = undefined;
delete CorrelationIdManager.pendingLookups[appIdUrlString];
} else {
// Retry after timeout.
setTimeout(fetchAppId, config.correlationIdRetryIntervalMs);
}
else {
// Keep retrying
return;
}
// Do not retry
if (CorrelationIdManager._handle) {
clearTimeout(CorrelationIdManager._handle);
CorrelationIdManager._handle = undefined;
}
});
if (req) {
req.on('error', (error: Error) => {
// Unable to contact endpoint.
// Do nothing for now.
Logging.warn(CorrelationIdManager.TAG, error);
if (this._handle) {
clearTimeout(CorrelationIdManager._handle);
CorrelationIdManager._handle = undefined;
}
});
req.end();
}
};
setTimeout(fetchAppId, 0);
if (!CorrelationIdManager._handle) {
CorrelationIdManager._handle = <any>setTimeout(fetchAppId, config.correlationIdRetryIntervalMs);
CorrelationIdManager._handle.unref(); // Don't block apps from terminating
}
// Initial fetch
setImmediate(fetchAppId);
}

public static cancelCorrelationIdQuery(config: Config, callback: (correlationId: string) => void) {
Expand All @@ -106,7 +121,7 @@ class CorrelationIdManager {
public static generateRequestId(parentId: string): string {
if (parentId) {
parentId = parentId[0] == '|' ? parentId : '|' + parentId;
if (parentId[parentId.length -1] !== '.') {
if (parentId[parentId.length - 1] !== '.') {
parentId += '.';
}

Expand Down Expand Up @@ -147,8 +162,8 @@ class CorrelationIdManager {
// overflow delimiter '#'
let trimPosition = CorrelationIdManager.requestIdMaxLength - 9;
if (parentId.length > trimPosition) {
for(; trimPosition > 1; --trimPosition) {
const c = parentId[trimPosition-1];
for (; trimPosition > 1; --trimPosition) {
const c = parentId[trimPosition - 1];
if (c === '.' || c === '_') {
break;
}
Expand All @@ -164,7 +179,7 @@ class CorrelationIdManager {
while (suffix.length < 8) {
suffix = '0' + suffix;
}
return parentId.substring(0,trimPosition) + suffix + '#';
return parentId.substring(0, trimPosition) + suffix + '#';
}
}

Expand Down
6 changes: 5 additions & 1 deletion Library/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class Util {
});
public static isNodeExit = false;

public constructor() {
Util._addCloseHandler();
}

/**
* helper method to access userId and sessionId cookie
*/
Expand Down Expand Up @@ -417,7 +421,7 @@ class Util {
}
}

private static addCloseHandler() {
private static _addCloseHandler() {
if (!Util._listenerAttached) {
process.on("exit", () => {
Util.isNodeExit = true;
Expand Down

0 comments on commit 577354c

Please sign in to comment.