Search in sources :

Example 1 with HttpClientResponse

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

the class HttpUtility method getGetResponse.

static Observable<String> getGetResponse(String host, int port, String uri) {
    return new CompositeHttpClientBuilder<ByteBuf, ByteBuf>().appendPipelineConfigurator(new PipelineConfigurator<HttpClientResponse<ByteBuf>, HttpClientRequest<ByteBuf>>() {

        @Override
        public void configureNewPipeline(ChannelPipeline pipeline) {
            pipeline.addLast("introspecting-handler", new ChannelDuplexHandler() {

                private String uri = "<undefined>";

                @Override
                public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                    if (msg instanceof HttpRequest) {
                        HttpRequest request = (HttpRequest) msg;
                        uri = request.getUri();
                        logger.info("Sending request on channel id: " + ctx.channel().toString() + ", request URI: " + uri);
                    }
                    super.write(ctx, msg, promise);
                }

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    if (msg instanceof HttpResponse) {
                        logger.info("Received response on channel id: " + ctx.channel().toString() + ", request URI: " + uri);
                    }
                    super.channelRead(ctx, msg);
                }
            });
            try {
                // Ten megabytes
                int maxContentLength = 10 * 1024 * 1024;
                pipeline.replace(HttpObjectAggregator.class, "http-object-aggregator", new HttpObjectAggregator(maxContentLength));
            } catch (NoSuchElementException ex) {
                logger.error("HttpObjectAggregator did not exist in this pipeline. Error: {}", ex.getMessage(), ex);
            } catch (IllegalArgumentException ex) {
                logger.error("ChannelHandler named http-object-aggregator already existed in this" + " pipeline. Error: {}", ex.getMessage(), ex);
            } catch (Throwable t) {
                logger.error("Unknown error adding HttpObjectAggregator to Master Client " + "Pipeline. Error: {}", t.getMessage(), t);
            }
        }
    }).build().submit(new RxClient.ServerInfo(host, port), HttpClientRequest.createGet(uri), new HttpClient.HttpClientConfig.Builder().setFollowRedirect(true).followRedirect(MAX_REDIRECTS).build()).flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ByteBuf>>() {

        @Override
        public Observable<ByteBuf> call(HttpClientResponse<ByteBuf> response) {
            return response.getContent();
        }
    }).map(new Func1<ByteBuf, String>() {

        @Override
        public String call(ByteBuf o) {
            return o.toString(Charset.defaultCharset());
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.warn("Error: " + throwable.getMessage(), throwable);
        }
    }).timeout(GET_TIMEOUT_SECS, TimeUnit.SECONDS);
}
Also used : CompositeHttpClientBuilder(mantis.io.reactivex.netty.protocol.http.client.CompositeHttpClientBuilder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) RxClient(mantis.io.reactivex.netty.client.RxClient) HttpClientResponse(mantis.io.reactivex.netty.protocol.http.client.HttpClientResponse) Func1(rx.functions.Func1) HttpRequest(io.netty.handler.codec.http.HttpRequest) Action1(rx.functions.Action1) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpClientRequest(mantis.io.reactivex.netty.protocol.http.client.HttpClientRequest) HttpClient(mantis.io.reactivex.netty.protocol.http.client.HttpClient) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with HttpClientResponse

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

the class HttpSources method pollingSource.

/**
 * Create a {@code HttpSource} that infinitely polls the give server with the given URL
 *
 * @param host The host name of the server to be queried
 * @param port The port used for query
 * @param uri  The URI used for query. The URI should be relative to http://host:port/
 *
 * @return A builder that will return an implementation that polls the give server infinitely
 */
