Skip to content

feat(gateway): add per-endpoint circuit breaker for /api/gateway (#924) - #1012

Merged
greatest0fallt1me merged 3 commits into
CalloraOrg:mainfrom
eogenyi23-creator:feature/924-per-endpoint-circuit-breaker
Jul 29, 2026
Merged

feat(gateway): add per-endpoint circuit breaker for /api/gateway (#924)#1012
greatest0fallt1me merged 3 commits into
CalloraOrg:mainfrom
eogenyi23-creator:feature/924-per-endpoint-circuit-breaker

Conversation

@eogenyi23-creator

Copy link
Copy Markdown

Add a per-endpoint circuit breaker to the /api/gateway downstream proxy path so that repeated upstream failures trip an isolated breaker per apiId, causing subsequent requests to fast-fail with HTTP 503 instead of waiting for a timeout.

Changes:

  • src/routes/gatewayRoutes.ts: wrap upstream fetch in breakerRegistry.getOrCreate(apiId).execute() with a pre-flight wouldBlock() check before billing so callers are never charged when the circuit is OPEN
  • src/lib/circuitBreaker.ts: add wouldBlock() method that replicates execute() cooldown logic; set lastFailureTime in trip() so pre-check correctly computes the cooldown window
  • src/config/env.ts: add GATEWAY_BREAKER_FAILURE_THRESHOLD, GATEWAY_BREAKER_COOLDOWN_MS, GATEWAY_BREAKER_SUCCESS_THRESHOLD
  • .env.example: document the three new env vars
  • src/routes/gatewayRoutes.circuitBreaker.test.ts: 7 focused tests covering CLOSED pass-through, OPEN fast-fail, billing skip, per-API isolation, threshold-based tripping, and HALF_OPEN recovery

Closes #924

…loraOrg#924)

Add a per-endpoint circuit breaker to the /api/gateway downstream proxy
path so that repeated upstream failures trip an isolated breaker per
apiId, causing subsequent requests to fast-fail with HTTP 503 instead of
waiting for a timeout.

Changes:
- src/routes/gatewayRoutes.ts: wrap upstream fetch in
  breakerRegistry.getOrCreate(apiId).execute() with a pre-flight
  wouldBlock() check before billing so callers are never charged when
  the circuit is OPEN
- src/lib/circuitBreaker.ts: add wouldBlock() method that replicates
  execute() cooldown logic; set lastFailureTime in trip() so pre-check
  correctly computes the cooldown window
- src/config/env.ts: add GATEWAY_BREAKER_FAILURE_THRESHOLD,
  GATEWAY_BREAKER_COOLDOWN_MS, GATEWAY_BREAKER_SUCCESS_THRESHOLD
- .env.example: document the three new env vars
- src/routes/gatewayRoutes.circuitBreaker.test.ts: 7 focused tests
  covering CLOSED pass-through, OPEN fast-fail, billing skip, per-API
  isolation, threshold-based tripping, and HALF_OPEN recovery

Closes CalloraOrg#924
@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@eogenyi23-creator Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me
greatest0fallt1me merged commit 463e230 into CalloraOrg:main Jul 29, 2026
1 check passed
@greatest0fallt1me

Copy link
Copy Markdown
Contributor

All checks green — merging. Nice job! 🚀

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add per-endpoint circuit breaker for downstream calls on /api/gateway [b#059]

2 participants