|
39 | 39 | import java.time.Clock; |
40 | 40 | import java.time.Duration; |
41 | 41 | import java.time.Instant; |
| 42 | +import java.time.temporal.ChronoUnit; |
42 | 43 | import java.util.BitSet; |
43 | 44 | import java.util.EnumSet; |
44 | 45 | import java.util.HashMap; |
@@ -417,24 +418,25 @@ void setPool(SessionPool pool) { |
417 | 418 | } |
418 | 419 |
|
419 | 420 | private void maybeWaitForSessionCreation( |
420 | | - SessionPoolOptions sessionPoolOptions, SettableApiFuture<SessionReference> initialSessionReferenceFuture) { |
| 421 | + SessionPoolOptions sessionPoolOptions, |
| 422 | + SettableApiFuture<SessionReference> initialSessionReferenceFuture) { |
421 | 423 | Duration waitDuration = sessionPoolOptions.getWaitForMinSessions(); |
422 | 424 | SpannerException lastException = null; |
423 | 425 | SettableApiFuture<SessionReference> sessionReferenceFuture = initialSessionReferenceFuture; |
424 | 426 |
|
425 | 427 | if (waitDuration != null && !waitDuration.isZero()) { |
426 | | - Instant endTime = Instant.now().plus(waitDuration); |
427 | | - while (Instant.now().isBefore(endTime)) { |
| 428 | + Instant endTime = Instant.now().plus(waitDuration).truncatedTo(ChronoUnit.MILLIS); |
| 429 | + Duration remainingTime; |
| 430 | + while ((remainingTime = Duration.between(Instant.now(), endTime)).toMillis() > 0) { |
428 | 431 | // If any exception is thrown, then retry the multiplexed session creation |
429 | 432 | if (sessionReferenceFuture == null) { |
430 | 433 | sessionReferenceFuture = SettableApiFuture.create(); |
431 | 434 | asyncCreateMultiplexedSession(sessionReferenceFuture); |
432 | 435 | this.multiplexedSessionReference.set(sessionReferenceFuture); |
433 | 436 | } |
434 | 437 | // Calculate the remaining time pending for the future to wait for multiplexed session |
435 | | - Duration remainingTime = Duration.between(Instant.now(), endTime); |
436 | 438 | System.out.println("Is ended: " + Instant.now().isBefore(endTime)); |
437 | | - System.out.println("Remaining time: " + remainingTime.toMillis()); |
| 439 | + System.out.println("Remaining time: " + remainingTime.toNanos()); |
438 | 440 | try { |
439 | 441 | sessionReferenceFuture.get(remainingTime.toMillis(), TimeUnit.MILLISECONDS); |
440 | 442 | lastException = null; |
|
0 commit comments