-
Notifications
You must be signed in to change notification settings - Fork 3
Stop HealthPoller from polling a closed client after cancellation #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
| import com.google.gson.Gson; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import reactor.core.Disposable; | ||
| import reactor.core.Disposables; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
|
|
@@ -19,6 +21,7 @@ | |
|
|
||
| public class HealthPoller implements AutoCloseable { | ||
| private final int StartingIndex = 0; | ||
| private final Duration RetryDelay = Duration.ofSeconds(5); | ||
|
|
||
| private final String healthUrl = "%s/v1/health/service/%s?passing=true&index=%d&wait=1m"; | ||
| private final Gson gson = new Gson(); | ||
|
|
@@ -35,32 +38,45 @@ public HealthPoller(HttpClient client, String consulHost) { | |
|
|
||
| public Flux<Set<Address>> start(String serviceName) { | ||
| return Flux.create(emitter -> { | ||
| Disposable.Swap backoff = Disposables.swap(); | ||
| emitter.onDispose(backoff); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to confirm my understanding: if an issue comes up while long polling Consul, the retry delay will be registered with this Sanity check: what happens if multiple retries are necessary? It will just keep updating
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each poll failure registers a 5s retry via Great question regarding the multiple retries. Yeah, it will keep updating |
||
|
|
||
| Recursive<Function<Integer, CompletableFuture<Integer>>> r = new Recursive<>(); | ||
| r.func = | ||
| index -> | ||
| client | ||
| .get(String.format(healthUrl, consulHost, serviceName, index)) | ||
| .thenApply( | ||
| res -> { | ||
| int nextIdx = getNextIdxFrom(res); | ||
|
|
||
| if (nextIdx > index) { | ||
| lastResponse = getAddressesFrom(res.getBody()); | ||
| emitter.next(lastResponse); | ||
| } | ||
|
|
||
| return nextIdx; | ||
| }) | ||
| .whenComplete( | ||
| (nextId, th) -> { | ||
| if (th != null) { | ||
| logger.warn("Got an exception while long polling Consul.", th); | ||
| Mono.delay(Duration.ofSeconds(5)) | ||
| .subscribe(i -> r.func.apply(StartingIndex)); | ||
| } else { | ||
| r.func.apply(nextId); | ||
| } | ||
| }); | ||
| index -> { | ||
| if (emitter.isCancelled()) { | ||
| return CompletableFuture.completedFuture(index); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we return a completed future containing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with a completed future because the returned future is never actually used. The recursion is driven by the It felt like the most straightforward no-op: basically cleanly stopping because the subscription is gone.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha, that makes sense. I think it might be more clear to use |
||
| } | ||
|
|
||
| return client | ||
| .get(String.format(healthUrl, consulHost, serviceName, index)) | ||
| .thenApply( | ||
| res -> { | ||
| int nextIdx = getNextIdxFrom(res); | ||
|
|
||
| if (nextIdx > index) { | ||
| lastResponse = getAddressesFrom(res.getBody()); | ||
| emitter.next(lastResponse); | ||
| } | ||
|
|
||
| return nextIdx; | ||
| }) | ||
| .whenComplete( | ||
| (nextId, th) -> { | ||
| if (emitter.isCancelled()) { | ||
| return; | ||
| } | ||
|
|
||
| if (th != null) { | ||
| logger.warn("Got an exception while long polling Consul.", th); | ||
| backoff.update( | ||
| Mono.delay(RetryDelay) | ||
| .subscribe(i -> r.func.apply(StartingIndex))); | ||
| } else { | ||
| r.func.apply(nextId); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| r.func.apply(StartingIndex); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: could this be
final?