-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt (failing, hanging) at custom sync GET requests
- Loading branch information
1 parent
bfc3e44
commit de4200a
Showing
6 changed files
with
406 additions
and
762 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
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,34 @@ | ||
const { | ||
Worker, | ||
MessageChannel, | ||
receiveMessageOnPort, | ||
} = require('worker_threads'); | ||
|
||
// Start a worker thread and return a `syncGetWorker` | ||
// capable of making sync requests until shut down. | ||
function startWorker() { | ||
const { port1: localPort, port2: workerPort } = new MessageChannel(); | ||
const sharedLock = new SharedArrayBuffer(4); | ||
const sharedLockArray = new Int32Array(sharedLock); | ||
const worker = new Worker('./SyncGetWorker.js', { | ||
workerData: { sharedLock, requestPort: workerPort }, | ||
transferList: [workerPort], | ||
}); | ||
function get(url) { | ||
worker.postMessage(url); | ||
Atomics.wait(sharedLockArray, 0, 0); // blocks until notified at index 0. | ||
const response = receiveMessageOnPort(localPort); | ||
if (response.message.error) { | ||
throw response.message.error; | ||
} else { | ||
return response.message; | ||
} | ||
} | ||
function shutDown() { | ||
localPort.close(); | ||
worker.terminate(); | ||
} | ||
return { get, shutDown }; | ||
} | ||
|
||
module.exports.startWorker = startWorker; |
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,36 @@ | ||
const { parentPort, workerData } = require('worker_threads'); | ||
const https = require('https'); | ||
|
||
const { sharedLock, requestPort } = workerData; | ||
const sharedLockArray = new Int32Array(sharedLock); | ||
|
||
parentPort.on('message', async (url) => { | ||
console.log('worker message:', url); | ||
try { | ||
const response = await getBody(url); | ||
requestPort.postMessage(response); | ||
} catch (error) { | ||
requestPort.postMessage({ error }); | ||
} | ||
Atomics.notify(sharedLockArray, 0); | ||
}); | ||
|
||
// Helpers ########################################################### | ||
|
||
async function getBody(url) { | ||
return new Promise(function (resolve, reject) { | ||
https | ||
.get(url, function (res) { | ||
let body = ''; | ||
res.on('data', function (chunk) { | ||
body += chunk; | ||
}); | ||
res.on('end', function () { | ||
resolve(body); | ||
}); | ||
}) | ||
.on('error', function (err) { | ||
reject(err); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.