Search in sources :

Example 1 with BaragonResponse

use of com.hubspot.baragon.models.BaragonResponse in project Singularity by HubSpot.

the class BaragonLoadBalancerClientImpl method sendRequest.

private LoadBalancerUpdateHolder sendRequest(LoadBalancerRequestId loadBalancerRequestId, Request request, BaragonRequestState onFailure) {
    try {
        LOG.trace("Sending LB {} request for {} to {}", request.getMethod(), loadBalancerRequestId, request.getUrl());
        ListenableFuture<Response> future = httpClient.executeRequest(request);
        Response response = future.get(loadBalancerTimeoutMillis, TimeUnit.MILLISECONDS);
        LOG.trace("LB {} request {} returned with code {}", request.getMethod(), loadBalancerRequestId, response.getStatusCode());
        if (response.getStatusCode() == 504) {
            return new LoadBalancerUpdateHolder(BaragonRequestState.UNKNOWN, Optional.of(String.format("LB %s request %s timed out", request.getMethod(), loadBalancerRequestId)));
        } else if (!JavaUtils.isHttpSuccess(response.getStatusCode())) {
            String body = response.getResponseBody();
            LOG.info("LB {} request {} failed with code {}: {}", request.getMethod(), loadBalancerRequestId, response.getStatusCode(), body);
            return new LoadBalancerUpdateHolder(onFailure, Optional.of(String.format("Response status code %s: %s", response.getStatusCode(), body)));
        }
        BaragonResponse lbResponse = readResponse(response);
        return new LoadBalancerUpdateHolder(lbResponse.getLoadBalancerState(), lbResponse.getMessage().toJavaUtil());
    } catch (TimeoutException te) {
        LOG.trace("LB {} request {} timed out after waiting {}", request.getMethod(), loadBalancerRequestId, JavaUtils.durationFromMillis(loadBalancerTimeoutMillis));
        return new LoadBalancerUpdateHolder(BaragonRequestState.UNKNOWN, Optional.of(String.format("Timed out after %s", JavaUtils.durationFromMillis(loadBalancerTimeoutMillis))));
    } catch (Throwable t) {
        LOG.error("LB {} request {} to {} threw error", request.getMethod(), loadBalancerRequestId, request.getUrl(), t);
        return new LoadBalancerUpdateHolder(BaragonRequestState.UNKNOWN, Optional.of(String.format("Exception %s - %s", t.getClass().getSimpleName(), t.getMessage())));
    }
}
Also used : Response(com.ning.http.client.Response) BaragonResponse(com.hubspot.baragon.models.BaragonResponse) BaragonResponse(com.hubspot.baragon.models.BaragonResponse) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with BaragonResponse

use of com.hubspot.baragon.models.BaragonResponse in project Singularity by HubSpot.

the class LoadBalancerClientImpl method sendRequest.

private LoadBalancerUpdateHolder sendRequest(LoadBalancerRequestId loadBalancerRequestId, Request request, BaragonRequestState onFailure) {
    try {
        LOG.trace("Sending LB {} request for {} to {}", request.getMethod(), loadBalancerRequestId, request.getUrl());
        ListenableFuture<Response> future = httpClient.executeRequest(request);
        Response response = future.get(loadBalancerTimeoutMillis, TimeUnit.MILLISECONDS);
        LOG.trace("LB {} request {} returned with code {}", request.getMethod(), loadBalancerRequestId, response.getStatusCode());
        if (response.getStatusCode() == 504) {
            return new LoadBalancerUpdateHolder(BaragonRequestState.UNKNOWN, Optional.of(String.format("LB %s request %s timed out", request.getMethod(), loadBalancerRequestId)));
        } else if (!JavaUtils.isHttpSuccess(response.getStatusCode())) {
            return new LoadBalancerUpdateHolder(onFailure, Optional.of(String.format("Response status code %s", response.getStatusCode())));
        }
        BaragonResponse lbResponse = readResponse(response);
        return new LoadBalancerUpdateHolder(lbResponse.getLoadBalancerState(), lbResponse.getMessage());
    } catch (TimeoutException te) {
        LOG.trace("LB {} request {} timed out after waiting {}", request.getMethod(), loadBalancerRequestId, JavaUtils.durationFromMillis(loadBalancerTimeoutMillis));
        return new LoadBalancerUpdateHolder(BaragonRequestState.UNKNOWN, Optional.of(String.format("Timed out after %s", JavaUtils.durationFromMillis(loadBalancerTimeoutMillis))));
    } catch (Throwable t) {
        LOG.error("LB {} request {} to {} threw error", request.getMethod(), loadBalancerRequestId, request.getUrl(), t);
        return new LoadBalancerUpdateHolder(BaragonRequestState.UNKNOWN, Optional.of(String.format("Exception %s - %s", t.getClass().getSimpleName(), t.getMessage())));
    }
}
Also used : Response(com.ning.http.client.Response) BaragonResponse(com.hubspot.baragon.models.BaragonResponse) BaragonResponse(com.hubspot.baragon.models.BaragonResponse) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

BaragonResponse (com.hubspot.baragon.models.BaragonResponse)2 Response (com.ning.http.client.Response)2 TimeoutException (java.util.concurrent.TimeoutException)2