Skip to content

Abstraction for Clock with DI via RxJavaPlugins #3295

Closed
@artem-zinnatullin

Description

@artem-zinnatullin

Intro: in RxJava (for the Operators) time is related to a Scheduler that runs the code, and this is awesome (Schedulers.test() proves that).

But, at the moment RxJava's Schedulers use System.currentTimeMillis() as the source of the time to make some time-relative things work: delay(), window(), schedule(), schedulePeriodically(), Schedulers.test() and so on.

The problem with System.currentTimeMillis() is that it represents "wall clock" which can be changed by:

  • By the user of the OS/device.
  • By the OS itself: for example, your mobile devices sync wall clock with the carrier periodically, desktop and server OSes also do this.
  • Leap second, half of the internet was not available because of Leap second problem this year, we can prevent this problem in our systems!

Such changes of wall clock may break the business logic of the applications (think about banking, security, apps and systems) and even crash them or create dead/live locks.

Proposal: Add Clock abstraction which will use System.currentTimeMillis() by default (no breaking changes) and ability for the users to inject their own implementation via RxJavaPlugins.

In Android we can use SystemClock.elapsedRealtime() as the source of linear, monotonic time in milliseconds. I guess, we will even be able to ship :shipit: this via RxAndroid as a default option for RxJava on Android, or it's possible to do this via reflection directly in RxJava (though I am not a fan of reflection in RxJava).

See also #2943.

I can work on PR for this both for RxJava and RxAndroid if RxJava team will agree with this proposal.

CC all who interested in this, what is your opinion?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions