increase process priority instead of offloading to workers

This commit is contained in:
2025-01-22 15:00:32 +09:00
parent c4c7c921d7
commit 4f8dcebcd1
6 changed files with 45 additions and 16 deletions
+3 -2
View File
@@ -2,5 +2,6 @@ FROM node:current-alpine
WORKDIR /app WORKDIR /app
COPY package.json package-lock.json ./ COPY package.json package-lock.json ./
RUN npm install RUN npm install
COPY . . COPY public ./public
CMD ["node", "js/index.js"] COPY src ./src
CMD ["node", "src/index.js"]
+1 -1
View File
@@ -8,7 +8,7 @@
"three": "/three/build/three.module.js", "three": "/three/build/three.module.js",
"three/addons/": "/three/examples/jsm/", "three/addons/": "/three/examples/jsm/",
"@tweenjs/tween.js": "/@tweenjs/tween.js/dist/tween.esm.js", "@tweenjs/tween.js": "/@tweenjs/tween.js/dist/tween.esm.js",
"stats.js": "/stats.js/src/stats.js" "stats.js": "/stats.js/src/Stats.js"
} }
} }
</script> </script>
+5 -6
View File
@@ -5,7 +5,6 @@ import Buff from './buff.js'
import Entity from './entity.js' import Entity from './entity.js'
import Projectile from './projectile.js' import Projectile from './projectile.js'
import Terrain from './terrain.js' import Terrain from './terrain.js'
import { Worker } from 'node:worker_threads'
export default class Game { export default class Game {
id = crypto.randomUUID() id = crypto.randomUUID()
@@ -32,6 +31,10 @@ export default class Game {
get tickBudget() { return this.#tickBudget } get tickBudget() { return this.#tickBudget }
set logic(value) { this.#logic = value } set logic(value) { this.#logic = value }
constructor() {
this.#eventEmitter.setMaxListeners(20)
}
action(id, options) { action(id, options) {
const entity = this.entities.find((it) => it.id == id) const entity = this.entities.find((it) => it.id == id)
if (entity == null) { if (entity == null) {
@@ -122,10 +125,6 @@ export default class Game {
} }
subscription(websocket, id) { subscription(websocket, id) {
const worker = new Worker('./src/worker/object-to-json.js')
const sendToWebSocket = function sendToWebSocket(message) { websocket.send(message) } // TODO: latency because workers wait for the main thread's next tick which is ~33ms
worker.on('message', sendToWebSocket)
return function builtSubscription() { return function builtSubscription() {
const game = this const game = this
@@ -136,7 +135,7 @@ export default class Game {
const state = game.visionByTeam(team) const state = game.visionByTeam(team)
state.currentTick = game.currentTick state.currentTick = game.currentTick
worker.postMessage(state) websocket.send(JSON.stringify(state))
} }
} }
+13 -2
View File
@@ -1,7 +1,16 @@
import { Dungeon, Ravine } from './level.js'
import { WebSocketExpress } from 'websocket-express' import { WebSocketExpress } from 'websocket-express'
import express from 'express' import express from 'express'
import Game from './game.js' import Game from './game.js'
import { Dungeon, Ravine } from './level.js' import os from 'node:os'
try {
// WARNING: process.nice can undermine dependencies?
os.setPriority(process.pid, os.constants.priority.PRIORITY_HIGHEST)
}
catch (error) {
console.warn('Could not adjust process priority on startup.')
}
const app = new WebSocketExpress() const app = new WebSocketExpress()
const port = 1280 const port = 1280
@@ -23,11 +32,13 @@ app.ws('/ws', async (req, res) => {
const message = JSON.parse(rawData) const message = JSON.parse(rawData)
console.log(message) console.log(message)
if (message.action == 'join') { if (message.action == 'join') {
const id = message.id
websocket.send(JSON.stringify(game.joinReport())) websocket.send(JSON.stringify(game.joinReport()))
const subscription = game.subscription(websocket, message.id).bind(game) const subscription = game.subscription(websocket, id).bind(game)
game.eventEmitter.on('tick', subscription) game.eventEmitter.on('tick', subscription)
websocket.on('close', () => { websocket.on('close', () => {
console.log({ event: 'disconnected', id })
game.eventEmitter.removeListener('tick', subscription) game.eventEmitter.removeListener('tick', subscription)
}) })
return return
-5
View File
@@ -1,5 +0,0 @@
import { parentPort } from 'node:worker_threads'
parentPort.on('message', (message) => {
parentPort.postMessage(JSON.stringify(message))
})
+23
View File
@@ -0,0 +1,23 @@
import WebSocket from 'ws'
const numberOfClients = 10
const url = 'ws://localhost:1280/ws'
for (let i = 1; i <= numberOfClients; i++) {
const id = `${i}`
const websocket = new WebSocket(url)
websocket.onerror = () => websocket.close()
websocket.onopen = () => {
websocket.send(JSON.stringify({ action: 'join', id }))
console.log({ client: id, event: 'joined' })
}
websocket.onclose = () => {
console.log({ client: id, event: 'disconnected' })
}
websocket.onmessage = (event) => {
const byteSize = new Blob([event.data]).size
// console.log({ client: id, received: `${byteSize} B of data` })
}
}