allthingssecurity commented on code in PR #30304:
URL: https://github.com/apache/camel/pull/30304#discussion_r4611829193
##########
core/camel-core-processor/src/main/java/org/apache/camel/processor/AbstractThrottler.java:
##########
@@ -82,6 +89,89 @@ void scheduleClean(AtomicReference<ScheduledFuture<?>>
cleanFuture, Runnable cle
}
}
+ /**
+ * A task that processes the exchange again on the thread pool, unless
doShutdown failed the exchange before.
+ */
+ final class AsyncCall implements Callable<Boolean> {
+ private final Exchange exchange;
+ private final AsyncCallback callback;
+
+ private AsyncCall(Exchange exchange, AsyncCallback callback) {
+ this.exchange = exchange;
+ this.callback = callback;
+ }
+
+ // a Callable, as the task was before, so the thread pool is called
with the same submit and schedule methods
+ @Override
+ public Boolean call() {
+ if (asyncCalls.remove(this)) {
+ return process(exchange, callback);
+ }
+ return false;
+ }
+
+ /**
+ * The thread pool did not take the task (rejected), so it is no
longer queued.
+ *
+ * @return true if the caller still owns the exchange, false if
doShutdown has already failed the exchange and
+ * called the callback (the caller must then leave the
exchange alone)
+ */
+ boolean rejected() {
+ return asyncCalls.remove(this);
+ }
+
+ private void fail() {
+ // the exchange is no longer queued (a later throttler must
process it synchronously)
+ exchange.removeProperty(PROPERTY_EXCHANGE_STATE);
+ exchange.setException(
+ new RouteStoppingException("Throttled exchange is not
processed as the throttler is shut down"));
+ }
+ }
+
+ /**
+ * Creates the task to process the exchange again on the thread pool, and
remembers it until it starts.
+ */
+ AsyncCall newAsyncCall(Exchange exchange, AsyncCallback callback) {
+ AsyncCall call = new AsyncCall(exchange, callback);
+ asyncCalls.add(call);
+ return call;
+ }
+
+ /**
+ * Fails the exchanges whose asynchronous task has not started yet. They
would never be processed once the thread
+ * pool is shut down (such as when the route is removed after the shutdown
timeout ran out), and their callers would
+ * wait for ever. Only called once doShutdown shut down the thread pool of
this EIP: a thread pool of the user still
+ * runs the tasks.
+ */
+ void failAsyncCalls() {
+ for (AsyncCall call : asyncCalls) {
Review Comment:
Right, the loop may still visit a call that its task has just claimed, and
the `asyncCalls.remove(call)` guard is what makes that safe: only the one that
removes it owns the exchange. Since the PR is approved I'll leave the code as
is and add the comment if there is another change here.
_Claude Code on behalf of allthingssecurity_
--
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]