Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
1.2 kB ยท 42 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243import type { MiddlewareHandler } from "hono";
import { limits } from "./limits";import { shedResponse, wideEvent } from "./shed";
/** Health checks must never be shed or Fly restarts a machine that is merely busy. */const EXEMPT_PATHS = new Set(["/ping"]);
/** `opts.maxInFlight` is read per request so the shared `limits` object stays live. */export function createConcurrencyGuard(opts: { maxInFlight: number }) { let inFlight = 0;
const middleware: MiddlewareHandler = async (c, next) => { if (EXEMPT_PATHS.has(c.req.path)) return next();
const event = wideEvent(c); if (inFlight >= opts.maxInFlight) { if (event) { event.shed = true; event.in_flight = inFlight; } return shedResponse(c, { code: "SERVICE_UNAVAILABLE", status: 503, message: "Server is busy, retry shortly", retryAfterSeconds: 5, }); }
inFlight++; if (event) event.in_flight = inFlight; try { await next(); } finally { inFlight--; } };
return { middleware, inFlight: () => inFlight };}
export const concurrencyGuard = createConcurrencyGuard(limits);