repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/express-hybrid...
[poolifier.git]
/
examples
/
typescript
/
http-server-pool
/
express-hybrid
/
src
/
express-worker.ts
diff --git
a/examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts
b/examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts
index ac2e34d43bfc9ab2f97f51c4a874f2dee782495c..2089b3ef928d95837fafdeaf106dcd33a4bde05c 100644
(file)
--- a/
examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts
+++ b/
examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts
@@
-1,17
+1,19
@@
import type { Server } from 'node:http'
import type { AddressInfo } from 'node:net'
import type { Server } from 'node:http'
import type { AddressInfo } from 'node:net'
+
+import express, { type Express, type Request, type Response } from 'express'
import {
import {
+ availableParallelism,
ClusterWorker,
DynamicThreadPool,
ClusterWorker,
DynamicThreadPool,
- availableParallelism
} from 'poolifier'
} from 'poolifier'
-import express, { type Express, type Request, type Response } from 'express'
-import {
-
type
ClusterWorkerData,
-
type
ClusterWorkerResponse,
-
type
DataPayload,
-
type
ThreadWorkerData,
- type ThreadWorkerResponse
+
+import
type
{
+ ClusterWorkerData,
+ ClusterWorkerResponse,
+ DataPayload,
+ ThreadWorkerData,
+ ThreadWorkerResponse,
} from './types.js'
const emptyFunction = (): void => {
} from './types.js'
const emptyFunction = (): void => {
@@
-19,24
+21,25
@@
const emptyFunction = (): void => {
}
class ExpressWorker extends ClusterWorker<
}
class ExpressWorker extends ClusterWorker<
-ClusterWorkerData,
-ClusterWorkerResponse
+
ClusterWorkerData,
+
ClusterWorkerResponse
> {
private static server: Server
private static requestHandlerPool: DynamicThreadPool<
> {
private static server: Server
private static requestHandlerPool: DynamicThreadPool<
- ThreadWorkerData<DataPayload>,
- ThreadWorkerResponse<DataPayload>
+
ThreadWorkerData<DataPayload>,
+
ThreadWorkerResponse<DataPayload>
>
private static readonly startExpress = (
workerData?: ClusterWorkerData
): ClusterWorkerResponse => {
const { port, workerFile, minWorkers, maxWorkers, ...poolOptions } =
>
private static readonly startExpress = (
workerData?: ClusterWorkerData
): ClusterWorkerResponse => {
const { port, workerFile, minWorkers, maxWorkers, ...poolOptions } =
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
workerData!
ExpressWorker.requestHandlerPool = new DynamicThreadPool<
workerData!
ExpressWorker.requestHandlerPool = new DynamicThreadPool<
- ThreadWorkerData<DataPayload>,
- ThreadWorkerResponse<DataPayload>
+
ThreadWorkerData<DataPayload>,
+
ThreadWorkerResponse<DataPayload>
>(
minWorkers ?? 1,
maxWorkers ?? availableParallelism(),
>(
minWorkers ?? 1,
maxWorkers ?? availableParallelism(),
@@
-61,22
+64,23
@@
ClusterWorkerResponse
application.get('/api/factorial/:number', (req: Request, res: Response) => {
const { number } = req.params
ExpressWorker.requestHandlerPool
application.get('/api/factorial/:number', (req: Request, res: Response) => {
const { number } = req.params
ExpressWorker.requestHandlerPool
- .execute({ data: { number: parseInt(number) } }, 'factorial')
+ .execute({ data: { number:
Number.
parseInt(number) } }, 'factorial')
.then(response => {
return res.send(response.data).end()
})
.catch(emptyFunction)
})
.then(response => {
return res.send(response.data).end()
})
.catch(emptyFunction)
})
+ let listenerPort: number | undefined
ExpressWorker.server = application.listen(port, () => {
ExpressWorker.server = application.listen(port, () => {
+ listenerPort = (ExpressWorker.server.address() as AddressInfo).port
console.info(
console.info(
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
- `⚡️[express server]: Express server is started in cluster worker at http://localhost:${port}/`
+ `⚡️[express server]: Express server is started in cluster worker at http://localhost:${listenerPort.toString()}/`
)
})
return {
status: true,
)
})
return {
status: true,
- port:
(ExpressWorker.server.address() as AddressInfo)?.port ?? port
+ port:
listenerPort ?? port,
}
}
}
}
@@
-85,7
+89,7
@@
ClusterWorkerResponse
killHandler: async () => {
await ExpressWorker.requestHandlerPool.destroy()
ExpressWorker.server.close()
killHandler: async () => {
await ExpressWorker.requestHandlerPool.destroy()
ExpressWorker.server.close()
- }
+ }
,
})
}
}
})
}
}