public static Builder<ByteBuf, String> pollingSource(final String host, final int port, String uri) {
    HttpClientFactory<ByteBuf, ByteBuf> clientFactory = HttpClientFactories.defaultFactory();
    HttpSourceImpl.Builder<ByteBuf, ByteBuf, String> builderImpl = HttpSourceImpl.builder(clientFactory, HttpRequestFactories.createGetFactory(uri), new Func2<ServerContext<HttpClientResponse<ByteBuf>>, ByteBuf, String>() {

        @Override
        public String call(ServerContext<HttpClientResponse<ByteBuf>> context, ByteBuf content) {
            return content.toString(Charset.defaultCharset());
        }
    }).withServerProvider(new HttpServerProvider() {

        @Override
        public Observable<ServerInfo> getServersToAdd() {
            return Observable.just(new ServerInfo(host, port));
        }

        @Override
        public Observable<ServerInfo> getServersToRemove() {
            return Observable.empty();
        }
    }).resumeWith(new ClientResumePolicy<ByteBuf, ByteBuf>() {

        @Override
        public Observable<HttpClientResponse<ByteBuf>> onError(ServerClientContext<ByteBuf, ByteBuf> clientContext, int attempts, Throwable error) {
            return clientContext.newResponse();
        }

        @Override
        public Observable<HttpClientResponse<ByteBuf>> onCompleted(ServerClientContext<ByteBuf, ByteBuf> clientContext, int attempts) {
            return clientContext.newResponse();
        }
    });
    return HttpSource.builder(builderImpl);
}
Also used : ServerInfo(mantis.io.reactivex.netty.client.RxClient.ServerInfo) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) ServerContext(io.mantisrx.runtime.source.http.impl.ServerContext) HttpClientResponse(mantis.io.reactivex.netty.protocol.http.client.HttpClientResponse) HttpSourceImpl(io.mantisrx.runtime.source.http.impl.HttpSourceImpl)

Example 3 with HttpClientResponse

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

the class ClientResumePoliciesTest method testDelayedOnError.

@Test
public void testDelayedOnError() throws Exception {
    final AtomicLong start = new AtomicLong();
    final AtomicLong end = new AtomicLong();
    final long delay = 100;
    final int repeat = 20;
    final CountDownLatch done = new CountDownLatch(repeat);
    ClientResumePolicy<String, String> policy = ClientResumePolicies.delayed(new Func0<Long>() {

        @Override
        public Long call() {
            return delay;
        }
    }, TimeUnit.MILLISECONDS);
    ServerClientContext<String, String> context = new ServerClientContext<>(new ServerInfo("localhost", 1000), client, factory, observer);
    start.set(System.currentTimeMillis());
    end.set(0);
    for (int i = 0; i < repeat; ++i) {
        Observable<HttpClientResponse<String>> resumedOnCompleted = policy.onError(context, i, new Throwable("error"));
        resumedOnCompleted.subscribe(new Subscriber<HttpClientResponse<String>>() {

            @Override
            public void onCompleted() {
                end.getAndAdd(System.currentTimeMillis() - start.get());
                done.countDown();
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(HttpClientResponse<String> stringHttpClientResponse) {
            }
        });
    }
    long wait = 5;
    if (!done.await(5, TimeUnit.SECONDS)) {
        fail("It should take far less than " + wait + " seconds to run the test. ");
    }
    long elapsed = end.get();
    long maxDelay = delay + delay / 2;
    assertTrue(String.format("The delay should be more than %d millionseconds, but no more than %d millionseconds. The actual: %d", repeat * delay, repeat * maxDelay, elapsed), elapsed >= repeat * delay && elapsed <= repeat * maxDelay);
}
Also used : ServerClientContext(io.mantisrx.runtime.source.http.impl.ServerClientContext) ServerInfo(mantis.io.reactivex.netty.client.RxClient.ServerInfo) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) HttpClientResponse(mantis.io.reactivex.netty.protocol.http.client.HttpClientResponse) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 4 with HttpClientResponse

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

the class ClientResumePoliciesTest method testMaxCombinator.

@Test
public void testMaxCombinator() throws Exception {
    final AtomicLong start = new AtomicLong();
    final AtomicLong end = new AtomicLong();
    final long delay = 100;
    final int repeat = 20;
    final CountDownLatch done = new CountDownLatch(repeat);
    ClientResumePolicy<String, String> delayedPolicy = ClientResumePolicies.delayed(new Func0<Long>() {

        @Override
        public Long call() {
            return delay;
        }
    }, TimeUnit.MILLISECONDS);
    ClientResumePolicy<String, String> policy = ClientResumePolicies.maxRepeat(delayedPolicy, repeat);
    ServerClientContext<String, String> context = new ServerClientContext<>(new ServerInfo("localhost", 1000), client, factory, observer);
    start.set(System.currentTimeMillis());
    end.set(0);
    for (int i = 0; i < repeat; ++i) {
        Observable<HttpClientResponse<String>> resumedOnCompleted = policy.onCompleted(context, i);
        Observable<HttpClientResponse<String>> resumedOnError = policy.onError(context, i, new Throwable("error"));
        if (i <= repeat) {
            assertNotNull(resumedOnCompleted);
            assertNotNull(resumedOnError);
        } else {
            assertNull("The resumed on completion should be null as max repeat is passed", resumedOnCompleted);
            assertNull("The resumed on error should be null as max repeat is passed", resumedOnError);
        }
        resumedOnCompleted.subscribe(new Subscriber<HttpClientResponse<String>>() {

            @Override
            public void onCompleted() {
                end.getAndAdd(System.currentTimeMillis() - start.get());
                done.countDown();
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(HttpClientResponse<String> stringHttpClientResponse) {
            }
        });
    }
    long wait = 5;
    if (!done.await(5, TimeUnit.SECONDS)) {
        fail("It should take far less than " + wait + " seconds to run the test. ");
    }
    long elapsed = end.get();
    long maxDelay = delay + delay / 2;
    assertTrue(String.format("The delay should be more than %d milliseconds, but no more than %d milliseconds. The actual: %d", repeat * delay, repeat * maxDelay, elapsed), elapsed >= repeat * delay && elapsed <= repeat * maxDelay);
}
Also used : ServerClientContext(io.mantisrx.runtime.source.http.impl.ServerClientContext) ServerInfo(mantis.io.reactivex.netty.client.RxClient.ServerInfo) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) HttpClientResponse(mantis.io.reactivex.netty.protocol.http.client.HttpClientResponse) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 5 with HttpClientResponse

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

the class ClientResumePoliciesTest method testMaxRepeatOnCompletionAndError.

@Test
public void testMaxRepeatOnCompletionAndError() throws Exception {
    int max = 10;
    ClientResumePolicy<String, String> policy = ClientResumePolicies.maxRepeat(max);
    ServerClientContext<String, String> context = new ServerClientContext<>(new ServerInfo("localhost", 1000), client, factory, observer);
    for (int i = 0; i < 20; ++i) {
        Observable<HttpClientResponse<String>> resumedOnCompleted = policy.onCompleted(context, i);
        Observable<HttpClientResponse<String>> resumedOnError = policy.onError(context, i, new Throwable("error"));
        if (i <= max) {
            assertNotNull(resumedOnCompleted);
            assertEquals(RESPONSE_CONTENT, resumedOnCompleted.toBlocking().first().getContent().toBlocking().first());
            assertNotNull(resumedOnError);
            assertEquals(RESPONSE_CONTENT, resumedOnError.toBlocking().first().getContent().toBlocking().first());
        } else {
            assertNull("The resumed on completion should be null as max repeat is passed", resumedOnCompleted);
            assertNull("The resumed on error should be null as max repeat is passed", resumedOnError);
        }
    }
}
Also used : ServerClientContext(io.mantisrx.runtime.source.http.impl.ServerClientContext) ServerInfo(mantis.io.reactivex.netty.client.RxClient.ServerInfo) HttpClientResponse(mantis.io.reactivex.netty.protocol.http.client.HttpClientResponse) Test(org.junit.Test)

Aggregations

HttpClientResponse (mantis.io.reactivex.netty.protocol.http.client.HttpClientResponse)6 ServerInfo (mantis.io.reactivex.netty.client.RxClient.ServerInfo)5 ServerClientContext (io.mantisrx.runtime.source.http.impl.ServerClientContext)4 Test (org.junit.Test)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 ByteBuf (io.netty.buffer.ByteBuf)2 HttpSourceImpl (io.mantisrx.runtime.source.http.impl.HttpSourceImpl)1 ServerContext (io.mantisrx.runtime.source.http.impl.ServerContext)1 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 ChannelPromise (io.netty.channel.ChannelPromise)1 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 NoSuchElementException (java.util.NoSuchElementException)1 RxClient (mantis.io.reactivex.netty.client.RxClient)1 CompositeHttpClientBuilder (mantis.io.reactivex.netty.protocol.http.client.CompositeHttpClientBuilder)1 HttpClient (mantis.io.reactivex.netty.protocol.http.client.HttpClient)1