Skip to content

feat: integrate RequestShaper with dispatcher#119

Open
TheArchitectit wants to merge 7 commits intomcowger:mainfrom
TheArchitectit:feat/dispatcher-shaper-integration
Open

feat: integrate RequestShaper with dispatcher#119
TheArchitectit wants to merge 7 commits intomcowger:mainfrom
TheArchitectit:feat/dispatcher-shaper-integration

Conversation

@TheArchitectit
Copy link
Contributor

Part 5: Dispatcher Integration (119 lines)

Integrate RequestShaper with dispatcher service.

Size: 119 lines ✅

Independent Security Research and others added 7 commits March 8, 2026 21:48
- Add type definitions: ShaperKey, QueueWaiter, QueueResult
- Add QueueFullError exception class
- Add ShaperTarget and ShaperTargetStatus interfaces
- Add ShaperStatus summary interface
- Add RequestShaper singleton class with getInstance()

Part 1 of 4: Types and Core Infrastructure for low-RPM request shaping
- Add initialize() method for service startup
- Add reload() method for config hot-reload
- Add stop() method for graceful shutdown
- Add cleanupExpiredRequests() for queue maintenance

Part 2 of 4: Lifecycle Management for low-RPM request shaping
- Add getStatus() and getAllStatus() for runtime monitoring
- Add isShaped() to check if provider/model is rate-limited
- Add canDispatchNow() to check budget availability
- Add consumeBudget() for permit acquisition
- Add getRemainingBudget() and getWaitTimeMs()
- Add refillBudgetIfNeeded() with token bucket algorithm
- Add getShapedCount() for summary statistics

Part 3 of 4: Status and Budget APIs for low-RPM request shaping
- Add acquirePermit() for dispatcher integration with queueing
- Add releasePermit() to return budget after request completion
- Add getQueueDepth() and getQueueWaiters() for monitoring

Part 4 of 5: Queue Operations for low-RPM request shaping
- Add admitNextIfAvailable() for queue admission
- Add clearQueue() for graceful shutdown/reload
- Add start/stopCleanupTimer() for queue maintenance
- Add initializeTarget() with persistence hydration
- Add loadBudgetFromPersistence() and persistToPersistence()
- Add discoverShapedTargets() for config parsing
- Add resolveLookupKey(), toStatus(), makeKey() helpers
- Add resetInstance() for testing

Part 5 of 5: Private Implementation for low-RPM request shaping

Completes the RequestShaper service implementation.
- Add RequestShaper and QueueFullError imports
- Add acquireShaperPermit() for queue-based rate limiting
- Add releaseShaperPermit() to return permits after requests
- Integrate permit acquisition/release in dispatch flows
- Handle queue timeouts and queue full errors
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
@TheArchitectit
Copy link
Contributor Author

Code Overview

This PR integrates RequestShaper with the dispatcher.

Key Changes:

  1. packages/backend/src/services/dispatcher.ts - Dispatcher integration

Methods Added:

  • acquireShaperPermit(): Check/acquire permit before request
  • releaseShaperPermit(): Release permit after completion

Integration Points:

  • dispatch(): Chat requests
  • dispatchStream(): Streaming requests
  • dispatchSpeech(): Speech synthesis
  • dispatchImageGeneration(): Image generation
  • dispatchImageEdit(): Image editing

Flow:

Error Handling:

  • QueueFullError: Returns 429 to client
  • Timeout: Returns queue timeout error

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.

1 participant