Search in sources :

Example 6 with RetryHandler

use of com.netflix.client.RetryHandler in project ribbon by Netflix.

the class LoadBalancerContext method noteResponse.

/**
 * This is called after a response is received from the client
 * to update related stats.
 */
protected void noteResponse(ServerStats stats, ClientRequest request, Object response, long responseTime) {
    if (stats == null) {
        return;
    }
    try {
        recordStats(stats, responseTime);
        RetryHandler errorHandler = getRetryHandler();
        if (errorHandler != null && response != null) {
            stats.clearSuccessiveConnectionFailureCount();
        }
    } catch (Exception ex) {
        logger.error("Error noting stats for client {}", clientName, ex);
    }
}
Also used : DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) RetryHandler(com.netflix.client.RetryHandler) URISyntaxException(java.net.URISyntaxException) ClientException(com.netflix.client.ClientException)

Example 7 with RetryHandler

use of com.netflix.client.RetryHandler in project ribbon by Netflix.

the class LoadBalancerContext method noteRequestCompletion.

/**
 * This is called after a response is received or an exception is thrown from the client
 * to update related stats.
 */
public void noteRequestCompletion(ServerStats stats, Object response, Throwable e, long responseTime, RetryHandler errorHandler) {
    if (stats == null) {
        return;
    }
    try {
        recordStats(stats, responseTime);
        RetryHandler callErrorHandler = errorHandler == null ? getRetryHandler() : errorHandler;
        if (callErrorHandler != null && response != null) {
            stats.clearSuccessiveConnectionFailureCount();
        } else if (callErrorHandler != null && e != null) {
            if (callErrorHandler.isCircuitTrippingException(e)) {
                stats.incrementSuccessiveConnectionFailureCount();
                stats.addToFailureCount();
            } else {
                stats.clearSuccessiveConnectionFailureCount();
            }
        }
    } catch (Exception ex) {
        logger.error("Error noting stats for client {}", clientName, ex);
    }
}
Also used : DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) RetryHandler(com.netflix.client.RetryHandler) URISyntaxException(java.net.URISyntaxException) ClientException(com.netflix.client.ClientException)

Example 8 with RetryHandler

use of com.netflix.client.RetryHandler in project feign-reactive by kptfh.

the class LoadBalancingReactiveHttpClientTest method loadBalancingWithRetry.

private void loadBalancingWithRetry(int failedAttemptsNo, int retryOnSame, int retryOnNext) throws IOException, InterruptedException {
    String body = "success!";
    Stream.of(server1, server2).forEach(server -> {
        mockSuccessAfterSeveralAttempts(server, "/", failedAttemptsNo, 503, aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(body));
    });
    String serverListKey = serviceName + ".ribbon.listOfServers";
    getConfigInstance().setProperty(serverListKey, "localhost:" + server1.port() + "," + "localhost:" + server2.port());
    RetryHandler retryHandler = new RequestSpecificRetryHandler(true, true, new DefaultLoadBalancerRetryHandler(retryOnSame, retryOnNext, true), null);
    TestInterface client = CloudReactiveFeign.<TestInterface>builder().webClient(WebClient.create()).setLoadBalancerCommand(LoadBalancerCommand.builder().withLoadBalancer(AbstractLoadBalancer.class.cast(getNamedLoadBalancer(serviceName))).withRetryHandler(retryHandler).build()).target(TestInterface.class, "http://" + serviceName);
    try {
        String result = client.get().block();
        assertThat(result).isEqualTo(body);
    } finally {
        getConfigInstance().clearProperty(serverListKey);
    }
}
Also used : DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) RequestSpecificRetryHandler(com.netflix.client.RequestSpecificRetryHandler) RetryHandler(com.netflix.client.RetryHandler) RequestSpecificRetryHandler(com.netflix.client.RequestSpecificRetryHandler) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) AbstractLoadBalancer(com.netflix.loadbalancer.AbstractLoadBalancer)

Example 9 with RetryHandler

