Retry with custom logic #1599
Unanswered
krezchikov42
asked this question in
Q&A
Replies: 1 comment
-
This is an attempt I had to do retries myself, but I don't think this would work correctly. public Uni<Optional<Event>> retryGetEvent(String key, int maxAttempts, int initialDelayMs) {
return Uni.createFrom().deferred(() -> {
Uni<Optional<Event>> uni = getEvent(key);
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
int finalAttempt = attempt;
uni = uni.onItem().transformToUni(event -> {
if (event.isPresent()) {
return Uni.createFrom().item(event); // Success, return the event
} else if (finalAttempt < maxAttempts) {
int delay = initialDelayMs * (1 << (finalAttempt - 1)); // Exponential backoff
LOGGER.warnf("Event not found, retry attempt %d with delay %d for event with key: %s", finalAttempt, delay, key);
return Uni.createFrom().voidItem().onItem().delayIt().by(Duration.ofMillis(delay))
.onItem().transformToUni(ignored -> getEvent(key));
} else {
return Uni.createFrom().item(Optional.empty()); // All retries exhausted, return empty
}
});
}
return uni;
});
} where this is the signature for |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello there.
Let's say I am doing a redis
mget
on a 1000 events. For some reason it is able to fetch 980 events, but it it doesn't find 20 events. We can safely assume that this is because of replication lag. I would then like to retry on these last 20 events up to 3 times within a 3 second period to get those responses back. This use case is more complicated than I believe.onFailure().retry().atMost(2)
or anything in this doc allows for because that would have you retry the whole 1000 event request. Does anyone have any ideas on how you could do something like this?Just being able to retry on a uni without failing would be helpful as well, but even that confuses me without doing an await.
Beta Was this translation helpful? Give feedback.
All reactions