use of io.servicetalk.http.api.StreamingHttpService in project servicetalk by apple.
the class GrpcRouter method bind.
Single<GrpcServerContext> bind(final ServerBinder binder, final GrpcExecutionContext executionContext) {
final CompositeCloseable closeable = AsyncCloseables.newCompositeCloseable();
final Map<String, StreamingHttpService> allRoutes = new HashMap<>();
populateRoutes(executionContext, allRoutes, routes, closeable, executionStrategies);
populateRoutes(executionContext, allRoutes, streamingRoutes, closeable, executionStrategies);
populateRoutes(executionContext, allRoutes, blockingRoutes, closeable, executionStrategies);
populateRoutes(executionContext, allRoutes, blockingStreamingRoutes, closeable, executionStrategies);
// TODO: Optimize to bind a specific programming model service based on routes
return binder.bindStreaming(new StreamingHttpService() {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
final StreamingHttpService service;
if (!POST.equals(request.method()) || (service = allRoutes.get(request.path())) == null) {
return NOT_FOUND_SERVICE.handle(ctx, request, responseFactory);
} else {
return service.handle(ctx, request, responseFactory);
}
}
@Override
public Completable closeAsync() {
return closeable.closeAsync();
}
@Override
public Completable closeAsyncGracefully() {
return closeable.closeAsyncGracefully();
}
}).map(httpServerContext -> new DefaultGrpcServerContext(httpServerContext, executionContext));
}
use of io.servicetalk.http.api.StreamingHttpService in project servicetalk by apple.
the class HttpServerMultipleRequestsTest method consumeOfRequestBodyDoesNotCloseConnection.
@Disabled("https://github.com/apple/servicetalk/issues/981")
@Test
void consumeOfRequestBodyDoesNotCloseConnection() throws Exception {
StreamingHttpService service = (ctx, request, responseFactory) -> {
request.messageBody().ignoreElements().subscribe();
CharSequence requestId = request.headers().get(REQUEST_ID_HEADER);
if (requestId != null) {
StreamingHttpResponse response = responseFactory.ok();
response.headers().set(REQUEST_ID_HEADER, requestId);
return succeeded(response);
} else {
return succeeded(responseFactory.newResponse(BAD_REQUEST));
}
};
final int concurrency = 10;
final int numRequests = 10;
CompositeCloseable compositeCloseable = AsyncCloseables.newCompositeCloseable();
ServerContext ctx = compositeCloseable.append(HttpServers.forAddress(localAddress(0)).ioExecutor(serverContext.ioExecutor()).executor(serverContext.executor()).executionStrategy(defaultStrategy()).listenStreamingAndAwait(service));
ExecutorService executorService = Executors.newCachedThreadPool();
try {
AtomicReference<Throwable> causeRef = new AtomicReference<>();
CyclicBarrier barrier = new CyclicBarrier(concurrency);
CountDownLatch latch = new CountDownLatch(concurrency);
for (int i = 0; i < concurrency; ++i) {
final int finalI = i;
executorService.execute(() -> {
try {
StreamingHttpClient client = compositeCloseable.append(HttpClients.forResolvedAddress(serverHostAndPort(ctx)).protocols(h1().maxPipelinedRequests(numRequests).build()).ioExecutor(clientContext.ioExecutor()).executor(clientContext.executor()).executionStrategy(defaultStrategy()).buildStreaming());
ReservedStreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get();
compositeCloseable.append(connection);
barrier.await();
for (int x = 0; x < numRequests; ++x) {
makeClientRequestWithId(connection, "thread=" + finalI + " request=" + x);
}
} catch (Throwable cause) {
causeRef.compareAndSet(null, cause);
} finally {
latch.countDown();
}
});
}
latch.await();
assertNull(causeRef.get());
} finally {
executorService.shutdown();
compositeCloseable.close();
}
}
use of io.servicetalk.http.api.StreamingHttpService in project servicetalk by apple.
the class HttpPredicateRouterBuilderTest method testDefaultFallback.
@Test
void testDefaultFallback() throws Exception {
final StreamingHttpService service = new HttpPredicateRouterBuilder().buildStreaming();
final Single<StreamingHttpResponse> responseSingle = service.handle(ctx, request, reqRespFactory);
final StreamingHttpResponse response = responseSingle.toFuture().get();
assert response != null;
assertEquals(404, response.status().code());
}
use of io.servicetalk.http.api.StreamingHttpService in project servicetalk by apple.
the class HttpPredicateRouterBuilderTest method testWhenMethod.
@Test
void testWhenMethod() {
final StreamingHttpService service = new HttpPredicateRouterBuilder().whenMethod(POST).thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
when(request.method()).thenReturn(POST);
assertSame(responseA, service.handle(ctx, request, reqRespFactory));
when(request.method()).thenReturn(GET);
assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
use of io.servicetalk.http.api.StreamingHttpService in project servicetalk by apple.
the class HttpPredicateRouterBuilderTest method testCloseAsyncClosesAllServicesWhenFirstOneIsError.
@Test
void testCloseAsyncClosesAllServicesWhenFirstOneIsError() throws Exception {
when(serviceA.closeAsync()).thenReturn(failCompletable);
when(serviceB.closeAsync()).thenReturn(completableB);
when(serviceC.closeAsync()).thenReturn(completableC);
final StreamingHttpService service = new HttpPredicateRouterBuilder().whenMethod(GET).thenRouteTo(serviceA).whenMethod(POST).thenRouteTo(serviceB).when((ctx, req) -> true).thenRouteTo(serviceC).buildStreaming();
try {
final Completable completable = service.closeAsync();
completable.toFuture().get();
Assertions.fail("Expected an exception from `await`");
} catch (final ExecutionException e) {
assertSame(DELIBERATE_EXCEPTION, e.getCause());
}
verify(serviceA).closeAsync();
verify(serviceB).closeAsync();
verify(serviceC).closeAsync();
failCompletable.verifyNotCancelled();
completableB.verifyNotCancelled();
completableC.verifyNotCancelled();
failCompletable.verifyListenCalled();
completableB.verifyListenCalled();
completableC.verifyListenCalled();
}
Aggregations