@Override public boolean canRetry(RetryContext context) { LoadBalancedRetryContext lbContext = (LoadBalancedRetryContext)context; if(lbContext.getRetryCount() == 0 && lbContext.getServiceInstance() == null) { //We haven't even tried to make the request yet so return true so we do lbContext.setServiceInstance(serviceInstanceChooser.choose(serviceName)); return true; } return policy.canRetryNextServer(lbContext); }
@Test public void canRetryBeforeExecution() throws Exception { InterceptorRetryPolicy interceptorRetryPolicy = new InterceptorRetryPolicy(request, policy, serviceInstanceChooser, serviceName); LoadBalancedRetryContext context = mock(LoadBalancedRetryContext.class); when(context.getRetryCount()).thenReturn(0); ServiceInstance serviceInstance = mock(ServiceInstance.class); when(serviceInstanceChooser.choose(eq(serviceName))).thenReturn(serviceInstance); assertThat(interceptorRetryPolicy.canRetry(context), is(true)); verify(context, times(1)).setServiceInstance(eq(serviceInstance)); }
@Override public boolean canRetry(RetryContext context) { LoadBalancedRetryContext lbContext = (LoadBalancedRetryContext)context; if(lbContext.getRetryCount() == 0 && lbContext.getServiceInstance() == null) { //We haven't even tried to make the request yet so return true so we do lbContext.setServiceInstance(serviceInstanceChooser.choose(serviceName)); return true; } return policy.canRetryNextServer(lbContext); }
@Override public void registerThrowable(LoadBalancedRetryContext context, Throwable throwable) { //if this is a circuit tripping exception then notify the load balancer if (lbContext.getRetryHandler().isCircuitTrippingException(throwable)) { updateServerInstanceStats(context); } //Check if we need to ask the load balancer for a new server. //Do this before we increment the counters because the first call to this method //is not a retry it is just an initial failure. if(!canRetrySameServer(context) && canRetryNextServer(context)) { context.setServiceInstance(loadBalanceChooser.choose(serviceId)); } //This method is called regardless of whether we are retrying or making the first request. //Since we do not count the initial request in the retry count we don't reset the counter //until we actually equal the same server count limit. This will allow us to make the initial //request plus the right number of retries. if(sameServerCount >= lbContext.getRetryHandler().getMaxRetriesOnSameServer() && canRetry(context)) { //reset same server since we are moving to a new server sameServerCount = 0; nextServerCount++; if(!canRetryNextServer(context)) { context.setExhaustedOnly(); } } else { sameServerCount++; } }