generated from 2BAD/ts-lib-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ts] add first problem - limited connection pool
- Loading branch information
Showing
3 changed files
with
64 additions
and
5 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,15 +1,17 @@ | ||
import type { FastifyInstance, FastifyPluginAsync } from 'fastify' | ||
import cpu from './cpu.ts' | ||
import io from './io.ts' | ||
import mem from './mem.ts' | ||
import ping from './ping.ts' | ||
import cpu from './load/cpu.ts' | ||
import io from './load/io.ts' | ||
import mem from './load/mem.ts' | ||
import ping from './load/ping.ts' | ||
import limitedConnectionsPool from './problems/connectionsPool.ts' | ||
|
||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
const routes: FastifyPluginAsync = async (fastify: FastifyInstance) => { | ||
void fastify.register(ping) | ||
void fastify.register(cpu) | ||
void fastify.register(mem) | ||
void fastify.register(io) | ||
void fastify.register(limitedConnectionsPool) | ||
} | ||
|
||
export default routes |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { FastifyPluginAsync } from 'fastify' | ||
import { delayResponse } from '~/simulate/load.ts' | ||
|
||
const MAX_CONCURRENT_CONNECTIONS = 5 | ||
|
||
class ConnectionsPool { | ||
private readonly maxConnections: number | ||
private currentConnections: number | ||
private readonly queue: Array<(value: string) => void> | ||
|
||
constructor(maxConnections: number) { | ||
this.maxConnections = maxConnections | ||
this.currentConnections = 0 | ||
this.queue = [] | ||
} | ||
|
||
async acquire(): Promise<void> { | ||
if (this.currentConnections < this.maxConnections) { | ||
this.currentConnections++ | ||
await Promise.resolve() | ||
return | ||
} | ||
|
||
await new Promise((resolve) => { | ||
this.queue.push(resolve) | ||
}) | ||
} | ||
|
||
release(): void { | ||
this.currentConnections-- | ||
if (this.queue.length > 0) { | ||
const next = this.queue.shift() | ||
if (next) { | ||
this.currentConnections++ | ||
next('work') | ||
} | ||
} | ||
} | ||
} | ||
|
||
const pool = new ConnectionsPool(MAX_CONCURRENT_CONNECTIONS) | ||
|
||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
export const limitedConnectionsPool: FastifyPluginAsync = async (fastify) => { | ||
fastify.get<{ Params: { ms: string } }>('/problems/limited-connections/pool/:ms', async (request) => { | ||
try { | ||
await pool.acquire() | ||
|
||
// Simulate some work | ||
return await delayResponse(Number.parseInt(request.params.ms, 10)) | ||
} finally { | ||
pool.release() | ||
} | ||
}) | ||
} | ||
|
||
export default limitedConnectionsPool |
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