Search code examples
timeoutfeign

Feign retry & timeouts


I use both Feign retryer:

    @Bean
    public Retryer retryer() {
        /*
         * It'll retry only a given number of times, will start with some time interval, and then increase it with each retry up to provided maximum.
         * Let's define it with starting interval of 100 milliseconds, the maximum interval of 3 seconds, and the maximum number of attempts of 5:
         */
        return new Retryer.Default(100L, TimeUnit.SECONDS.toMillis(3L), 5);
    }

and custom timeouts:

    @Bean
    public Builder proxyFeignBuilder() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        if (StringUtils.isNotEmpty(proxyHost)) {
            final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
            okHttpClient = new OkHttpClient.Builder().proxy(proxy).build();
        }
        return Feign.builder().options(new Request.Options(feignConnectTimeout, TimeUnit.MILLISECONDS, feignReadTimeout, TimeUnit.MILLISECONDS, true))
                .client(new feign.okhttp.OkHttpClient(okHttpClient));
    }

What I don't understand is the sequence of waiting times in case the request is not successful. Is it as follows?

  1. Wait at most the maximum of feignConnectTimeout and feignReadTimeout depending of the nature of the error.
  2. Then wait 100ms.
  3. Then retry a first time, waiting again as in step 1.
  4. Then wait something between 100ms and 3s
  5. Then retry a second time, waiting again as in step 1.
  6. etc
  7. Then wait 3 seconds (fifth attempt)
  8. Then retry a last time, waiting again as in step 1.

Solution

  • Yes, here is the formula

    long nextMaxInterval() {
          long interval = (long) (period * Math.pow(1.5, attempt - 1));
          return interval > maxPeriod ? maxPeriod : interval;
    }
    

    The number of attempt will decide how long the interval is and it's not over maxPeriod