Skip to content

Commit b430529

Browse files
Switch initialDelay to Duration. (#768)
1 parent cfeb38a commit b430529

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

docs/src/main/asciidoc/_configprops.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
|spring.cloud.loadbalancer.cache.capacity | 256 | Initial cache capacity expressed as int.
3232
|spring.cloud.loadbalancer.cache.enabled | true | Enables Spring Cloud LoadBalancer caching mechanism.
3333
|spring.cloud.loadbalancer.cache.ttl | 35s | Time To Live - time counted from writing of the record, after which cache entries are expired, expressed as a {@link Duration}. The property {@link String} has to be in keeping with the appropriate syntax as specified in Spring Boot <code>StringToDurationConverter</code>. @see <a href= "https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/StringToDurationConverter.java">StringToDurationConverter.java</a>
34-
|spring.cloud.loadbalancer.health-check.initial-delay | 0 | Initial delay value for the HealthCheck scheduler.
34+
|spring.cloud.loadbalancer.health-check.initial-delay | null | Initial delay value for the HealthCheck scheduler.
3535
|spring.cloud.loadbalancer.health-check.interval | 25s | Interval for rerunning the HealthCheck scheduler.
3636
|spring.cloud.loadbalancer.health-check.path | null | null
3737
|spring.cloud.loadbalancer.retry.enabled | true | null

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerProperties.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static class HealthCheck {
4949
/**
5050
* Initial delay value for the HealthCheck scheduler.
5151
*/
52-
private int initialDelay = 0;
52+
private Duration initialDelay = Duration.ZERO;
5353

5454
/**
5555
* Interval for rerunning the HealthCheck scheduler.
@@ -58,11 +58,11 @@ public static class HealthCheck {
5858

5959
private Map<String, String> path = new LinkedCaseInsensitiveMap<>();
6060

61-
public int getInitialDelay() {
61+
public Duration getInitialDelay() {
6262
return initialDelay;
6363
}
6464

65-
public void setInitialDelay(int initialDelay) {
65+
public void setInitialDelay(Duration initialDelay) {
6666
this.initialDelay = initialDelay;
6767
}
6868

spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplier.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.cloud.loadbalancer.core;
1818

19-
import java.time.Duration;
2019
import java.util.ArrayList;
2120
import java.util.Collections;
2221
import java.util.List;
@@ -69,7 +68,7 @@ public HealthCheckServiceInstanceListSupplier(ServiceInstanceListSupplier delega
6968
"/actuator/health");
7069
this.webClient = webClient;
7170
aliveInstancesReplay = Flux.defer(delegate)
72-
.delaySubscription(Duration.ofMillis(healthCheck.getInitialDelay()))
71+
.delaySubscription(healthCheck.getInitialDelay())
7372
.switchMap(serviceInstances -> healthCheckFlux(serviceInstances).map(
7473
alive -> Collections.unmodifiableList(new ArrayList<>(alive))))
7574
.replay(1).refCount(1);

spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void setUp() {
7979
}
8080

8181
@AfterEach
82-
void tearDown() throws Exception {
82+
void tearDown() {
8383
if (listSupplier != null) {
8484
listSupplier.destroy();
8585
listSupplier = null;
@@ -132,7 +132,7 @@ void shouldReturnFalseIfEndpointNotFound() {
132132

133133
@Test
134134
void shouldReturnOnlyAliveService() {
135-
healthCheck.setInitialDelay(1000);
135+
healthCheck.setInitialDelay(Duration.ofSeconds(1));
136136

137137
ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1",
138138
SERVICE_ID, "127.0.0.1", port, false);
@@ -160,16 +160,15 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
160160
};
161161

162162
return listSupplier.get();
163-
}).expectSubscription()
164-
.expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay()))
163+
}).expectSubscription().expectNoEvent(healthCheck.getInitialDelay())
165164
.expectNext(Lists.list(serviceInstance1))
166165
.expectNoEvent(healthCheck.getInterval()).thenCancel()
167166
.verify(VERIFY_TIMEOUT);
168167
}
169168

170169
@Test
171170
void shouldEmitOnEachAliveServiceInBatch() {
172-
healthCheck.setInitialDelay(1000);
171+
healthCheck.setInitialDelay(Duration.ofSeconds(1));
173172
ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1",
174173
SERVICE_ID, "127.0.0.1", port, false);
175174
ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2",
@@ -196,8 +195,7 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
196195
};
197196

198197
return listSupplier.get();
199-
}).expectSubscription()
200-
.expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay()))
198+
}).expectSubscription().expectNoEvent(healthCheck.getInitialDelay())
201199
.expectNext(Lists.list(serviceInstance1))
202200
.expectNext(Lists.list(serviceInstance1, serviceInstance2))
203201
.expectNoEvent(healthCheck.getInterval()).thenCancel()
@@ -206,7 +204,7 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
206204

207205
@Test
208206
void shouldNotFailIfIsAliveReturnsError() {
209-
healthCheck.setInitialDelay(1000);
207+
healthCheck.setInitialDelay(Duration.ofSeconds(1));
210208
ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1",
211209
SERVICE_ID, "127.0.0.1", port, false);
212210
ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2",
@@ -234,16 +232,15 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
234232
};
235233

236234
return listSupplier.get();
237-
}).expectSubscription()
238-
.expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay()))
235+
}).expectSubscription().expectNoEvent(healthCheck.getInitialDelay())
239236
.expectNext(Lists.list(serviceInstance1))
240237
.expectNoEvent(healthCheck.getInterval()).thenCancel()
241238
.verify(VERIFY_TIMEOUT);
242239
}
243240

244241
@Test
245242
void shouldEmitAllInstancesIfAllIsAliveChecksFailed() {
246-
healthCheck.setInitialDelay(1000);
243+
healthCheck.setInitialDelay(Duration.ofSeconds(1));
247244
ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1",
248245
SERVICE_ID, "127.0.0.1", port, false);
249246
ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2",
@@ -269,15 +266,14 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
269266
};
270267

271268
return listSupplier.get();
272-
}).expectSubscription()
273-
.expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay()))
269+
}).expectSubscription().expectNoEvent(healthCheck.getInitialDelay())
274270
.expectNext(Lists.list()).expectNoEvent(healthCheck.getInterval())
275271
.thenCancel().verify(VERIFY_TIMEOUT);
276272
}
277273

278274
@Test
279275
void shouldMakeInitialDaleyAfterPropertiesSet() {
280-
healthCheck.setInitialDelay(1000);
276+
healthCheck.setInitialDelay(Duration.ofSeconds(1));
281277
ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1",
282278
SERVICE_ID, "127.0.0.1", port, false);
283279

@@ -298,16 +294,15 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
298294
listSupplier.afterPropertiesSet();
299295

300296
return listSupplier.get();
301-
}).expectSubscription()
302-
.expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay()))
297+
}).expectSubscription().expectNoEvent(healthCheck.getInitialDelay())
303298
.expectNext(Lists.list(serviceInstance1))
304299
.expectNoEvent(healthCheck.getInterval()).thenCancel()
305300
.verify(VERIFY_TIMEOUT);
306301
}
307302

308303
@Test
309304
void shouldRepeatIsAliveChecksIndefinitely() {
310-
healthCheck.setInitialDelay(1000);
305+
healthCheck.setInitialDelay(Duration.ofSeconds(1));
311306
ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1",
312307
SERVICE_ID, "127.0.0.1", port, false);
313308
ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2",
@@ -336,8 +331,7 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
336331
};
337332

338333
return listSupplier.get();
339-
}).expectSubscription()
340-
.expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay()))
334+
}).expectSubscription().expectNoEvent(healthCheck.getInitialDelay())
341335
.expectNext(Lists.list()).expectNoEvent(healthCheck.getInterval())
342336
.expectNext(Lists.list(serviceInstance1))
343337
.expectNoEvent(healthCheck.getInterval())
@@ -347,7 +341,7 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
347341

348342
@Test
349343
void shouldTimeoutIsAliveCheck() {
350-
healthCheck.setInitialDelay(1000);
344+
healthCheck.setInitialDelay(Duration.ofSeconds(1));
351345
ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1",
352346
SERVICE_ID, "127.0.0.1", port, false);
353347

@@ -372,8 +366,7 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
372366
};
373367

374368
return listSupplier.get();
375-
}).expectSubscription()
376-
.expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay()))
369+
}).expectSubscription().expectNoEvent(healthCheck.getInitialDelay())
377370
.expectNoEvent(healthCheck.getInterval()).expectNext(Lists.list())
378371
.expectNoEvent(healthCheck.getInterval())
379372
.expectNext(Lists.list(serviceInstance1))
@@ -384,7 +377,7 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
384377

385378
@Test
386379
void shouldUpdateInstances() {
387-
healthCheck.setInitialDelay(1000);
380+
healthCheck.setInitialDelay(Duration.ofSeconds(1));
388381
ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1",
389382
SERVICE_ID, "127.0.0.1", port, false);
390383
ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2",
@@ -409,8 +402,7 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
409402
};
410403

411404
return listSupplier.get();
412-
}).expectSubscription()
413-
.expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay()))
405+
}).expectSubscription().expectNoEvent(healthCheck.getInitialDelay())
414406
.expectNext(Lists.list(serviceInstance1))
415407
.thenAwait(healthCheck.getInterval().dividedBy(2))
416408
.expectNext(Lists.list(serviceInstance1))
@@ -423,7 +415,7 @@ protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
423415

424416
@Test
425417
void shouldCacheResultIfAfterPropertiesSetInvoked() {
426-
healthCheck.setInitialDelay(1000);
418+
healthCheck.setInitialDelay(Duration.ofSeconds(1));
427419
ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1",
428420
SERVICE_ID, "127.0.0.1", port, false);
429421

@@ -454,8 +446,7 @@ protected Flux<List<ServiceInstance>> healthCheckFlux(
454446
listSupplier.afterPropertiesSet();
455447

456448
return listSupplier.get().take(1).concatWith(listSupplier.get().take(1));
457-
}).expectSubscription()
458-
.expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay()))
449+
}).expectSubscription().expectNoEvent(healthCheck.getInitialDelay())
459450
.expectNext(Lists.list(serviceInstance1))
460451
.expectNext(Lists.list(serviceInstance1)).thenCancel()
461452
.verify(VERIFY_TIMEOUT);

0 commit comments

Comments
 (0)