Search in sources :

Example 1 with IoExecutor

use of io.servicetalk.transport.api.IoExecutor in project servicetalk by apple.

the class BackendsStarter method main.

public static void main(String[] args) throws Exception {
    // Create an AutoCloseable representing all resources used in this example.
    try (CompositeCloseable resources = newCompositeCloseable()) {
        // Shared IoExecutor for the application.
        IoExecutor ioExecutor = resources.prepend(createIoExecutor());
        // This is a single Completable used to await closing of all backends started by this class. It is used to
        // provide a way to not let main() exit.
        Completable allServicesOnClose = completed();
        BackendStarter starter = new BackendStarter(ioExecutor, resources);
        final ServerContext recommendationService = starter.start(RECOMMENDATIONS_BACKEND_ADDRESS.port(), RECOMMENDATION_SERVICE_NAME, newRecommendationsService());
        allServicesOnClose = allServicesOnClose.merge(recommendationService.onClose());
        final ServerContext metadataService = starter.start(METADATA_BACKEND_ADDRESS.port(), METADATA_SERVICE_NAME, newMetadataService());
        allServicesOnClose = allServicesOnClose.merge(metadataService.onClose());
        final ServerContext userService = starter.start(USER_BACKEND_ADDRESS.port(), USER_SERVICE_NAME, newUserService());
        allServicesOnClose = allServicesOnClose.merge(userService.onClose());
        final ServerContext ratingService = starter.start(RATINGS_BACKEND_ADDRESS.port(), RATING_SERVICE_NAME, newRatingService());
        allServicesOnClose = allServicesOnClose.merge(ratingService.onClose());
        // Await termination of all backends started by this class.
        allServicesOnClose.toFuture().get();
    }
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) IoExecutor(io.servicetalk.transport.api.IoExecutor) NettyIoExecutors.createIoExecutor(io.servicetalk.transport.netty.NettyIoExecutors.createIoExecutor) ServerContext(io.servicetalk.transport.api.ServerContext) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)

Example 2 with IoExecutor

use of io.servicetalk.transport.api.IoExecutor in project servicetalk by apple.

the class DefaultMultiAddressUrlHttpClientBuilderTest method internalClientsUseDifferentExecutionContextWhenConfigured.

