1313package io .kubernetes .client .extended .workqueue ;
1414
1515import java .time .Duration ;
16- import java .time .Instant ;
17- import java .time .temporal .Temporal ;
1816import java .util .Map ;
1917import java .util .concurrent .BlockingQueue ;
2018import java .util .concurrent .ConcurrentHashMap ;
@@ -35,10 +33,14 @@ public class DefaultDelayingQueue<T> extends DefaultWorkQueue<T> implements Dela
3533 private DelayQueue <WaitForEntry <T >> delayQueue ;
3634 private ConcurrentMap <T , WaitForEntry <T >> waitingEntryByData ;
3735 protected BlockingQueue <WaitForEntry <T >> waitingForAddQueue ;
38- private Supplier <Instant > timeSource ;
36+ private Supplier <Long > timeSource ;
37+
38+ private static Long now () {
39+ return System .nanoTime () / 1000000 ;
40+ }
3941
4042 public DefaultDelayingQueue (ExecutorService waitingWorker ) {
41- this .timeSource = Instant ::now ;
43+ this .timeSource = DefaultDelayingQueue ::now ;
4244 this .delayQueue = new DelayQueue <>();
4345 this .waitingEntryByData = new ConcurrentHashMap <>();
4446 this .waitingForAddQueue = new LinkedBlockingQueue <>(1000 );
@@ -61,12 +63,12 @@ public void addAfter(T item, Duration duration) {
6163 return ;
6264 }
6365 WaitForEntry <T > entry =
64- new WaitForEntry <>(item , duration . addTo ( this .timeSource .get ()), this .timeSource );
66+ new WaitForEntry <>(item , this .timeSource .get () + duration . toMillis ( ), this .timeSource );
6567 this .waitingForAddQueue .offer (entry );
6668 }
6769
6870 // Visible for testing
69- protected void injectTimeSource (Supplier <Instant > fn ) {
71+ protected void injectTimeSource (Supplier <Long > fn ) {
7072 this .timeSource = fn ;
7173 }
7274
@@ -87,21 +89,21 @@ private void waitingLoop() {
8789 // a. if ready, remove it from the delay-queue and push it into underlying
8890 // work-queue
8991 // b. if not, refresh the next ready-at time.
90- Instant now = this .timeSource .get ();
91- if (!Duration . between ( entry .readyAtMillis , now ). isNegative ( )) {
92+ long now = this .timeSource .get ();
93+ if (!(( now - entry .readyAtMillis ) < 0 )) {
9294 delayQueue .remove (entry );
9395 super .add (entry .data );
9496 this .waitingEntryByData .remove (entry .data );
9597 continue ;
9698 } else {
97- nextReadyAt = Duration .between ( now , entry .readyAtMillis );
99+ nextReadyAt = Duration .ofMillis ( entry .readyAtMillis - now );
98100 }
99101 }
100102
101103 WaitForEntry <T > waitForEntry =
102104 waitingForAddQueue .poll (nextReadyAt .toMillis (), TimeUnit .MILLISECONDS );
103105 if (waitForEntry != null ) {
104- if (Duration . between ( waitForEntry . readyAtMillis , this .timeSource .get ()). isNegative () ) {
106+ if (this .timeSource .get () - waitForEntry . readyAtMillis < 0 ) {
105107 // the item is not yet ready, insert it to the delay-queue
106108 insert (this .delayQueue , this .waitingEntryByData , waitForEntry );
107109 } else {
@@ -119,7 +121,7 @@ private void insert(
119121 DelayQueue <WaitForEntry <T >> q , Map <T , WaitForEntry <T >> knownEntries , WaitForEntry entry ) {
120122 WaitForEntry existing = knownEntries .get ((T ) entry .data );
121123 if (existing != null ) {
122- if (Duration . between ( existing .readyAtMillis , entry .readyAtMillis ). isNegative () ) {
124+ if (( entry .readyAtMillis - existing .readyAtMillis ) < 0 ) {
123125 q .remove (existing );
124126 existing .readyAtMillis = entry .readyAtMillis ;
125127 q .add (existing );
@@ -135,20 +137,20 @@ private void insert(
135137 // WaitForEntry holds the data to add and the time it should be added.
136138 private static class WaitForEntry <T > implements Delayed {
137139
138- private WaitForEntry (T data , Temporal readyAtMillis , Supplier <Instant > timeSource ) {
140+ private WaitForEntry (T data , long readyAtMillis , Supplier <Long > timeSource ) {
139141 this .data = data ;
140142 this .readyAtMillis = readyAtMillis ;
141143 this .timeSource = timeSource ;
142144 }
143145
144146 private T data ;
145- private Temporal readyAtMillis ;
146- private Supplier <Instant > timeSource ;
147+ private long readyAtMillis ;
148+ private Supplier <Long > timeSource ;
147149
148150 @ Override
149151 public long getDelay (TimeUnit unit ) {
150- Duration duration = Duration . between ( this .timeSource .get (), readyAtMillis );
151- return unit .convert (duration . toMillis () , TimeUnit .MILLISECONDS );
152+ long duration = readyAtMillis - this .timeSource .get ();
153+ return unit .convert (duration , TimeUnit .MILLISECONDS );
152154 }
153155
154156 @ Override
0 commit comments