repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
build(deps-dev): apply updartes
[poolifier.git]
/
examples
/
typescript
/
http-server-pool
/
fastify-cluster
/
src
/
worker.ts
diff --git
a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts
b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts
index 4084bf71fb7b6461311e5a2490ed6ab6bbe9c7a3..e491fa8f4b05c3ee4712740e1e669a102775108e 100644
(file)
--- a/
examples/typescript/http-server-pool/fastify-cluster/src/worker.ts
+++ b/
examples/typescript/http-server-pool/fastify-cluster/src/worker.ts
@@
-1,22
+1,31
@@
import type { AddressInfo } from 'node:net'
import type { AddressInfo } from 'node:net'
-import { ClusterWorker } from 'poolifier'
+
import Fastify, { type FastifyInstance } from 'fastify'
import Fastify, { type FastifyInstance } from 'fastify'
-import
type { WorkerData, WorkerResponse } from './types.js
'
+import
{ ClusterWorker } from 'poolifier
'
-const factorial: (n: number) => number = n => {
- if (n === 0) {
- return 1
- }
- return factorial(n - 1) * n
-}
+import type { WorkerData, WorkerResponse } from './types.js'
class FastifyWorker extends ClusterWorker<WorkerData, WorkerResponse> {
private static fastify: FastifyInstance
class FastifyWorker extends ClusterWorker<WorkerData, WorkerResponse> {
private static fastify: FastifyInstance
+ private static readonly factorial = (n: number | bigint): bigint => {
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
+ }
+ }
+
private static readonly startFastify = async (
workerData?: WorkerData
): Promise<WorkerResponse> => {
private static readonly startFastify = async (
workerData?: WorkerData
): Promise<WorkerResponse> => {
- const { port } = workerData as WorkerData
+ const { port } = workerData!
+
FastifyWorker.fastify = Fastify({
logger: true
})
FastifyWorker.fastify = Fastify({
logger: true
})
@@
-29,7
+38,7
@@
class FastifyWorker extends ClusterWorker<WorkerData, WorkerResponse> {
Params: { number: number }
}>('/api/factorial/:number', request => {
const { number } = request.params
Params: { number: number }
}>('/api/factorial/:number', request => {
const { number } = request.params
- return { number:
factorial(number
) }
+ return { number:
FastifyWorker.factorial(number).toString(
) }
})
await FastifyWorker.fastify.listen({ port })
})
await FastifyWorker.fastify.listen({ port })
@@
-40,7
+49,11
@@
class FastifyWorker extends ClusterWorker<WorkerData, WorkerResponse> {
}
public constructor () {
}
public constructor () {
- super(FastifyWorker.startFastify)
+ super(FastifyWorker.startFastify, {
+ killHandler: async () => {
+ await FastifyWorker.fastify.close()
+ }
+ })
}
}
}
}