use of io.servicetalk.grpc.api.GrpcServiceContext in project servicetalk by apple.
the class InMemoryServerTransport method getServiceContext.
private static GrpcServiceContext getServiceContext(Channel channel, BufferAllocator allocator) {
// GrpcServiceContext is 1:1 with the Channel for the request. Closing it also closes the connection.
Attribute<GrpcServiceContext> attr = channel.attr(GRPC_SERVICE_CONTEXT_KEY);
GrpcServiceContext serviceContext = attr.get();
if (serviceContext != null) {
return serviceContext;
}
// GrpcExecutionContext exposes IoExecutor which is 1:1 with EventLoop. You could also use a
// FastThreadLocal to store this info as well.
GrpcExecutionContext executionContext = EVENT_LOOP_GRPC_EXECUTION_CONTEXT_MAP.computeIfAbsent(channel.eventLoop(), el -> new UtilGrpcExecutionContext(allocator, NettyIoExecutors.fromNettyEventLoop(el), GlobalExecutionContext.globalExecutionContext().executor()));
serviceContext = new ChannelGrpcServiceContext(channel, executionContext);
attr.set(serviceContext);
return serviceContext;
}
use of io.servicetalk.grpc.api.GrpcServiceContext in project servicetalk by apple.
the class ProtocolCompatibilityTest method serviceTalkServer.
private static TestServerContext serviceTalkServer(final ErrorMode errorMode, final boolean ssl, final GrpcExecutionStrategy strategy, @Nullable final String compression, @Nullable final Duration timeout, Queue<Throwable> reqStreamError) throws Exception {
final Compat.CompatService compatService = new Compat.CompatService() {
@Override
public Publisher<CompatResponse> bidirectionalStreamingCall(final GrpcServiceContext ctx, final Publisher<CompatRequest> pub) {
reqStreamError.add(SERVER_PROCESSED_TOKEN);
maybeThrowFromRpc(errorMode);
return pub.map(req -> response(req.getId())).beforeFinally(errorConsumer());
}
@Override
public Single<CompatResponse> clientStreamingCall(final GrpcServiceContext ctx, final Publisher<CompatRequest> pub) {
reqStreamError.add(SERVER_PROCESSED_TOKEN);
maybeThrowFromRpc(errorMode);
return pub.collect(() -> 0, (sum, req) -> sum + req.getId()).map(this::response).beforeFinally(errorConsumer());
}
@Override
public Single<CompatResponse> scalarCall(final GrpcServiceContext ctx, final CompatRequest req) {
maybeThrowFromRpc(errorMode);
return succeeded(response(req.getId()));
}
@Override
public Publisher<CompatResponse> serverStreamingCall(final GrpcServiceContext ctx, final CompatRequest req) {
maybeThrowFromRpc(errorMode);
return Publisher.fromIterable(() -> IntStream.range(0, req.getId()).iterator()).map(this::response);
}
private CompatResponse response(final int value) {
if (errorMode == ErrorMode.SIMPLE_IN_RESPONSE) {
throwGrpcStatusException();
} else if (errorMode == ErrorMode.STATUS_IN_RESPONSE) {
throwGrpcStatusExceptionWithStatus();
}
return computeResponse(value);
}
private TerminalSignalConsumer errorConsumer() {
return new TerminalSignalConsumer() {
@Override
public void onComplete() {
}
@Override
public void onError(final Throwable throwable) {
reqStreamError.add(throwable);
}
@Override
public void cancel() {
reqStreamError.add(new IOException("cancelled"));
}
};
}
};
final ServiceFactory serviceFactory = new ServiceFactory.Builder().bufferEncoders(serviceTalkCompressions(compression)).bufferDecoderGroup(serviceTalkDecompression(compression)).bidirectionalStreamingCall(strategy, compatService).clientStreamingCall(strategy, compatService).scalarCall(strategy, compatService).serverStreamingCall(strategy, compatService).build();
final ServerContext serverContext = serviceTalkServerBuilder(errorMode, ssl, timeout, b -> b.executionStrategy(strategy)).listenAndAwait(serviceFactory);
return TestServerContext.fromServiceTalkServerContext(serverContext);
}
use of io.servicetalk.grpc.api.GrpcServiceContext in project servicetalk by apple.
the class TestSingle method singleGenerated.
@Test
void singleGenerated() throws ExecutionException, InterruptedException {
GreeterService service = new GreeterService() {
@Override
public Single<HelloReply> sayHelloFromMany(final GrpcServiceContext ctx, final Publisher<UserRequest> request) {
return Single.succeeded(HelloReply.newBuilder().build());
}
@Override
public Single<HelloReply> sayHello(final GrpcServiceContext ctx, final UserRequest request) {
return Single.succeeded(HelloReply.newBuilder().build());
}
@Override
public Publisher<HelloReply> sayHelloToFromMany(final GrpcServiceContext ctx, final Publisher<UserRequest> request) {
return Publisher.from(HelloReply.newBuilder().build());
}
@Override
public Publisher<HelloReply> sayHelloToMany(final GrpcServiceContext ctx, final UserRequest request) {
return Publisher.from(HelloReply.newBuilder().build());
}
};
FarewellerService service2 = new FarewellerService() {
@Override
public Publisher<Untils> getAllUntils(final GrpcServiceContext ctx, final Generic.Empty request) {
return Publisher.from(Untils.newBuilder().build());
}
@Override
public Single<SharedReply> sayGoodbye(final GrpcServiceContext ctx, final UserRequest request) {
return Single.succeeded(SharedReply.newBuilder().build());
}
};
service.closeAsync().toFuture().get();
service2.closeAsync().toFuture().get();
}
use of io.servicetalk.grpc.api.GrpcServiceContext in project servicetalk by apple.
the class TestMulti method multiGenerated.
@Test
void multiGenerated() throws ExecutionException, InterruptedException {
TesterService service = new TesterService() {
@Override
public Publisher<TestReply> testBiDiStream(final GrpcServiceContext ctx, final Publisher<TestRequest> request) {
return Publisher.from(TestReply.newBuilder().build());
}
@Override
public Single<TestReply> testRequestStream(final GrpcServiceContext ctx, final Publisher<TestRequest> request) {
return Single.succeeded(TestReply.newBuilder().build());
}
@Override
public Publisher<TestReply> testResponseStream(final GrpcServiceContext ctx, final TestRequest request) {
return Publisher.from(TestReply.newBuilder().build());
}
@Override
public Single<TestReply> test(final GrpcServiceContext ctx, final TestRequest request) {
return Single.succeeded(TestReply.newBuilder().build());
}
};
Tester2Service service2 = new Tester2Service() {
@Override
public Publisher<Untils> testUntils(final GrpcServiceContext ctx, final Generic.Empty request) {
return Publisher.from(Untils.newBuilder().build());
}
@Override
public Single<SharedReply> testShared(final GrpcServiceContext ctx, final TestRequest request) {
return Single.succeeded(SharedReply.newBuilder().build());
}
};
service.closeAsync().toFuture().get();
service2.closeAsync().toFuture().get();
}
Aggregations