@Test
void internalClientsUseDifferentExecutionContextWhenConfigured() throws Exception {
    // Assert prerequisites first.
    // Use different strategies, as ExecutionContextExtension shares the same strategy.
    HttpExecutionStrategy internalExecutionStrategy = HttpExecutionStrategies.customStrategyBuilder().offloadNone().build();
    HttpExecutionStrategy externalExecutionStrategy = HttpExecutionStrategies.customStrategyBuilder().offloadAll().build();
    assertThat(internalExecutionStrategy, not(equalTo(externalExecutionStrategy)));
    BufferAllocator internalBufferAllocator = BufferAllocators.PREFER_DIRECT_ALLOCATOR;
    BufferAllocator externalBufferAllocator = BufferAllocators.PREFER_HEAP_ALLOCATOR;
    assertThat(internalBufferAllocator, not(equalTo(externalBufferAllocator)));
    assertThat(CTX.executor(), not(equalTo(INTERNAL_CLIENT_CTX.executor())));
    assertThat(CTX.ioExecutor(), not(equalTo(INTERNAL_CLIENT_CTX.ioExecutor())));
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).executionStrategy(offloadNever()).listenStreamingAndAwait((ctx, request, responseFactory) -> succeeded(responseFactory.ok()))) {
        AtomicReference<BufferAllocator> actualInternalBufferAllocator = new AtomicReference<>();
        AtomicReference<IoExecutor> actualInternalIoExecutor = new AtomicReference<>();
        AtomicReference<Executor> actualInternalExecutor = new AtomicReference<>();
        AtomicReference<ExecutionStrategy> actualInternalExecutionStrategy = new AtomicReference<>();
        final StreamingHttpClient streamingHttpClient = HttpClients.forMultiAddressUrl().initializer((scheme, address, builder) -> builder.executionStrategy(internalExecutionStrategy).executor(INTERNAL_CLIENT_CTX.executor()).ioExecutor(INTERNAL_CLIENT_CTX.ioExecutor()).bufferAllocator(internalBufferAllocator).executionStrategy(internalExecutionStrategy).appendClientFilter(client -> {
            HttpExecutionContext internalContext = client.executionContext();
            actualInternalBufferAllocator.set(internalContext.bufferAllocator());
            actualInternalExecutor.set(internalContext.executor());
            actualInternalIoExecutor.set(internalContext.ioExecutor());
            actualInternalExecutionStrategy.set(internalContext.executionStrategy());
            return new StreamingHttpClientFilter(client) {
            };
        })).executor(CTX.executor()).ioExecutor(CTX.ioExecutor()).executionStrategy(externalExecutionStrategy).bufferAllocator(externalBufferAllocator).buildStreaming();
        assertNotNull(streamingHttpClient);
        // Check external client
        final HttpExecutionContext executionContext = streamingHttpClient.executionContext();
        assertThat(executionContext.executor(), equalTo(CTX.executor()));
        assertThat(executionContext.executionStrategy(), equalTo(externalExecutionStrategy));
        assertThat(executionContext.ioExecutor(), equalTo(CTX.ioExecutor()));
        assertThat(executionContext.bufferAllocator(), equalTo(CTX.bufferAllocator()));
        // Make a request to trigger the filter execution that extracts the execution context.
        final HostAndPort address = HostAndPort.of((InetSocketAddress) serverContext.listenAddress());
        streamingHttpClient.reserveConnection(streamingHttpClient.get("http://" + address)).toFuture().get();
        // Check internal client
        assertThat(actualInternalBufferAllocator.get(), equalTo(internalBufferAllocator));
        assertThat(actualInternalExecutor.get(), equalTo(INTERNAL_CLIENT_CTX.executor()));
        assertThat(actualInternalIoExecutor.get(), equalTo(INTERNAL_CLIENT_CTX.ioExecutor()));
        assertThat(actualInternalExecutionStrategy.get(), equalTo(internalExecutionStrategy));
        streamingHttpClient.closeAsync().toFuture().get();
    }
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Matchers.not(org.hamcrest.Matchers.not) ExecutionContextExtension.cached(io.servicetalk.transport.netty.internal.ExecutionContextExtension.cached) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) ServiceDiscovererEvent(io.servicetalk.client.api.ServiceDiscovererEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpRequester(io.servicetalk.http.api.HttpRequester) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) ExecutionStrategy(io.servicetalk.transport.api.ExecutionStrategy) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpExecutionStrategies(io.servicetalk.http.api.HttpExecutionStrategies) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) Executor(io.servicetalk.concurrent.api.Executor) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ExecutionContextExtension.immediate(io.servicetalk.transport.netty.internal.ExecutionContextExtension.immediate) ServerContext(io.servicetalk.transport.api.ServerContext) ServiceDiscoverer(io.servicetalk.client.api.ServiceDiscoverer) ExecutionContextExtension(io.servicetalk.transport.netty.internal.ExecutionContextExtension) BlockingHttpRequester(io.servicetalk.http.api.BlockingHttpRequester) BlockingStreamingHttpRequester(io.servicetalk.http.api.BlockingStreamingHttpRequester) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) InetSocketAddress(java.net.InetSocketAddress) Mockito.verify(org.mockito.Mockito.verify) BufferAllocators(io.servicetalk.buffer.netty.BufferAllocators) Test(org.junit.jupiter.api.Test) IoExecutor(io.servicetalk.transport.api.IoExecutor) Mockito.never(org.mockito.Mockito.never) BufferAllocator(io.servicetalk.buffer.api.BufferAllocator) Matchers.equalTo(org.hamcrest.Matchers.equalTo) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpExecutionStrategies.offloadNever(io.servicetalk.http.api.HttpExecutionStrategies.offloadNever) Mockito.mock(org.mockito.Mockito.mock) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) IoExecutor(io.servicetalk.transport.api.IoExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) BufferAllocator(io.servicetalk.buffer.api.BufferAllocator) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) HostAndPort(io.servicetalk.transport.api.HostAndPort) Executor(io.servicetalk.concurrent.api.Executor) IoExecutor(io.servicetalk.transport.api.IoExecutor) ServerContext(io.servicetalk.transport.api.ServerContext) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) ExecutionStrategy(io.servicetalk.transport.api.ExecutionStrategy) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) Test(org.junit.jupiter.api.Test)

Example 3 with IoExecutor

use of io.servicetalk.transport.api.IoExecutor in project servicetalk by apple.

the class GrpcUdsTest method udsRoundTrip.

