jersey
  1. jersey
  2. JERSEY-1894

Exponential backoff algorithm in response to HTTP 503 (Service Unavailable)

    Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.0-rc2, 2.3.1
    • Fix Version/s: icebox
    • Component/s: core
    • Labels:
      None

      Description

      Please add the ability to use an Exponential Backoff algorithm in response to 503 (Service Unavailable) responses.

      An Apache-licensed algorithm can be found here: https://code.google.com/p/google-http-java-client/wiki/ExponentialBackoff

        Activity

        Hide
        zero1zero added a comment -

        It sounds like the JAX-RS client needs to allow for retries in general. Is there a way to specify a retry handler at all as of right now?

        I implemented the google exponential backoff for JAX-RS, but it was simply calling a custom callable in a loop.

            public Response exec(RemoteExec exec) {
        
                ExponentialBackOff backoff = new ExponentialBackOff.Builder().build();
        
                long delay = 0;
        
                Response response;
                do {
                    try {
                        Thread.sleep(delay);
        
                        response = exec.exec(target());
        
                        if (response.getStatusInfo().getFamily() == Family.SERVER_ERROR) {
                            log.warn("Server error {} when accessing path {}. Delaying {}ms", response.getStatus(), response.getLocation().toASCIIString(), delay);
                        }
        
                        delay = backoff.nextBackOffMillis();
                    } catch (InterruptedException|IOException e) {
                        throw new RuntimeException("Client request failed", e);
                    }
        
                } while(delay != ExponentialBackOff.STOP && response.getStatusInfo().getFamily() == Family.SERVER_ERROR);
        
                if (response.getStatusInfo().getFamily() == Family.SERVER_ERROR) {
                    throw new IllegalStateException("Client request failed for " + response.getLocation().toASCIIString());
                }
        
                return response;
            }
        
        Show
        zero1zero added a comment - It sounds like the JAX-RS client needs to allow for retries in general. Is there a way to specify a retry handler at all as of right now? I implemented the google exponential backoff for JAX-RS, but it was simply calling a custom callable in a loop. public Response exec(RemoteExec exec) { ExponentialBackOff backoff = new ExponentialBackOff.Builder().build(); long delay = 0; Response response; do { try { Thread .sleep(delay); response = exec.exec(target()); if (response.getStatusInfo().getFamily() == Family.SERVER_ERROR) { log.warn( "Server error {} when accessing path {}. Delaying {}ms" , response.getStatus(), response.getLocation().toASCIIString(), delay); } delay = backoff.nextBackOffMillis(); } catch (InterruptedException|IOException e) { throw new RuntimeException( "Client request failed" , e); } } while (delay != ExponentialBackOff.STOP && response.getStatusInfo().getFamily() == Family.SERVER_ERROR); if (response.getStatusInfo().getFamily() == Family.SERVER_ERROR) { throw new IllegalStateException( "Client request failed for " + response.getLocation().toASCIIString()); } return response; }

          People

          • Assignee:
            Unassigned
            Reporter:
            cowwoc
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: