rpc: allow sharing the in-flight Semaphore across ExecutionContexts#7647
Merged
Conversation
The cap added in #7614 was scoped per-ExecutionContext: setMaxInFlight stored an int in the ctx message map and withInFlightSlot lazily created a Semaphore from it. Each independent ExecutionContext therefore got its own Semaphore, which made the cap a per-ctx cap rather than the host-wide cap callers like Moderne SaaS and the mod CLI expect when they create one InMemoryExecutionContext per repo and run several in parallel. Replace the int-message + lazy-create plumbing with a Semaphore-valued message and an explicit setInFlightSemaphore(Semaphore) setter. Callers that want a host-wide cap construct one Semaphore at the orchestrator level and install the same instance on every participating context. The old setMaxInFlight(int) is kept as a convenience for the single-ctx case but its Javadoc now spells out that it is not host-wide. withInFlightSlot runs the work unthrottled if no Semaphore is installed, preserving the "unbounded by default" behavior. Adds RewriteRpcExecutionContextViewTest covering: no-op default, fresh Semaphore from setMaxInFlight, per-ctx isolation (the misuse pattern), true sharing across many ctx/threads, and permit release on exception.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
setMaxInFlight(int)from rpc: cap concurrent in-flight requests via ExecutionContext #7614 stored an int on theExecutionContextand lazily built theSemaphoreinsidewithInFlightSlot. Each independentExecutionContexttherefore ended up with its ownSemaphore, so the cap was per-ctx, not host-wide.setInFlightSemaphore(Semaphore)so callers that want a true host-wide cap can construct oneSemaphoreonce at the orchestrator level and install the same instance on every participating context (e.g. one per repo task running in parallel).setMaxInFlight(int)as a single-ctx convenience but document that it is not host-wide.withInFlightSlotis unthrottled when noSemaphoreis installed, matching the prior "unbounded by default" baseline.This unblocks Moderne's
mod runand the SaaS recipe-worker from honoring a true process-wide rewrite-rpc concurrency cap; the wiring on those sides is in follow-up PRs.Test plan
RewriteRpcExecutionContextViewTestcovers: no-op default, fresh-Semaphore-from-setMaxInFlight, per-ctx isolation (the misuse pattern callers hit today), true sharing across many ctx/threads, permit release on exception../gradlew :rewrite-core:test --tests '*Rpc*'green locally.