@Test
void udsRoundTrip() throws Exception {
    Assumptions.assumeTrue(ioExecutor.isUnixDomainSocketSupported());
    String greetingPrefix = "Hello ";
    String name = "foo";
    String expectedResponse = greetingPrefix + name;
    try (ServerContext serverContext = forAddress(newSocketAddress()).initializeHttp(builder -> builder.ioExecutor(ioExecutor)).listenAndAwait((GreeterService) (ctx, request) -> succeeded(HelloReply.newBuilder().setMessage(greetingPrefix + request.getName()).build()));
        BlockingGreeterClient client = forResolvedAddress(serverContext.listenAddress()).buildBlocking(new ClientFactory())) {
        assertEquals(expectedResponse, client.sayHello(HelloRequest.newBuilder().setName(name).build()).getMessage());
    }
}
Also used : AddressUtils.newSocketAddress(io.servicetalk.transport.netty.internal.AddressUtils.newSocketAddress) ServerContext(io.servicetalk.transport.api.ServerContext) ClientFactory(io.grpc.examples.helloworld.Greeter.ClientFactory) GreeterService(io.grpc.examples.helloworld.Greeter.GreeterService) HelloRequest(io.grpc.examples.helloworld.HelloRequest) GrpcClients.forResolvedAddress(io.servicetalk.grpc.netty.GrpcClients.forResolvedAddress) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) BlockingGreeterClient(io.grpc.examples.helloworld.Greeter.BlockingGreeterClient) GrpcServers.forAddress(io.servicetalk.grpc.netty.GrpcServers.forAddress) IoExecutor(io.servicetalk.transport.api.IoExecutor) Assumptions(org.junit.jupiter.api.Assumptions) BeforeAll(org.junit.jupiter.api.BeforeAll) NettyIoExecutors.createIoExecutor(io.servicetalk.transport.netty.NettyIoExecutors.createIoExecutor) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) HelloReply(io.grpc.examples.helloworld.HelloReply) Nullable(javax.annotation.Nullable) ServerContext(io.servicetalk.transport.api.ServerContext) ClientFactory(io.grpc.examples.helloworld.Greeter.ClientFactory) BlockingGreeterClient(io.grpc.examples.helloworld.Greeter.BlockingGreeterClient) Test(org.junit.jupiter.api.Test)

Example 4 with IoExecutor

use of io.servicetalk.transport.api.IoExecutor in project servicetalk by apple.

the class GatewayServer method main.

/**
 * Starts this server.
 *
 * @param args Program arguments, none supported yet.
 * @throws Exception If the server could not be started.
 */
public static void main(String[] args) throws Exception {
    // Create an AutoCloseable representing all resources used in this example.
    try (CompositeCloseable resources = newCompositeCloseable()) {
        // Shared IoExecutor for the application.
        IoExecutor ioExecutor = resources.prepend(createIoExecutor());
        // Create clients for the different backends we are going to use in the gateway.
        StreamingHttpClient recommendationsClient = newClient(ioExecutor, RECOMMENDATIONS_BACKEND_ADDRESS, resources, "recommendations backend");
        HttpClient metadataClient = newClient(ioExecutor, METADATA_BACKEND_ADDRESS, resources, "metadata backend").asClient();
        HttpClient userClient = newClient(ioExecutor, USER_BACKEND_ADDRESS, resources, "user backend").asClient();
        HttpClient ratingsClient = newClient(ioExecutor, RATINGS_BACKEND_ADDRESS, resources, "ratings backend").asClient();
        // Gateway supports different endpoints for blocking, streaming or aggregated implementations.
        // We create a router to express these endpoints.
        HttpPredicateRouterBuilder routerBuilder = new HttpPredicateRouterBuilder();
        final StreamingHttpService gatewayService = routerBuilder.whenPathStartsWith("/recommendations/stream").thenRouteTo(new StreamingGatewayService(recommendationsClient, metadataClient, ratingsClient, userClient)).whenPathStartsWith("/recommendations/aggregated").thenRouteTo(new GatewayService(recommendationsClient.asClient(), metadataClient, ratingsClient, userClient)).whenPathStartsWith("/recommendations/blocking").thenRouteTo(new BlockingGatewayService(recommendationsClient.asBlockingClient(), metadataClient.asBlockingClient(), ratingsClient.asBlockingClient(), userClient.asBlockingClient())).buildStreaming();
        // Create configurable starter for HTTP server.
        // Starting the server will start listening for incoming client requests.
        ServerContext serverContext = HttpServers.forPort(8080).ioExecutor(ioExecutor).appendServiceFilter(new BadResponseHandlingServiceFilter()).listenStreamingAndAwait(gatewayService);
        LOGGER.info("Listening on {}", serverContext.listenAddress());
        // Blocks and awaits shutdown of the server this ServerContext represents.
        serverContext.awaitShutdown();
    }
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) IoExecutor(io.servicetalk.transport.api.IoExecutor) NettyIoExecutors.createIoExecutor(io.servicetalk.transport.netty.NettyIoExecutors.createIoExecutor) ServerContext(io.servicetalk.transport.api.ServerContext) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) HttpClient(io.servicetalk.http.api.HttpClient) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpPredicateRouterBuilder(io.servicetalk.http.router.predicate.HttpPredicateRouterBuilder)

Example 5 with IoExecutor

use of io.servicetalk.transport.api.IoExecutor in project FrameworkBenchmarks by TechEmpower.

the class Server method main.

