Add admission control hook for worker backpressure — Closes #57#149
Open
conradbzura wants to merge 4 commits intowool-labs:mainfrom
Open
Add admission control hook for worker backpressure — Closes #57#149conradbzura wants to merge 4 commits intowool-labs:mainfrom
conradbzura wants to merge 4 commits intowool-labs:mainfrom
Conversation
Add BackpressureLike protocol and BackpressureContext dataclass that let users configure a per-worker hook to reject incoming tasks. The hook receives a snapshot of active task count and the incoming task, returning True to reject (triggering gRPC RESOURCE_EXHAUSTED) or False to accept. The load balancer already treats RESOURCE_EXHAUSTED as transient and skips to the next worker. The hook is threaded through LocalWorker -> WorkerProcess -> WorkerService, serialized via cloudpickle across the spawn boundary to support lambdas and closures.
Cover BackpressureContext (instantiation, frozen immutability, equality, Hypothesis property test), BackpressureLike protocol (sync, async, callable class, negative case), WorkerService dispatch with backpressure (sync/async accept and reject, context field validation, active task counting), and LocalWorker construction with the hook. Update public API completeness test for the new exports.
1fdf83e to
3b0a441
Compare
conradbzura
commented
Apr 1, 2026
LocalWorker, WorkerProcess, and WorkerService were missing docstring entries for parameters introduced by the backpressure feature. WorkerProcess was also missing uid, tags, and extra params that predated this PR. Removes a stale :param tasks: entry from WorkerService._cancel which takes no arguments.
Extends the pairwise covering array and Hypothesis strategy with a BackpressureMode dimension (NONE, SYNC, ASYNC) to verify that backpressure hooks survive cloudpickle serialization through real subprocesses. Adds rejection composition tests validating the end-to-end RESOURCE_EXHAUSTED path and load balancer fallback to an accepting worker.
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
Add a
BackpressureLikeprotocol andBackpressureContextdataclass that let users configure a per-worker hook to reject incoming tasks based on workload-specific criteria. The hook receives a snapshot of the worker's active task count and the incoming task, returningTrueto reject (triggering gRPCRESOURCE_EXHAUSTED) orFalseto accept. The load balancer already treatsRESOURCE_EXHAUSTEDas transient and skips to the next worker — so no client-side changes are needed.The default behavior (no hook) accepts all tasks, preserving current semantics. The existing
WorkerConnectionsemaphore continues to cap per-worker concurrent dispatches on the client side; this hook adds server-side admission control.Closes #57
Proposed changes
BackpressureLike protocol and BackpressureContext dataclass (
service.py)Introduce two new public types in
wool.runtime.worker.service:BackpressureContext(frozen=True)— immutable snapshot withactive_task_count: intandtask: TaskBackpressureLike(Protocol)— runtime-checkable protocol accepting both sync and async callables. ReturnTrueto reject,Falseto accept. The falsey default ofNone(no hook) naturally means "accept all".Backpressure check in WorkerService.dispatch (
service.py)Insert the check after task deserialization but before the Ack response. If the hook returns truthy, the service calls
context.abort(StatusCode.RESOURCE_EXHAUSTED, ...)— the task never enters the docket and no Ack is sent. Both sync and async hooks are supported viainspect.isawaitable().Thread hook through WorkerProcess (
process.py)Accept
backpressureinWorkerProcess.__init__and serialize it withcloudpickle.dumps()to cross themultiprocessing.spawnboundary. This supports lambdas, closures, and callable class instances — not just top-level functions. Deserialize withcloudpickle.loads()in_serve()before passing toWorkerService.User-facing API on LocalWorker (
local.py)Add
backpressure: BackpressureLike | None = Noneparameter toLocalWorker.__init__, passed through toWorkerProcess. Usage withWorkerPoolis supported viafunctools.partial:Public exports (
__init__.py)Export
BackpressureContextandBackpressureLikefrom the top-levelwoolpackage.Test cases
TestBackpressureContextTestBackpressureContextTestBackpressureContext==TestBackpressureContext==TestBackpressureContextTestBackpressureLikeisinstancecheckTestBackpressureLikeisinstancecheckTestBackpressureLikeisinstancecheckTestBackpressureLikeisinstancecheckTestWorkerServiceBackpressureWorkerService(backpressure=hook)TestWorkerServiceBackpressureTestWorkerServiceBackpressureTestWorkerServiceBackpressureTestWorkerServiceBackpressureTestWorkerServiceBackpressureTestWorkerServiceBackpressureTestLocalWorkerLocalWorker(backpressure=hook)