Created
August 18, 2019 22:16
-
-
Save caesaneer/afac2d5a0a9b1ac260a0b62d48327c10 to your computer and use it in GitHub Desktop.
Part 2 - Worker Threads - Index.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
// ════════════════════════════════════════════════════════════════════════════════════════════════╡ | |
const http = require('http') | |
const { StaticPool } = require('node-worker-threads-pool') | |
const numCPUs = require('os').cpus().length | |
// ════════════════════════════════════════════════════════════════════════════════════════════════╡ | |
const host = '192.168.0.14' | |
const port = 8080 | |
const start = function startServer() { | |
// Path to worker.js | |
const filePath = './worker.js' | |
// Create worker pool | |
const pool = new StaticPool({ | |
size: 16, | |
task: filePath, | |
workerData: {}, | |
}) | |
// Simple request router | |
const router = async function requestRouter(request, reply) { | |
// Send to workers | |
try { | |
await pool.exec() | |
} catch (e) { | |
console.log(e) | |
process.exit(0) | |
} | |
// console.log(result) | |
reply.end('OK') | |
} | |
// Start HTTP server | |
const server = http.createServer(router) | |
server.listen(port, host, () => { | |
console.log(`Node.js Standard Library HTTP server running on port: ${port}`) | |
}) | |
} | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment