Search in sources :

Example 1 with SingleNioLoopProvider

use of io.reactivex.netty.channel.SingleNioLoopProvider in project WSPerfLab by Netflix-Skunkworks.

the class StartServer method main.

public static void main(String[] args) {
    Clock.disableSystemTimeCalls();
    int eventLoops = Runtime.getRuntime().availableProcessors();
    int port = 8888;
    String backendHost = "127.0.0.1";
    int backendPort = 8989;
    if (args.length == 0) {
    // use defaults
    } else if (args.length == 4) {
        eventLoops = Integer.parseInt(args[0]);
        port = Integer.parseInt(args[1]);
        backendHost = args[2];
        backendPort = Integer.parseInt(args[3]);
    } else {
        System.err.println("Execute with either no argument (for defaults) or 4 arguments: EVENTLOOPS, PORT, BACKEND_HOST, BACKEND_PORT");
        System.exit(-1);
    }
    System.out.println(String.format("Using eventloops: %d port: %d backend host: %s backend port: %d", eventLoops, port, backendHost, backendPort));
    route = new TestRouteBasic(backendHost, backendPort);
    routeHello = new TestRouteHello();
    SingleNioLoopProvider provider = new SingleNioLoopProvider(eventLoops);
    RxNetty.useEventLoopProvider(provider);
    System.out.println("Starting service on port " + port + " with backend at " + backendHost + ':' + backendPort + " ...");
    startMonitoring();
    RxNetty.<ByteBuf, ByteBuf>newHttpServerBuilder(port, (request, response) -> {
        try {
            if (request.getUri().startsWith("/hello")) {
                return routeHello.handle(request, response);
            }
            long startTime = System.currentTimeMillis();
            counter.increment(CounterEvent.REQUESTS);
            return route.handle(request, response).doOnCompleted(() -> {
                counter.increment(CounterEvent.SUCCESS);
                latency.addValue((int) (System.currentTimeMillis() - startTime));
            }).onErrorResumeNext(t -> {
                if (t instanceof PoolExhaustedException) {
                    counter.increment(CounterEvent.CLIENT_POOL_EXHAUSTION);
                } else if (t instanceof SocketException) {
                    counter.increment(CounterEvent.SOCKET_EXCEPTION);
                } else if (t instanceof IOException) {
                    counter.increment(CounterEvent.IO_EXCEPTION);
                } else if (t instanceof CancellationException) {
                    counter.increment(CounterEvent.CANCELLATION_EXCEPTION);
                } else if (t instanceof JsonParseException) {
                    counter.increment(CounterEvent.PARSING_EXCEPTION);
                } else {
                    counter.increment(CounterEvent.NETTY_ERROR);
                }
                response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
                response.writeString("");
                return response.close(false);
            });
        } catch (Throwable e) {
            System.err.println("Server => Error [" + request.getPath() + "] => " + e);
            counter.increment(CounterEvent.NETTY_ERROR);
            response.setStatus(HttpResponseStatus.BAD_REQUEST);
            return response.writeStringAndFlush("Error 400: Bad Request\n" + e.getMessage() + '\n');
        }
    }).eventLoops(new NioEventLoopGroup(1), provider.globalServerEventLoop()).build().withErrorHandler(throwable -> Observable.empty()).withErrorResponseGenerator((response, error) -> System.err.println("Error: " + error.getMessage())).startAndWait();
}
Also used : NumerusRollingPercentile(com.netflix.numerus.NumerusRollingPercentile) CancellationException(java.util.concurrent.CancellationException) RxNetty(io.reactivex.netty.RxNetty) Clock(io.reactivex.netty.metrics.Clock) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) IOException(java.io.IOException) Factory.asProperty(com.netflix.numerus.NumerusProperty.Factory.asProperty) SingleNioLoopProvider(io.reactivex.netty.channel.SingleNioLoopProvider) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) NumerusRollingNumber(com.netflix.numerus.NumerusRollingNumber) PoolExhaustedException(io.reactivex.netty.client.PoolExhaustedException) SocketException(java.net.SocketException) ByteBuf(io.netty.buffer.ByteBuf) JsonParseException(perf.test.utils.JsonParseException) SocketException(java.net.SocketException) PoolExhaustedException(io.reactivex.netty.client.PoolExhaustedException) SingleNioLoopProvider(io.reactivex.netty.channel.SingleNioLoopProvider) IOException(java.io.IOException) JsonParseException(perf.test.utils.JsonParseException) CancellationException(java.util.concurrent.CancellationException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

Factory.asProperty (com.netflix.numerus.NumerusProperty.Factory.asProperty)1 NumerusRollingNumber (com.netflix.numerus.NumerusRollingNumber)1 NumerusRollingPercentile (com.netflix.numerus.NumerusRollingPercentile)1 ByteBuf (io.netty.buffer.ByteBuf)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 RxNetty (io.reactivex.netty.RxNetty)1 SingleNioLoopProvider (io.reactivex.netty.channel.SingleNioLoopProvider)1 PoolExhaustedException (io.reactivex.netty.client.PoolExhaustedException)1 Clock (io.reactivex.netty.metrics.Clock)1 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 CancellationException (java.util.concurrent.CancellationException)1 TimeUnit (java.util.concurrent.TimeUnit)1 JsonParseException (perf.test.utils.JsonParseException)1 Observable (rx.Observable)1