-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New WorkerThread.js apporoach using addChildApp
- Loading branch information
1 parent
e322cd8
commit bd1b369
Showing
3 changed files
with
93 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,61 @@ | ||
/* This example spawns two worker threads, each with their own | ||
* server listening to the same port (Linux feature). */ | ||
/* This example shows two different approaches to multi-core load balancing. | ||
* The first approach (the oldest) requires Linux and will only work on Linux. | ||
* This approach listens to port 4000 on all CPUs. That's it. That's all you do. | ||
* Listening to the same port from many worker threads will work on Linux. | ||
* The second approach will work on all platforms; you set up a main acceptorApp and register all child apps | ||
* (worker apps) with it. The acceptorApp will listen to port 9001 and move sockets in round-robin fashion to | ||
* the registered child apps. | ||
* Note that, in this example we only create 2 worker threads. Ideally you should create as many as there are CPUs | ||
* in your system. But by only creating 2 here, it is simple to see the perf. gain on a system of 4 cores, as you can then | ||
* run the client side on the remaining 2 cores without interfering with the server side. */ | ||
|
||
const uWS = require('../dist/uws.js'); | ||
const port = 9001; | ||
const { Worker, isMainThread, threadId } = require('worker_threads'); | ||
const { Worker, isMainThread, threadId, parentPort } = require('worker_threads'); | ||
const os = require('os'); | ||
|
||
if (isMainThread) { | ||
|
||
/* The acceptorApp only listens, but must be SSL if worker apps are SSL and likewise opposite */ | ||
const acceptorApp = uWS./*SSL*/App({ | ||
key_file_name: 'misc/key.pem', | ||
cert_file_name: 'misc/cert.pem', | ||
passphrase: '1234' | ||
}).listen(port, (token) => { | ||
if (token) { | ||
console.log('Listening to port ' + port + ' from thread ' + threadId + ' as main acceptor'); | ||
} else { | ||
console.log('Failed to listen to port ' + port + ' from thread ' + threadId); | ||
} | ||
}); | ||
|
||
/* Main thread loops over all CPUs */ | ||
/* In this case we only spawn two (hardcoded) */ | ||
/*os.cpus()*/[0, 1].forEach(() => { | ||
|
||
/* Spawn a new thread running this source file */ | ||
new Worker(__filename); | ||
new Worker(__filename).on("message", (workerAppDescriptor) => { | ||
acceptorApp.addChildAppDescriptor(workerAppDescriptor); | ||
}); | ||
}); | ||
|
||
/* I guess main thread joins by default? */ | ||
} else { | ||
/* Here we are inside a worker thread */ | ||
const app = uWS.SSLApp({ | ||
const app = uWS./*SSL*/App({ | ||
key_file_name: 'misc/key.pem', | ||
cert_file_name: 'misc/cert.pem', | ||
passphrase: '1234' | ||
}).get('/*', (res, req) => { | ||
res.end('Hello Worker!'); | ||
}).listen(port, (token) => { | ||
if (token) { | ||
console.log('Listening to port ' + port + ' from thread ' + threadId); | ||
} else { | ||
console.log('Failed to listen to port ' + port + ' from thread ' + threadId); | ||
} | ||
}).listen(4000, (token) => { | ||
if (token) { | ||
console.log('Listening to port ' + 4000 + ' from thread ' + threadId); | ||
} else { | ||
console.log('Failed to listen to port ' + 4000 + ' from thread ' + threadId); | ||
} | ||
}); | ||
|
||
/* The worker sends back its descriptor to the main acceptor */ | ||
parentPort.postMessage(app.getDescriptor()); | ||
} |
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
Submodule uWebSockets
updated
6 files
+40 −1 | src/App.h | |
+2 −2 | src/AsyncSocket.h | |
+1 −1 | src/HttpContext.h | |
+4 −0 | src/HttpContextData.h | |
+1 −1 | src/LocalCluster.h | |
+1 −1 | uSockets |