gnodet-bot commented on code in PR #30304:
URL: https://github.com/apache/camel/pull/30304#discussion_r4611140249
##########
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:
`asyncCalls` is a `ConcurrentHashMap.newKeySet()` whose iterator is [weakly
consistent](https://docs.oracle.com/en/java/docs/api/java.base/java/util/concurrent/ConcurrentHashMap.html)
— it may not reflect concurrent removes mid-iteration. The `if
(asyncCalls.remove(call))` guard inside the loop already ensures exactly-once
ownership, so the iteration is safe; a brief comment here would make the intent
clear to future readers.
```suggestion
// ConcurrentHashMap.newKeySet() iterator is weakly consistent:
entries removed concurrently
// may still be visited, but the remove() guard below ensures
exactly-once ownership.
for (AsyncCall call : asyncCalls) {
```
--
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]