allthingssecurity opened a new pull request, #30567:
URL: https://github.com/apache/camel/pull/30567

   # Description
   
   [CAMEL-27319](https://issues.apache.org/jira/browse/CAMEL-27319)
   
   The Throttle EIP docs (throttle-eip.adoc, "Dynamically changing maximum 
requests per period") say that when the limit
   comes from a header and the header is absent, "the Throttler uses the old 
value". `ThrottlerNullEvalTest` tests this.
   The old value is kept in the throttling state of the key 
(`ThrottlingState.throttleRate`). Both throttlers remove that
   state when it has not been used for a while: `ConcurrentRequestsThrottler` 
10 seconds after the last permit was
   returned, `TotalRequestsThrottler` (the default mode) about 10 time periods 
after the last permit was returned. The next
   exchange gets a new state with no limit, and if it has no header it fails 
with `RuntimeExchangeException: The
   maxConcurrentRequestsExpression was evaluated as null` (or 
`maxRequestsPerPeriodExpression`).
   
   So a route like the one in the docs, `throttle(header("throttleValue"))`, 
that sets the header only when the value is to
   be changed, fails its first message without the header after the route was 
idle for 10 seconds (or 10 periods). No race
   is needed.
   
   The fix: without a correlation expression (one throttling state), the 
throttler keeps the last limit the expression
   gave (`AbstractThrottler`, one `int`). When the expression gives no limit 
for a state that has none yet, which is a
   state created after the clean, the kept limit is used, as the old value. 
Both throttling modes use it. The first message
   without a limit still fails when there was never one 
(`ThrottlerNullEvalTest#testFirstNoHeaderTest`), and the kept limit
   is cleared with the states when the throttler is shut down.
   
   With a correlation expression nothing changes: the limit of a removed state 
of a key is not kept. There can be any
   number of keys (the clean exists so that their states do not stay in 
memory), and keeping a limit per key would keep an
   entry for every key ever seen. The docs only describe the case without a 
correlation expression; the new test pins the
   behaviour with one.
   
   Found with a TLA+ model. I adapted the model of the concurrent requests 
throttler (the state of a key with its permits
   and limit, exchange threads that look up the state, set the limit, take and 
return permits, a clean that removes an
   unused state at any time after a release, blocking, async-delayed and reject 
modes) to the fix. On main it finds an
   exchange without a limit that fails after the clean although the key had a 
limit before. With the change that holds,
   and the other properties of the model still hold (68,557 distinct states 
with limits 1 and 2 and async delay, 73,283
   with rejection and two cleans; TLC and everystate agree). The total requests 
mode has the same state, clean and
   null-limit code; the model covers the concurrent mode.
   
   Tests: new `ThrottlerCleanKeepsLimitTest`, for both modes (the clean task is 
captured by the executor and run by the
   test, standing in for the idle time):
   
   - a message with the limit 3, the clean removes the state, then a message 
without the header: it must pass, and the
     limit must be 3 again;
   - the first message without a header still fails (control, passes on main);
   - with a correlation expression, a message without the header after the 
clean of its key still fails (control, passes
     on main).
   
   On main the first test fails for both modes (`RuntimeExchangeException ... 
was evaluated as null`). With the change the 6
   pass, and the whole camel-core test suite passes (9697 run, 0 failures, 45 
skipped; 12 `DumpModelAsYaml*` tests could not run offline because the 
`camel-yaml-io` snapshot in my local repository is older than the model, 
`NoClassDefFound OgnlExpression`; they do not use the throttler). The new class 
20 times in one JVM: 120 run, 0 failures.
   
   Not changed: a limit of 0 (the "pause" use). An exchange can still wait for 
ever on a state that the clean removed
   after the exchange looked it up, when it lowers the limit to 0 and blocks on 
it. That is a different change (in the
   removal condition or in `acquire`), and the model shows it is not fixed by 
this one; I will handle it separately.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it): 
[CAMEL-27319](https://issues.apache.org/jira/browse/CAMEL-27319).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
     (I built and tested the affected modules, including the formatter and 
import-sort plugins. I did not run the full root build.)
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
     This PR was prepared with Claude Code (Claude Opus 5.5). The commit 
carries a `Co-Authored-By` trailer.
   
   _Claude Code on behalf of allthingssecurity_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to