Skip to content

student2ua/myTaning

Repository files navigation

education examples

Maven Oracle driver

todo read

private static HttpClient client = HttpClient.newHttpClient();
private static Set<Integer> retryableCodes = Set.of(HTTP_BAD_GATEWAY, HTTP_UNAVAILABLE, HTTP_GATEWAY_TIMEOUT); //расширьте по вкусу

/*
    Дописывайте свои специфичные реализации, кому что надо
 */
public static @Nullable String httpGetStringWithRetries(HttpRequest request, int retryCount, int delay) {
    Optional<HttpResponse<String>> result = httpGetWithRetries(request, retryCount, delay, false, HttpResponse.BodyHandlers.ofString());
    return result.map(HttpResponse::body).orElse(null);
}

public static <T> Optional<HttpResponse<T>> httpGetWithRetries(HttpRequest request,
                                                               int tryCount,
                                                               int delay,
                                                               boolean isExponentDelay,
                                                               HttpResponse.BodyHandler<T> bodyHandler) {
    if (tryCount < 1) {
        throw new RuntimeException("Нельзя меньше 1 попытки");
    }
    if (delay < 0) {
        throw new RuntimeException("Машину времени еще не придумали. Задержка между попытками должна быть неотрицательной.");
    }
    int retryDelay = delay;
    for (int i = 0; i < tryCount; ++i) {
        if (i > 0 && delay > 0) {
            try {
                int jitter = (int) (ThreadLocalRandom.current().nextInt() % (retryDelay * 0.2)) - (int) (retryDelay * 0.1);
                Thread.sleep(retryDelay + jitter);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        HttpResponse<T> result = null;
        try {
            result = client.send(request, bodyHandler);
        } catch (Exception e) {
            if (!(e instanceof IOException))
                throw new RuntimeException(e);
        }
        if (result == null || retryableCodes.contains(result.statusCode())) {
            if (isExponentDelay)
                retryDelay = retryDelay * 2;
            continue;
        }
        return Optional.of(result);
    }
    return Optional.empty();
}

image java lib

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •