Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Avoid blocking work on the request path
Do not run heavy or blocking operations inside request handlers; offload to async jobs.
Bad example
| 1 | export async function generateReport(req: Request, res: Response) { |
| 2 | // BAD: heavy CPU/IO work blocks the request |
| 3 | const pdf = await reportService.generatePdf(req.body); |
| 4 |
|
| 5 | res.setHeader("Content-Type", "application/pdf"); |
| 6 | return res.send(pdf); |
| 7 | } |
Explanation (EN)
Long CPU work or unbounded processing can occupy request capacity and exceed the latency contract.
Objašnjenje (HR)
Dug CPU posao ili neograničena obrada zauzimaju kapacitet zahtjeva i mogu prekoračiti dopušteno vrijeme odgovora.
Good example
| 1 | export async function generateReport(req: Request, res: Response) { |
| 2 | const jobId = await queue.enqueue("generate-report", { |
| 3 | userId: req.user.id, |
| 4 | params: req.body, |
| 5 | }); |
| 6 |
|
| 7 | return res.status(202).json({ jobId }); |
| 8 | } |
Explanation (EN)
Offloading long-running work keeps the request fast and lets a worker handle heavy processing.
Objašnjenje (HR)
Premjestanje dugotrajnih poslova drzi zahtjev brzima, a worker obradjuje tezak posao.
Notes (EN)
Measure long-running request work and move it to a durable job only when the API can expose asynchronous completion, retries, authorization and an idempotent submission contract.
Exceptions / Tradeoffs (EN)
Small, fast operations can run inline, but anything heavy should be queued or precomputed. Nonblocking I/O can legitimately be awaited in a request. Returning 202 changes the API and requires a status/result path; async syntax does not make CPU work nonblocking.
Iznimke / Tradeoffi (HR)
Mali i brzi poslovi mogu ostati u zahtjevu; teške poslove premjesti u red ili izračunaj unaprijed kada ugovor to dopušta. Neblokirajući I/O može se legitimno awaitati. Odgovor 202 mijenja API i zahtijeva način praćenja rezultata; async sintaksa sama ne uklanja blokiranje CPU posla.