feat: add supervision strategy support for throttle costCalculation#3183
Open
He-Pin wants to merge 1 commit into
Open
feat: add supervision strategy support for throttle costCalculation#3183He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
Motivation: The throttle operator's costCalculation function is user-provided and may throw, but it was called without a try-catch. Any exception failed the stream unconditionally, ignoring the configured SupervisionStrategy. Modification: Wrap costCalculation(elem) in Throttle.onPush() with a try-catch that consults the SupervisionStrategy decider. Stop fails the stage, and Resume and Restart both skip the offending element (throttle keeps no accumulated per-element state, so Restart behaves the same as Resume; the rate-limiting token bucket is deliberately not reset). The costCalculation is wrapped before tokenBucket.offer so a throwing function never consumes rate budget. The decider is a lazy val for zero overhead on the happy path. Update the Scala and Java DSL scaladoc for the costCalculation throttle overloads and the operator reference page. Result: throttle now adheres to the SupervisionStrategy attribute for its costCalculation function. Tests: - sbt "stream-tests/Test/testOnly org.apache.pekko.stream.scaladsl.FlowThrottleSpec" -- 28/28 passed References: Refs apache#3110, Refs apache#3101
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.
Motivation
Per the stream error handling docs, operators that apply user-provided functions should consult the configured
SupervisionStrategy. Thethrottleoperator'scostCalculationfunction is user-provided and may throw, but it was called without a try-catch, so any exception failed the stream unconditionally, even underSupervision.Resume/Restart.This is part of the meta-issue #3110 (add supervisor strategy support to stream operators that accept user functions). One operator per PR.
Modification
costCalculation(elem)inThrottle.onPush()with atry/catch(NonFatal) that consults theSupervisionStrategydecider:tokenBucket.offer(cost), so a throwing function never consumes rate budget.decideris alazy val→ zero overhead on the happy path.costCalculationthrottle overloads in the Scala/Java DSL scaladoc and the operator reference page. The element-count throttle overloads are intentionally left undocumented because they use a constant cost and never throw.Result
throttlenow adheres to theSupervisionStrategyattribute for its costCalculation function, with no buffer-underflow on supervised skip paths.Tests
sbt "stream-tests/Test/testOnly org.apache.pekko.stream.scaladsl.FlowThrottleSpec"— 28/28 passed, including 6 new tests: explicit Stop, default Stop, Resume (Shaping), Resume (Enforcing), Restart (same as Resume for throttle), and a last-element Resume regression.sbt "stream/mimaReportBinaryIssues"— clean (binary compatible)References
Refs #3110, Refs #3101
This is a clean-room implementation written directly for Apache Pekko.