Hystrix Retry - Add hystrix retry for database calls

16 views
Skip to first unread message

priyank doshi

unread,
Mar 6, 2020, 9:46:19 AM3/6/20
to HystrixOSS
Hi All,

We are using hystrix for wrapping database calls inside a command. Many of the time, we get command timeout errors due to query couldn't run in specified time period. It can happen due to high load on DB or some network error as well. I am trying to write a generic hystrix retries where we blindly execute hystrix command again if we receive any exception.

I tried following things:
  • Wrap all commands inside a custom retrier object. And within my object, I am calling command.execute().  Problem is I can't execute command.execute() again on same instance. Is there a better way to achieve this?

public class Retry<R> {


private static int MAX_RETRY_COUNT = 3;

private HystrixCommand<R> command;

public HystrixCommandRetryExecutor(HystrixCommand<R> command) {
this.command = command;
}

public R execute() {
R result = null;
for (int currentRequestCount = 1; currentRequestCount <= MAX_RETRY_COUNT; currentRequestCount++) {
try {
int retryAttempt = currentRequestCount - 1;
long sleepTime = 50 * retryAttempt;
TimeUnit.MILLISECONDS.sleep(sleepTime);
result = command.execute();
break;
} catch (Exception e) {
LOG.debug("Command=" + command, e);
}
}
return result;
}

}
Reply all
Reply to author
Forward
0 new messages