Skip to content

Commit

Permalink
[ts] add first problem - limited connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhyde committed Aug 20, 2024
1 parent bc7b6df commit 367d474
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
10 changes: 6 additions & 4 deletions source/routes/index.ts
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
57 changes: 57 additions & 0 deletions source/routes/problems/connectionsPool.ts
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
2 changes: 1 addition & 1 deletion source/simulate/load.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { parseSize } from '../utils/parseSize.ts'
import { parseSize } from '~/utils/parseSize.ts'

const MAX_DELAY = 5000
const memoryLeaks: unknown[] = []
Expand Down

0 comments on commit 367d474

Please sign in to comment.