-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaed537
commit 035d6d8
Showing
7 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
blockchain-patch/apply/privatesky/psknode/core/utils/pingpongFork.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const PING = "PING"; | ||
const PONG = "PONG"; | ||
|
||
module.exports.fork = function pingPongFork(modulePath, args, options){ | ||
const child_process = require("child_process"); | ||
const defaultStdio = ["inherit", "inherit", "inherit", "ipc"]; | ||
|
||
if(!options){ | ||
options = {stdio: defaultStdio}; | ||
}else{ | ||
if(typeof options.stdio === "undefined"){ | ||
options.stdio = defaultStdio; | ||
} | ||
|
||
let stdio = options.stdio; | ||
if(stdio.length<3){ | ||
for(let i=stdio.length; i<4; i++){ | ||
stdio.push("inherit"); | ||
} | ||
stdio.push("ipc"); | ||
} | ||
} | ||
|
||
let child = child_process.fork(modulePath, args, options); | ||
|
||
child.on("message", (message)=>{ | ||
if(message === PING){ | ||
child.send(PONG); | ||
} | ||
}); | ||
|
||
return child; | ||
}; | ||
|
||
module.exports.enableLifeLine = function(timeout){ | ||
|
||
if(typeof process.send === "undefined"){ | ||
console.log("\"process.send\" not found. LifeLine mechanism disabled!"); | ||
return; | ||
} | ||
|
||
let lastConfirmationTime; | ||
const interval = timeout || 10 * 60 * 1000; | ||
|
||
// this is needed because new Date().getTime() has reduced precision to mitigate timer based attacks | ||
// for more information see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime | ||
const roundingError = 101; | ||
|
||
function sendPing(){ | ||
try { | ||
process.send(PING); | ||
} catch (e) { | ||
//console.log('Parent is not available, shutting down'); | ||
//exit(1) | ||
} | ||
} | ||
|
||
process.on("message", function (message){ | ||
if(message === PONG){ | ||
lastConfirmationTime = new Date().getTime(); | ||
} | ||
}); | ||
|
||
function exit(code){ | ||
setTimeout(()=>{ | ||
process.exit(code); | ||
}, 0); | ||
} | ||
|
||
const exceptionEvents = ["SIGINT", "SIGUSR1", "SIGUSR2", "SIGTERM", "SIGHUP"]; | ||
let killingSignal = false; | ||
for(let i=0; i<exceptionEvents.length; i++){ | ||
process.on(exceptionEvents[i], (event, code)=>{ | ||
killingSignal = true; | ||
clearInterval(timeoutInterval); | ||
//console.log(`Caught event type [${exceptionEvents[i]}]. Shutting down...`, code, event); | ||
//exit(code); | ||
}); | ||
} | ||
|
||
const timeoutInterval = setInterval(function(){ | ||
const currentTime = new Date().getTime(); | ||
|
||
if(typeof lastConfirmationTime === "undefined" || currentTime - lastConfirmationTime < interval + roundingError && !killingSignal){ | ||
sendPing(); | ||
}else{ | ||
//console.log("Parent process did not answer. Shutting down...", process.argv, killingSignal); | ||
//exit(1); | ||
} | ||
}, interval); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters