npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@youneed/server-middleware-load-shed

v0.1.0

Published

@youneed/server middleware: load-shedding (global concurrency limit) — fast-fail surplus requests with 503 under overload.

Readme

@youneed/server-middleware-load-shed

Load-shedding (global concurrency limit) middleware for @youneed/server. Under overload it fast-fails surplus requests with 503 Service Unavailable instead of letting them pile up, so the server stays healthy.

import { Application, Response } from "@youneed/server";
import { loadShed } from "@youneed/server-middleware-load-shed";

const app = Application()
  .use(loadShed({ maxConcurrent: 500, retryAfter: 2 }))
  .get("/work", () => doExpensiveWork());
// once 500 requests are in flight, the 501st gets:
//   503 Service Unavailable
//   Retry-After: 2
//   { "error": "Service Unavailable" }

What is load-shedding?

A server has finite capacity. When traffic exceeds it, naively accepting every request makes things worse: queues grow, latency climbs, memory balloons, and eventually the whole process falls over — taking down the requests it could have served. Load-shedding caps how many requests are in flight at once and, past that cap, rejects the surplus immediately with a cheap 503. The requests you DO accept stay fast. This is backpressure: a fast, honest "I'm full, come back later" beats a slow, dishonest "maybe..." that never lands.

The middleware keeps a counter: it increments before calling the next handler and decrements in a finally, so a slot is freed whether the request succeeds or throws. On entry, if the counter is at the limit, it sheds.

vs. rate-limiting

Rate-limiting is per-client over a time window (e.g. "100 req/min per API key") — a fairness/abuse policy. Load-shedding is a global, here-and-now capacity gate: it doesn't care who you are, only whether the server has room right now. They're complementary — rate-limit for fairness, shed load for survival.

API

  • loadShed(opts?) — middleware. Tracks in-flight requests; sheds the surplus with 503 + Retry-After (does not call next()). Options:

    • maxConcurrent — max in-flight requests before shedding (default 100).

    • retryAfter — seconds advertised in the Retry-After header (default 1).

    • shouldShed(ctx, inflight) — custom shed decision, used instead of the inflight >= maxConcurrent threshold. Return true to shed. Fold in extra load signals (event-loop lag, memory pressure, …):

      loadShed({
        shouldShed: (_ctx, inflight) => inflight >= 500 || eventLoopLagMs() > 50,
      });

    The returned middleware also carries a live .inflight getter (current in-flight count) for metrics/health checks.