public static void main(String[] args) throws Exception {
    /*
         * Disable  AsyncContext
         */
    AsyncContext.disable();
    /*
         *   Factory to implement io pooling
         */
    IoExecutor ioExecutor = NettyIoExecutors.createIoExecutor(Runtime.getRuntime().availableProcessors(), new IoThreadFactory("io-pool"));
    /*
         * Factory to disable headers validation
         */
    DefaultHttpHeadersFactory headersFactory = new DefaultHttpHeadersFactory(false, false);
    HttpSerializationProvider serializer = HttpSerializationProviders.jsonSerializer(new JacksonSerializationProvider());
    // Create a custom server builder with performance enhancements
    HttpServers.forPort(8080).executionStrategy(HttpExecutionStrategies.noOffloadsStrategy()).ioExecutor(ioExecutor).disableDrainingRequestPayloadBody().protocols(HttpProtocolConfigs.h1().headersFactory(headersFactory).build()).appendConnectionAcceptorFilter(delegate -> new ConnectionAcceptor() {

        @Override
        public Completable accept(ConnectionContext context) {
            ((NettyConnectionContext) context).updateFlushStrategy((current, isOrig) -> FlushStrategies.flushOnEnd());
            return delegate.accept(context);
        }
    }).listenAndAwait((ctx, request, responseFactory) -> {
        ((NettyConnectionContext) ctx).updateFlushStrategy(((current, isCurrentOriginal) -> FlushStrategies.flushOnEach()));
        if (request.path().equals("/json")) {
            Map<String, String> obj = new HashMap<String, String>();
            obj.put("message", "Hello, World!");
            return succeeded(responseFactory.ok().payloadBody(obj, serializer.serializerFor(Map.class)).addHeader("Date", getCurrentTime()).addHeader("Server", "ServiceTalk"));
        }
        if (request.path().equals("/plaintext")) {
            return succeeded(responseFactory.ok().payloadBody("Hello, World!", HttpSerializationProviders.textSerializer()).addHeader("Date", getCurrentTime()).addHeader("Server", "ServiceTalk"));
        }
        ;
        return null;
    }).awaitShutdown();
}
Also used : io.servicetalk.transport.netty.internal(io.servicetalk.transport.netty.internal) ZonedDateTime(java.time.ZonedDateTime) Completable(io.servicetalk.concurrent.api.Completable) HttpProtocolConfigs(io.servicetalk.http.netty.HttpProtocolConfigs) HashMap(java.util.HashMap) ConnectionAcceptor(io.servicetalk.transport.api.ConnectionAcceptor) IoExecutor(io.servicetalk.transport.api.IoExecutor) JacksonSerializationProvider(io.servicetalk.data.jackson.JacksonSerializationProvider) AsyncContext(io.servicetalk.concurrent.api.AsyncContext) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Map(java.util.Map) DateTimeFormatter(java.time.format.DateTimeFormatter) ZoneOffset(java.time.ZoneOffset) io.servicetalk.http.api(io.servicetalk.http.api) HttpServers(io.servicetalk.http.netty.HttpServers) ConnectionContext(io.servicetalk.transport.api.ConnectionContext) ConnectionAcceptor(io.servicetalk.transport.api.ConnectionAcceptor) Completable(io.servicetalk.concurrent.api.Completable) IoExecutor(io.servicetalk.transport.api.IoExecutor) JacksonSerializationProvider(io.servicetalk.data.jackson.JacksonSerializationProvider) ConnectionContext(io.servicetalk.transport.api.ConnectionContext) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IoExecutor (io.servicetalk.transport.api.IoExecutor)5 ServerContext (io.servicetalk.transport.api.ServerContext)4 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)3 NettyIoExecutors.createIoExecutor (io.servicetalk.transport.netty.NettyIoExecutors.createIoExecutor)3 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)2 Completable (io.servicetalk.concurrent.api.Completable)2 CompositeCloseable (io.servicetalk.concurrent.api.CompositeCloseable)2 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)2 Test (org.junit.jupiter.api.Test)2 BlockingGreeterClient (io.grpc.examples.helloworld.Greeter.BlockingGreeterClient)1 ClientFactory (io.grpc.examples.helloworld.Greeter.ClientFactory)1 GreeterService (io.grpc.examples.helloworld.Greeter.GreeterService)1 HelloReply (io.grpc.examples.helloworld.HelloReply)1 HelloRequest (io.grpc.examples.helloworld.HelloRequest)1 BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)1 BufferAllocators (io.servicetalk.buffer.netty.BufferAllocators)1 ServiceDiscoverer (io.servicetalk.client.api.ServiceDiscoverer)1 ServiceDiscovererEvent (io.servicetalk.client.api.ServiceDiscovererEvent)1 AsyncContext (io.servicetalk.concurrent.api.AsyncContext)1 Executor (io.servicetalk.concurrent.api.Executor)1