Search in sources :

Example 16 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class LoadBalancingHttpClient method requestToOperation.

/**
     * Convert an HttpClientRequest to a ServerOperation 
     * 
     * @param server
     * @param request
     * @param rxClientConfig
     * @return
     */
protected ServerOperation<HttpClientResponse<O>> requestToOperation(final HttpClientRequest<I> request, final ClientConfig rxClientConfig) {
    Preconditions.checkNotNull(request);
    return new ServerOperation<HttpClientResponse<O>>() {

        final AtomicInteger count = new AtomicInteger(0);

        @Override
        public Observable<HttpClientResponse<O>> call(Server server) {
            HttpClient<I, O> rxClient = getOrCreateRxClient(server);
            setHostHeader(request, server.getHost());
            Observable<HttpClientResponse<O>> o;
            if (rxClientConfig != null) {
                o = rxClient.submit(request, rxClientConfig);
            } else {
                o = rxClient.submit(request);
            }
            return o.concatMap(new Func1<HttpClientResponse<O>, Observable<HttpClientResponse<O>>>() {

                @Override
                public Observable<HttpClientResponse<O>> call(HttpClientResponse<O> t1) {
                    if (t1.getStatus().code() / 100 == 4 || t1.getStatus().code() / 100 == 5)
                        return responseToErrorPolicy.call(t1, backoffStrategy.call(count.getAndIncrement()));
                    else
                        return Observable.just(t1);
                }
            });
        }
    };
}
Also used : Server(com.netflix.loadbalancer.Server) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) URI(java.net.URI) ServerOperation(com.netflix.loadbalancer.reactive.ServerOperation) Observable(rx.Observable)

Example 17 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class NettyClientTest method testLoadBalancerThrottle.

@Test
public void testLoadBalancerThrottle() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/throttle");
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(IClientConfigKey.Keys.MaxAutoRetriesNextServer, 1).set(IClientConfigKey.Keys.OkToRetryOnAllOperations, true);
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config);
    Server server = new Server(host, port);
    lb.setServersList(Lists.newArrayList(server, server, server));
    Observable<HttpClientResponse<ByteBuf>> response = lbObservables.submit(request);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    response.subscribe(new Action1<HttpClientResponse<ByteBuf>>() {

        @Override
        public void call(HttpClientResponse<ByteBuf> t1) {
            System.err.println("Get response: " + t1.getStatus().code());
            latch.countDown();
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable t1) {
            error.set(t1);
            latch.countDown();
        }
    }, new Action0() {

        @Override
        public void call() {
            Thread.dumpStack();
            latch.countDown();
        }
    });
    latch.await();
    assertTrue(error.get() instanceof ClientException);
    ClientException ce = (ClientException) error.get();
    assertTrue(ce.toString(), ce.getErrorType() == ClientException.ErrorType.NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED);
    assertEquals(2, lbObservables.getServerStats(server).getSuccessiveConnectionFailureCount());
}
Also used : Action0(rx.functions.Action0) HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch) DummyPing(com.netflix.loadbalancer.DummyPing) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) IClientConfig(com.netflix.client.config.IClientConfig) ClientException(com.netflix.client.ClientException) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Test(org.junit.Test)

Example 18 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class NettyClientTest method testPoolReuse.

@Test
public void testPoolReuse() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person");
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(IClientConfig.Builder.newBuilder().withDefaultValues().withMaxAutoRetries(1).withMaxAutoRetriesNextServer(1).build());
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
    Person person = getPersonObservable(response).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    response = observableClient.submit(request);
    person = getPersonObservable(response).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    final HttpClientListener listener = observableClient.getListener();
    assertEquals(2, listener.getPoolAcquires());
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {

        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 2;
        }
    });
    assertEquals(1, listener.getConnectionCount());
    assertEquals(1, listener.getPoolReuse());
}
Also used : HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) HttpClientListener(io.reactivex.netty.servo.http.HttpClientListener) Test(org.junit.Test)

Example 19 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class ListenerTest method testAbortedExecutionOnServer.

@Test
public void testAbortedExecutionOnServer() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100").withProperty(CommonClientConfigKey.MaxAutoRetries, 1).withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) {

        @Override
        public void onStartWithServer(ExecutionContext context, ExecutionInfo info) {
            throw new AbortExecutionException("exit now");
        }
    };
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> ref = new AtomicReference<Throwable>();
    client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() {

        @Override
        public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) {
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            ref.set(throwable);
            latch.countDown();
        }
    });
    try {
        latch.await(500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(ref.get() instanceof AbortExecutionException);
}
Also used : Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) ExecutionInfo(com.netflix.loadbalancer.reactive.ExecutionInfo) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) AbortExecutionException(com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException) ByteBuf(io.netty.buffer.ByteBuf) ExecutionListener(com.netflix.loadbalancer.reactive.ExecutionListener) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionContext(com.netflix.loadbalancer.reactive.ExecutionContext) DummyPing(com.netflix.loadbalancer.DummyPing) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Test(org.junit.Test)

Example 20 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class NettyClientTest method testPostWithByteBuf.

@Test
public void testPostWithByteBuf() throws Exception {
    Person myPerson = new Person("netty", 5);
    ObjectMapper mapper = new ObjectMapper();
    byte[] raw = mapper.writeValueAsBytes(myPerson);
    ByteBuf buffer = Unpooled.copiedBuffer(raw);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost(SERVICE_URI + "testAsync/person").withHeader("Content-type", "application/json").withHeader("Content-length", String.valueOf(raw.length)).withContent(buffer);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(CommonClientConfigKey.ReadTimeout, 10000));
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
    Person person = getPersonObservable(response).toBlocking().single();
    assertEquals(myPerson, person);
}
Also used : HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Aggregations

HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)21 ByteBuf (io.netty.buffer.ByteBuf)18 Test (org.junit.Test)16 Server (com.netflix.loadbalancer.Server)9 MockWebServer (com.google.mockwebserver.MockWebServer)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 IClientConfig (com.netflix.client.config.IClientConfig)5 Person (com.netflix.ribbon.test.resources.EmbeddedResources.Person)5 HttpServer (com.sun.net.httpserver.HttpServer)5 Observable (rx.Observable)5 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)4 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)3 DummyPing (com.netflix.loadbalancer.DummyPing)3 ExecutionContext (com.netflix.loadbalancer.reactive.ExecutionContext)3 HttpResourceGroup (com.netflix.ribbon.http.HttpResourceGroup)3 HttpClientListener (io.reactivex.netty.servo.http.HttpClientListener)3 ClientException (com.netflix.client.ClientException)2 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)2 AbortExecutionException (com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException)2