-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implicit Schedulers #815
Comments
@headinthebox and RxScala users your opinion and comments are welcome ;-) |
Need some time to digest this ... |
Considering the Scala bindings are broken right now (after the recent |
I must be missing something, but given the possibility to explicitly specify the required scheduler, I don't see why per-method type of the scheduler is needed? Brings too much complexity for no obvious win... |
Also implicit defs repacking schedulers with creating new instances brings runtime overhead, right? |
And if ImmediateScheduler is the default for all overloads without Scheduler parameter now, why not include implicit val in Observable companion object? |
In the current version, the defaults for overloads without Scheduler parameter are already different per method. For instance, the default for |
As for the complexity, I think that 50% of less methods in Observable justifies a bit of complexity in the implicit parameters. Furthermore, the Scala community is used to passing ExecutionContexts in the same manner. The additional marker trait, with quite a descriptive name, should not make much confusion. |
Ok, but why is the proposed change superior to just supplying default arguments for schedulers for all methods? |
@vigdorchik look at the "Usage examples" I posted above. Just supplying default arguments for schedulers for all methods would only allow case 1) and 2) to work, but not 3). |
I did some analysis on
However, there are also methods with a different default Scheduler:
As we can see, the default is always either |
Like the simplified variant very much! |
@headinthebox how's your digestion going ;-) Your opinion would be appreciated |
Actually, I am not so much convinced that schedulers should be implicit anymore, since the typical scenario is that you have several different schedulers in the same query xs.subscribeOn(s0).groupBy(...).flatMap(g => g.obServerOn(s2)).observerOn(s3) How would that work out? If there is a default scheduler, you don't have to pass one. |
My 2 cents as a user. Specifying a default scheduler would be amazing. We've implemented our own scheduler, which is essentially an I/O scheduler with an upper limit. We've had to add a load of calls of .subscribeOn to all our observables. It would be nice to just configure it once on init and everything just uses that scheduler. |
Something I've been wanting for a while is the ability to use the This could be useful to use lightweight threads for example (https://github.com/puniverse/pulsar) instead of normal threads everywhere in the system. It could allow specific customization of thread behavior when an app knows a better way to do things. Here is some code I wrote (but have not yet tested) that would allow this behavior: #905 Obviously if someone is using a specific implementation directly it would not help, but generally the Perhaps we can deprecate the actual Scheduler implementation constructors so only |
@headinthebox when I wrote this proposal I thought that |
This would make rx consistent with futures and actors in scala/akka, which accept an implicit execution context. And that seems to have worked out ok for them! |
The Schedulers Plugin exists now: https://github.com/Netflix/RxJava/blob/master/rxjava-core/src/main/java/rx/plugins/RxJavaDefaultSchedulers.java |
@headinthebox @samuelgruetter Anything to do here? |
I'd love to discuss this in person this summer when I am at EPFL. Can we leave it open for now? |
Yup, it's fine leaving open. Assigned it to you for tracking purposes. |
@headinthebox that sounds great |
Moved to ReactiveX/RxScala#16 |
This is a proposal to use Scala's implicits to improve the Schedulers part of the RxScala API, by @vjovanov and myself.
Current situtation
There are many methods depending on a Scheduler. Since we do not want to always pass the Scheduler argument explicitly, there's a second version of each method which chooses a reasonable default Scheduler, for instance
Advantages of this proposal
Compared to the current situation, this proposal brings the following adavantages:
Usage examples
There are three ways of using Schedulers:
1) With default implicits: For each method, the framework chooses a reasonable default scheduler. It can be a different Scheduler for each method: For instance, for
buffer
,ThreadPoolForComputation()
is chosen, and forfrom
,ImmediateScheduler()
is chosen.2) With explicitly specified Schedulers:
3) With custom default Schedulers:
This feature is the whole point of this proposal.
How it works
There's one marker trait (without any members) for each method in Observable which takes a Scheduler:
All method pairs of a method with Scheduler and its corresponding method without Scheduler are replaced by one single method with an implicit Scheduler parameter of type
Scheduler with DefaultXxxScheduler
, for instanceis replaced by
Further, the original
is replaced by
because we need to create new Schedulers using the
new
keyword, such that we can specifywith DefaultXxxScheduler with DefaultYyyScheduler ...
.The three ways of using Schedulers work as follows:
1) Default implicits
There's
which has to be imported by all users who don't want to customize:
This is similar to Futures requiring all users to
so users should not get too upset about it ;-)
2) With explicitly specified Schedulers
There are implicit conversions to convert a regular
Scheduler
to aScheduler with DefaultXxxScheduler
:These implicits just unwrap the underlying Java Scheduler and wrap it again in a differently labeled Scala Scheduler.
3) With custom default Schedulers
Whenever a method xxx is called without giving a Scheduler, the Scala compiler will look for an implicit value of type
DefaultXxxScheduler
, issue a "could not find implicit value" error if none was found or an "ambiguous implicit values" error if multiple implicit values matched.Advanced usage
By defining new Scheduler marker traits, users can define their own method groups which should use the same Scheduler, and new groups can also be defined in terms of smaller groups:
These could then be used as follows:
but also as like this:
Preview
An incomplete preview can be found in this branch.
The text was updated successfully, but these errors were encountered: