belliottsmith commented on code in PR #17:
URL: https://github.com/apache/cassandra-accord/pull/17#discussion_r1056542210
##########
accord-core/src/main/java/accord/utils/async/AsyncCallbacks.java:
##########
@@ -0,0 +1,45 @@
+package accord.utils.async;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.Executor;
+import java.util.function.BiConsumer;
+
+public class AsyncCallbacks
+{
+ private static final Logger logger =
LoggerFactory.getLogger(AsyncCallbacks.class);
+
+ private static final BiConsumer<Object, Throwable> NOOP = (unused,
failure) -> {
+ if (failure != null)
+ logger.error("Exception received by noop callback", failure);
+ };
+
+ public static <T> BiConsumer<? super T, Throwable> noop()
+ {
+ return NOOP;
+ }
+
+ public static <T> BiConsumer<? super T, Throwable> inExecutor(BiConsumer<?
super T, Throwable> callback, Executor executor)
+ {
+ return (result, throwable) -> {
+ try
+ {
+ executor.execute(() -> callback.accept(result, throwable));
+ }
+ catch (Throwable t)
+ {
+ callback.accept(null, t);
+ }
+ };
+ }
+
+
+ public static <T> BiConsumer<? super T, Throwable> inExecutor(Runnable
runnable, Executor executor)
+ {
+ return (result, throwable) -> {
+ if (throwable == null) executor.execute(runnable);
+ else throw new RuntimeException(throwable);
+ };
+ }
+}
Review Comment:
Is this the right contract? We seem to be relying on the executor’s
exception handling, and may be duplicating the exception handling since this is
typically for a listener not the final consumer - should we just not submit to
the executor if throwable is non-null?
In general I think composability should terminate with the exception, except
for use cases that explicitly handle both…
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]