use of com.netflix.client.RetryHandler in project ribbon by Netflix.

the class LoadBalancerContext method noteError.

/**
 * This is called after an error is thrown from the client
 * to update related stats.
 */
protected void noteError(ServerStats stats, ClientRequest request, Throwable e, long responseTime) {
    if (stats == null) {
        return;
    }
    try {
        recordStats(stats, responseTime);
        RetryHandler errorHandler = getRetryHandler();
        if (errorHandler != null && e != null) {
            if (errorHandler.isCircuitTrippingException(e)) {
                stats.incrementSuccessiveConnectionFailureCount();
                stats.addToFailureCount();
            } else {
                stats.clearSuccessiveConnectionFailureCount();
            }
        }
    } catch (Exception ex) {
        logger.error("Error noting stats for client {}", clientName, ex);
    }
}
Also used : DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) RetryHandler(com.netflix.client.RetryHandler) URISyntaxException(java.net.URISyntaxException) ClientException(com.netflix.client.ClientException)

Example 10 with RetryHandler

use of com.netflix.client.RetryHandler in project ribbon by Netflix.

the class LoadBalancerCommandTest method testRetrySameServer.

@Test
public void testRetrySameServer() {
    BaseLoadBalancer loadBalancer = LoadBalancerBuilder.newBuilder().buildFixedServerListLoadBalancer(list);
    RetryHandler handler = new RetryHandler() {

        @Override
        public boolean isRetriableException(Throwable e, boolean sameServer) {
            return (e instanceof IllegalArgumentException);
        }

        @Override
        public boolean isCircuitTrippingException(Throwable e) {
            return false;
        }

        @Override
        public int getMaxRetriesOnSameServer() {
            return 3;
        }

        @Override
        public int getMaxRetriesOnNextServer() {
            return 0;
        }
    };
    LoadBalancerCommand<String> command = LoadBalancerCommand.<String>builder().withLoadBalancer(loadBalancer).withRetryHandler(handler).withServer(server1).build();
    ServerOperation<String> operation = new ServerOperation<String>() {

        AtomicInteger count = new AtomicInteger();

        @Override
        public Observable<String> call(final Server server) {
            return Observable.create(new OnSubscribe<String>() {

                @Override
                public void call(Subscriber<? super String> t1) {
                    if (count.incrementAndGet() < 3) {
                        t1.onError(new IllegalArgumentException());
                    } else {
                        t1.onNext(server.getHost());
                        t1.onCompleted();
                    }
                }
            });
        }
    };
    String result = command.submit(operation).toBlocking().single();
    assertEquals(3, loadBalancer.getLoadBalancerStats().getSingleServerStat(server1).getTotalRequestsCount());
    assertEquals("1", result);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryHandler(com.netflix.client.RetryHandler) ServerOperation(com.netflix.loadbalancer.reactive.ServerOperation) Test(org.junit.Test)

Aggregations

RetryHandler (com.netflix.client.RetryHandler)15 DefaultLoadBalancerRetryHandler (com.netflix.client.DefaultLoadBalancerRetryHandler)8 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)5 Server (com.netflix.loadbalancer.Server)5 Test (org.junit.Test)5 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)4 IClientConfig (com.netflix.client.config.IClientConfig)4 LoadBalancedRetryFactory (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory)4 RibbonLoadBalancedRetryFactory (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryFactory)4 RibbonLoadBalancerContext (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerContext)4 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)4 SpringClientFactory (org.springframework.cloud.netflix.ribbon.SpringClientFactory)4 MockResponse (com.google.mockwebserver.MockResponse)3 MockWebServer (com.google.mockwebserver.MockWebServer)3 ClientException (com.netflix.client.ClientException)3 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)3 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)3 DummyPing (com.netflix.loadbalancer.DummyPing)3 ServerStats (com.netflix.loadbalancer.ServerStats)3 Person (com.netflix.ribbon.test.resources.EmbeddedResources.Person)3