Skip to content

Commit 77c2ef1

Browse files
authored
3.x: Allow setting the drift tolerance timeunit via system property (#6969)
1 parent fbee37a commit 77c2ef1

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

Diff for: src/main/java/io/reactivex/rxjava3/core/Scheduler.java

+34-9
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
* based on the relative time between it and {@link Worker#now(TimeUnit)}. However, drifts or changes in the
7474
* system clock could affect this calculation either by scheduling subsequent runs too frequently or too far apart.
7575
* Therefore, the default implementation uses the {@link #clockDriftTolerance()} value (set via
76-
* {@code rx3.scheduler.drift-tolerance} in minutes) to detect a drift in {@link Worker#now(TimeUnit)} and
77-
* re-adjust the absolute/relative time calculation accordingly.
76+
* {@code rx3.scheduler.drift-tolerance} and {@code rx3.scheduler.drift-tolerance-unit}) to detect a
77+
* drift in {@link Worker#now(TimeUnit)} and re-adjust the absolute/relative time calculation accordingly.
7878
* <p>
7979
* The default implementations of {@link #start()} and {@link #shutdown()} do nothing and should be overridden if the
8080
* underlying task-execution scheme supports stopping and restarting itself.
@@ -91,17 +91,42 @@ public abstract class Scheduler {
9191
/**
9292
* The tolerance for a clock drift in nanoseconds where the periodic scheduler will rebase.
9393
* <p>
94-
* The associated system parameter, {@code rx3.scheduler.drift-tolerance}, expects its value in minutes.
94+
* Associated system parameters:
95+
* <ul>
96+
* <li>{@code rx3.scheduler.drift-tolerance}, long, default {@code 15}</li>
97+
* <li>{@code rx3.scheduler.drift-tolerance-unit}, string, default {@code minutes},
98+
* supports {@code seconds} and {@code milliseconds}.
99+
* </ul>
95100
*/
96-
static final long CLOCK_DRIFT_TOLERANCE_NANOSECONDS;
97-
static {
98-
CLOCK_DRIFT_TOLERANCE_NANOSECONDS = TimeUnit.MINUTES.toNanos(
99-
Long.getLong("rx3.scheduler.drift-tolerance", 15));
101+
static final long CLOCK_DRIFT_TOLERANCE_NANOSECONDS =
102+
computeClockDrift(
103+
Long.getLong("rx3.scheduler.drift-tolerance", 15),
104+
System.getProperty("rx3.scheduler.drift-tolerance-unit", "minutes")
105+
);
106+
107+
/**
108+
* Returns the clock drift tolerance in nanoseconds based on the input selection.
109+
* @param time the time value
110+
* @param timeUnit the time unit string
111+
* @return the time amount in nanoseconds
112+
*/
113+
static long computeClockDrift(long time, String timeUnit) {
114+
if ("seconds".equalsIgnoreCase(timeUnit)) {
115+
return TimeUnit.SECONDS.toNanos(time);
116+
} else if ("milliseconds".equalsIgnoreCase(timeUnit)) {
117+
return TimeUnit.MILLISECONDS.toNanos(time);
118+
}
119+
return TimeUnit.MINUTES.toNanos(time);
100120
}
101121

102122
/**
103123
* Returns the clock drift tolerance in nanoseconds.
104-
* <p>Related system property: {@code rx3.scheduler.drift-tolerance} in minutes.
124+
* <p>Related system properties:
125+
* <ul>
126+
* <li>{@code rx3.scheduler.drift-tolerance}, long, default {@code 15}</li>
127+
* <li>{@code rx3.scheduler.drift-tolerance-unit}, string, default {@code minutes},
128+
* supports {@code seconds} and {@code milliseconds}.
129+
* </ul>
105130
* @return the tolerance in nanoseconds
106131
* @since 2.0
107132
*/
@@ -350,7 +375,7 @@ public <S extends Scheduler & Disposable> S when(@NonNull Function<Flowable<Flow
350375
* based on the relative time between it and {@link #now(TimeUnit)}. However, drifts or changes in the
351376
* system clock would affect this calculation either by scheduling subsequent runs too frequently or too far apart.
352377
* Therefore, the default implementation uses the {@link #clockDriftTolerance()} value (set via
353-
* {@code rx3.scheduler.drift-tolerance} in minutes) to detect a drift in {@link #now(TimeUnit)} and
378+
* {@code rx3.scheduler.drift-tolerance} and {@code rx3.scheduler.drift-tolerance-unit}) to detect a drift in {@link #now(TimeUnit)} and
354379
* re-adjust the absolute/relative time calculation accordingly.
355380
* <p>
356381
* If the {@code Worker} is disposed, the {@code schedule} methods
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2016-present, RxJava Contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.reactivex.rxjava3.core;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import org.junit.Test;
21+
22+
public class SchedulerTest {
23+
24+
@Test
25+
public void clockDriftCalculation() {
26+
assertEquals(100_000_000L, Scheduler.computeClockDrift(100, "milliseconds"));
27+
28+
assertEquals(2_000_000_000L, Scheduler.computeClockDrift(2, "seconds"));
29+
30+
assertEquals(180_000_000_000L, Scheduler.computeClockDrift(3, "minutes"));
31+
32+
assertEquals(240_000_000_000L, Scheduler.computeClockDrift(4, "random"));
33+
34+
assertEquals(300_000_000_000L, Scheduler.computeClockDrift(5, null));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)