Skip to content

Commit e8b4e42

Browse files
committed
Consistently use Instant for next trigger computation.
Deprecate Date-based variant. Closes gh-831
1 parent 95381f3 commit e8b4e42

File tree

5 files changed

+42
-26
lines changed

5 files changed

+42
-26
lines changed

spring-vault-core/src/main/java/org/springframework/vault/authentication/LifecycleAwareSessionManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ private void scheduleRenewal() {
366366
}
367367

368368
private OneShotTrigger createTrigger(TokenWrapper tokenWrapper) {
369-
370-
return new OneShotTrigger(getRefreshTrigger().nextExecutionTime((LoginToken) tokenWrapper.getToken()));
369+
return new OneShotTrigger(getRefreshTrigger().nextExecution((LoginToken) tokenWrapper.getToken()));
371370
}
372371

373372
private static String format(String message, RuntimeException e) {

spring-vault-core/src/main/java/org/springframework/vault/authentication/LifecycleAwareSessionManagerSupport.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.vault.authentication;
1717

18+
import java.time.Clock;
1819
import java.time.Duration;
1920
import java.time.Instant;
2021
import java.util.Date;
@@ -180,19 +181,20 @@ protected static class OneShotTrigger implements Trigger {
180181

181182
private final AtomicBoolean fired = new AtomicBoolean();
182183

183-
private final Date nextExecutionTime;
184+
@Nullable
185+
private final Instant nextExecutionTime;
184186

185-
public OneShotTrigger(Date nextExecutionTime) {
187+
public OneShotTrigger(@Nullable Date nextExecutionTime) {
188+
this(nextExecutionTime != null ? nextExecutionTime.toInstant() : null);
189+
}
190+
191+
public OneShotTrigger(@Nullable Instant nextExecutionTime) {
186192
this.nextExecutionTime = nextExecutionTime;
187193
}
188194

189195
@Override
190196
public Instant nextExecution(TriggerContext triggerContext) {
191-
if (this.fired.compareAndSet(false, true)) {
192-
return this.nextExecutionTime.toInstant();
193-
}
194-
195-
return null;
197+
return this.fired.compareAndSet(false, true) ? this.nextExecutionTime : null;
196198
}
197199

198200
}
@@ -207,9 +209,25 @@ public interface RefreshTrigger {
207209
* Determine the next execution time according to the given trigger context.
208210
* @param loginToken login token encapsulating renewability and lease duration.
209211
* @return the next execution time as defined by the trigger, or {@code null} if
210-
* the trigger won't fire anymore
212+
* the trigger won't fire anymore.
213+
* @deprecated since 3.1, use {@link #nextExecution(LoginToken) instead}.
211214
*/
212-
Date nextExecutionTime(LoginToken loginToken);
215+
@Nullable
216+
@Deprecated(since = "3.1")
217+
default Date nextExecutionTime(LoginToken loginToken) {
218+
Instant instant = nextExecution(loginToken);
219+
return instant != null ? Date.from(instant) : null;
220+
}
221+
222+
/**
223+
* Determine the next execution time according to the given trigger context.
224+
* @param loginToken login token encapsulating renewability and lease duration.
225+
* @return the next execution time as defined by the trigger, or {@code null} if
226+
* the trigger won't fire anymore.
227+
* @since 3.1
228+
*/
229+
@Nullable
230+
Instant nextExecution(LoginToken loginToken);
213231

214232
/**
215233
* Returns the minimum TTL duration to consider a token valid after renewal.
@@ -231,6 +249,8 @@ public interface RefreshTrigger {
231249
*/
232250
public static class FixedTimeoutRefreshTrigger implements RefreshTrigger {
233251

252+
private static final Clock CLOCK = Clock.systemDefaultZone();
253+
234254
private static final Duration ONE_SECOND = Duration.ofSeconds(1);
235255

236256
private final Duration duration;
@@ -289,12 +309,12 @@ public FixedTimeoutRefreshTrigger(Duration refreshBeforeExpiry, Duration expiryT
289309
}
290310

291311
@Override
292-
public Date nextExecutionTime(LoginToken loginToken) {
312+
public Instant nextExecution(LoginToken loginToken) {
293313

294314
long milliseconds = Math.max(ONE_SECOND.toMillis(),
295315
loginToken.getLeaseDuration().toMillis() - this.duration.toMillis());
296316

297-
return new Date(System.currentTimeMillis() + milliseconds);
317+
return CLOCK.instant().plusMillis(milliseconds);
298318
}
299319

300320
@Override

spring-vault-core/src/main/java/org/springframework/vault/authentication/ReactiveLifecycleAwareSessionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ private void scheduleRenewal(VaultToken token) {
395395

396396
private OneShotTrigger createTrigger(VaultToken token) {
397397

398-
return new OneShotTrigger(getRefreshTrigger().nextExecutionTime((LoginToken) token));
398+
return new OneShotTrigger(getRefreshTrigger().nextExecution((LoginToken) token));
399399
}
400400

401401
private static Mono<VaultToken> augmentWithSelfLookup(WebClient webClient, VaultToken token) {

spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.concurrent.CopyOnWriteArrayList;
3030
import java.util.concurrent.Executor;
3131
import java.util.concurrent.ScheduledFuture;
32-
import java.util.concurrent.TimeUnit;
3332
import java.util.concurrent.atomic.AtomicInteger;
3433
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
3534
import java.util.concurrent.atomic.AtomicReference;
@@ -1070,6 +1069,8 @@ public boolean isExpired(Clock clock) {
10701069
*/
10711070
static class OneShotTrigger implements Trigger {
10721071

1072+
private static final Clock CLOCK = Clock.systemDefaultZone();
1073+
10731074
private static final AtomicIntegerFieldUpdater<OneShotTrigger> UPDATER = AtomicIntegerFieldUpdater
10741075
.newUpdater(OneShotTrigger.class, "status");
10751076

@@ -1089,11 +1090,8 @@ static class OneShotTrigger implements Trigger {
10891090
@Nullable
10901091
@Override
10911092
public Instant nextExecution(TriggerContext triggerContext) {
1092-
if (UPDATER.compareAndSet(this, STATUS_ARMED, STATUS_FIRED)) {
1093-
return Instant.ofEpochMilli(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(this.seconds));
1094-
}
1095-
1096-
return null;
1093+
return UPDATER.compareAndSet(this, STATUS_ARMED, STATUS_FIRED) ? CLOCK.instant().plusSeconds(this.seconds)
1094+
: null;
10971095
}
10981096

10991097
}

spring-vault-core/src/test/java/org/springframework/vault/authentication/LifecycleAwareSessionManagerSupportUnitTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.vault.authentication;
1717

1818
import java.time.Duration;
19+
import java.time.Instant;
1920
import java.util.Date;
2021
import java.util.concurrent.TimeUnit;
2122

@@ -37,19 +38,17 @@ void shouldScheduleNextExecutionTimeCorrectly() {
3738

3839
FixedTimeoutRefreshTrigger trigger = new FixedTimeoutRefreshTrigger(5, TimeUnit.SECONDS);
3940

40-
Date nextExecutionTime = trigger.nextExecutionTime(LoginToken.of("foo".toCharArray(), Duration.ofMinutes(1)));
41-
assertThat(nextExecutionTime).isBetween(new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(52)),
42-
new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(56)));
41+
Instant nextExecutionTime = trigger.nextExecution(LoginToken.of("foo".toCharArray(), Duration.ofMinutes(1)));
42+
assertThat(nextExecutionTime).isBetween(Instant.now().plusSeconds(52), Instant.now().plusSeconds(56));
4343
}
4444

4545
@Test
4646
void shouldScheduleNextExecutionIfValidityLessThanTimeout() {
4747

4848
FixedTimeoutRefreshTrigger trigger = new FixedTimeoutRefreshTrigger(5, TimeUnit.SECONDS);
4949

50-
Date nextExecutionTime = trigger.nextExecutionTime(LoginToken.of("foo".toCharArray(), Duration.ofSeconds(2)));
51-
assertThat(nextExecutionTime).isBetween(new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(0)),
52-
new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(2)));
50+
Instant nextExecutionTime = trigger.nextExecution(LoginToken.of("foo".toCharArray(), Duration.ofSeconds(2)));
51+
assertThat(nextExecutionTime).isBetween(Instant.now(), Instant.now().plusSeconds(2));
5352
}
5453

5554
}

0 commit comments

Comments
 (0)