Skip to content
This repository was archived by the owner on Jun 7, 2026. It is now read-only.

feat(gateway): add concurrency limits for request handling - #65

Merged
matthew-petty merged 5 commits into
mainfrom
feat/concurrency-limits
Dec 26, 2025
Merged

feat(gateway): add concurrency limits for request handling#65
matthew-petty merged 5 commits into
mainfrom
feat/concurrency-limits

Conversation

@matthew-petty

Copy link
Copy Markdown
Member

Summary

  • Add configurable concurrency limits to prevent resource exhaustion
  • Separate pools for streaming (default: 3) and non-streaming (default: 5) requests
  • Returns 429 with Retry-After: 5 header when limits exceeded
  • Environment variable configuration: KOINE_MAX_STREAMING_CONCURRENT, KOINE_MAX_NONSTREAMING_CONCURRENT

Changes

Core Implementation

  • packages/gateway/src/concurrency.ts - Semaphore-based limiter with acquire/release pattern
  • packages/gateway/src/types.ts - Added CONCURRENCY_LIMIT_ERROR code and health schema
  • Routes wrapped with withConcurrencyLimit() middleware

Validation & Logging

  • Environment variable validation with logged fallbacks for invalid values
  • Logging when concurrency limits are hit (warn level)
  • Logging when handler errors occur (error level)
  • setConfig() validates inputs are non-negative integers

Documentation

  • Updated docs/environment-variables.md, docs/api-reference.md, docs/sdk-guide.md
  • Added retry handling examples to SDK READMEs
  • Updated CONTRIBUTING.md roadmap

Tests (24 new tests)

  • Acquire/release slot logic
  • Wrapper middleware behavior
  • Error handling and slot release
  • Integration with generate and stream routes
  • Edge cases: slot exhaustion, response events

Test plan

  • bun run test - 107 tests pass
  • bun run lint - No errors
  • Manual testing with docker compose up and curl

Implement global concurrency limits to prevent resource exhaustion:
- Separate limits for streaming (default: 3) and non-streaming (default: 5)
- Configurable via KOINE_MAX_STREAMING_CONCURRENT and KOINE_MAX_NONSTREAMING_CONCURRENT
- Returns 429 Too Many Requests when limit exceeded
- Health endpoint now includes concurrency status

Files:
- Add concurrency.ts with acquire/release semaphore and wrapper
- Wrap generate and stream routes with concurrency limiter
- Update types.ts with CONCURRENCY_LIMIT_ERROR code
- Add concurrency tests
- Add KOINE_MAX_STREAMING_CONCURRENT and KOINE_MAX_NONSTREAMING_CONCURRENT
  to environment variables documentation
- Document 429 response in API reference
- Add retry handling examples to both SDK READMEs and sdk-guide
- Update CONTRIBUTING.md roadmap (mark concurrency limits done, remove
  request queuing and per-key throttling as out of scope)
- Add concurrency config section to docker-deployment guide
- Mention concurrency limits in root README features
Address issues found by code review:

- Add env var validation with logged fallback for invalid values
  (prevents NaN from silently disabling all limits)
- Add logging when 429 concurrency limit is hit
- Add validation to setConfig (throws on invalid values)
- Add logging in catch block before re-throw
- Update comments for clarity (JS event loop, not Bun-specific)
- Add tests for error handling, validation, and streaming integration
Address remaining code review findings:

- Add Zod refinements to concurrencyPoolSchema (int, nonnegative, active <= limit)
- Document that getConfig() returns a shallow copy to prevent mutation
- Add test for slot exhaustion behavior (rejects when slots full)
- Add test for response close/finish event handling
@matthew-petty
matthew-petty merged commit c36a43d into main Dec 26, 2025
2 checks passed
@matthew-petty
matthew-petty deleted the feat/concurrency-limits branch December 26, 2025 17:58
@codecov

codecov Bot commented Dec 26, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.72973% with 90 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/gateway/src/routes/stream.ts 70.93% 59 Missing ⚠️
packages/gateway/src/concurrency.ts 79.72% 30 Missing ⚠️
packages/gateway/src/routes/generate.ts 98.87% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant