-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor]: worker as a separate entry point for cluster primary
- Loading branch information
Showing
4 changed files
with
146 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
/** | ||
* The purpose of this module is to provide a serialization layer for passing arguments to a new Worker instance | ||
* This allows us to completely separate the cluster worker from the cluster primary | ||
*/ | ||
|
||
function circularReplacer() { | ||
const seen = new WeakSet(); | ||
|
||
return (key, value) => { | ||
if (typeof value === 'object' && value !== null) { | ||
if (seen.has(value)) { | ||
return; | ||
} | ||
|
||
seen.add(value); | ||
} | ||
|
||
return value; | ||
} | ||
} | ||
|
||
function serialize(object) { | ||
let data = encodeURIComponent(JSON.stringify(object, circularReplacer())); | ||
let buff = new Buffer.from(data); | ||
return buff.toString('base64'); | ||
} | ||
|
||
function deserialize(string) { | ||
let buff = new Buffer.from(string, 'base64'); | ||
return JSON.parse(decodeURIComponent(buff.toString('ascii'))); | ||
} | ||
|
||
module.exports = { | ||
serialize, | ||
deserialize | ||
}; |
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,6 @@ | ||
'use strict'; | ||
|
||
const ClusterWorker = require('./worker'); | ||
const worker = new ClusterWorker(); | ||
|
||
worker.start(); |
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