question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improving onSchedule hook implementation

See original GitHub issue

Hi, We are actively using RxJava in multiple of our projects. In some of #the cases I would like to be able to register multiple ScheduleHandler handlers. For example one for the zipkin/brave and another one for our own project related purposes. I’m proposing some changes to the RxJavaPlugins class that will allow to do that without breaking a current API. I can create a PR but here is a patch for now:

 import java.lang.Thread.UncaughtExceptionHandler;
 import java.util.concurrent.*;
+import java.util.*;
 
 import org.reactivestreams.Subscriber;
 
 import io.reactivex.*;
+import io.reactivex.Observable;
+import io.reactivex.Observer;
 import io.reactivex.annotations.*;
 import io.reactivex.exceptions.*;
 import io.reactivex.flowables.ConnectableFlowable;
@@ -36,7 +39,7 @@
     static volatile Consumer<? super Throwable> errorHandler;
 
     @Nullable
-    static volatile Function<? super Runnable, ? extends Runnable> onScheduleHandler;
+    static volatile List<Function<? super Runnable, ? extends Runnable>> onScheduleHandler = new ArrayList<Function<? super Runnable, ? extends Runnable>>();
 
     @Nullable
     static volatile Function<? super Callable<Scheduler>, ? extends Scheduler> onInitComputationHandler;
@@ -244,7 +247,7 @@
      * @return the hook function, may be null
      */
     @Nullable
-    public static Function<? super Runnable, ? extends Runnable> getScheduleHandler() {
+    public static List<Function<? super Runnable, ? extends Runnable>> getScheduleHandler() {
         return onScheduleHandler;
     }
 
@@ -446,11 +449,35 @@
      */
     @NonNull
     public static Runnable onSchedule(@NonNull Runnable run) {
-        Function<? super Runnable, ? extends Runnable> f = onScheduleHandler;
-        if (f == null) {
-            return run;
+
+        final List<Runnable> scheduledActions = new ArrayList<Runnable>();
+
+        /*
+         * Passing "empty" actions to all handlers but the last one.
+         * The last one should call an original action
+         */
+        Iterator<Function<? super Runnable, ? extends Runnable>> onScheduleIterator = onScheduleHandler.iterator();
+        while (onScheduleIterator.hasNext()) {
+            try {
+                scheduledActions.add(onScheduleIterator.next().apply(onScheduleIterator.hasNext() ? new Runnable() {
+                    @Override
+                    public void run() {
+                        // Does nothing, just a stub method
+                    }
+                } : run));
+            } catch (Exception ex) {
+                // Noting to do
+            }
         }
-        return apply(f, run);
+
+        return new Runnable() {
+            @Override
+            public void run() {
+                for (Runnable innerAction : scheduledActions) {
+                    innerAction.run();
+                }
+            }
+        };
     }
 
     /**
@@ -606,7 +633,7 @@
         if (lockdown) {
             throw new IllegalStateException("Plugins can't be changed anymore");
         }
-        onScheduleHandler = handler;
+        onScheduleHandler.add(handler);
     }
 
     /**

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
akarnokdcommented, Nov 22, 2017

You should write a small library that manages multiple handles for you which you can depend on in your bundles.

The proper management of multiple handlers is non trivial as you have to consider concurrent access, ordering of the handlers as well as the infrastructure impact.

It should be up to developer to understand the performance impact and take care of it.

Since the scheduler hook is a core hook, it affects all users and leaves not much choice to avoid the overhead when there is no hook present. Your patch adds a lot of overhead even if there are no handles, plus it changes existing RxJavaPlugins API which is binary incompatible and not acceptable.

Also I’d reconsider using handlers in the first place as their primary purpose is to support testing and diagnostics, not resource-management or -tracking really.

0reactions
codefromthecryptcommented, Mar 26, 2018

by the way, we’re trying to top-level some functionality to prevent the endless copy/paste https://github.com/openzipkin/brave/pull/665

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flight of the Flux 3 - Hopping Threads and Schedulers - Spring
In this post, we explore the threading model, how some (most) operators are concurrent agnostic, the Scheduler abstraction and how to hop from ......
Read more >
How to use JavaScript scheduling methods with React hooks
Let's initialize them using the useState hook. The hook useState returns a pair. First is the current state and second is an updater...
Read more >
Copy Reactor Context to MDC in the case of ... - Stack Overflow
I have been following the code at https://github.com/archie-swif/webflux-mdc/blob/master/src/main/java/com/example/webfluxmdc/MdcContextLifter.
Read more >
how to fill the implementation gap for inclusive growth: - case ...
The Implementation Gap in Urban Transport Development in Egypt . ... fill gaps and enhance implementation, thus promoting inclusive growth and development ...
Read more >
QUALITY IMPROVEMENT STRATEGY PT 2
Using technology to optimize communication between patients and their care team, including using a patient portal in the EHR. Page 4. 4. IMPLEMENTATION...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found