Search in sources :

Example 6 with ServerInterceptor

use of io.grpc.ServerInterceptor in project pinpoint by naver.

the class StatServerTestMain method newStatBindableService.

private ServerServiceDefinition newStatBindableService(Executor executor) throws Exception {
    GrpcStreamConfiguration streamConfiguration = newStreamConfiguration();
    FactoryBean<ServerInterceptor> interceptorFactory = new StreamExecutorServerInterceptorFactory(executor, Executors.newSingleThreadScheduledExecutor(), streamConfiguration);
    ServerInterceptor interceptor = interceptorFactory.getObject();
    StatService statService = new StatService(new MockDispatchHandler(), new DefaultServerRequestFactory());
    return ServerInterceptors.intercept(statService, interceptor);
}
Also used : GrpcStreamConfiguration(com.navercorp.pinpoint.collector.grpc.config.GrpcStreamConfiguration) ServerInterceptor(io.grpc.ServerInterceptor) DefaultServerRequestFactory(com.navercorp.pinpoint.collector.receiver.grpc.service.DefaultServerRequestFactory) StatService(com.navercorp.pinpoint.collector.receiver.grpc.service.StatService) StreamExecutorServerInterceptorFactory(com.navercorp.pinpoint.collector.receiver.grpc.service.StreamExecutorServerInterceptorFactory)

Example 7 with ServerInterceptor

use of io.grpc.ServerInterceptor in project pinpoint by naver.

the class ChannelFactoryTest method addFilter.

private static void addFilter(ServerFactory serverFactory) {
    TransportMetadataFactory transportMetadataFactory = new TransportMetadataFactory(ChannelFactoryTest.class.getSimpleName());
    final ServerTransportFilter metadataTransportFilter = new MetadataServerTransportFilter(transportMetadataFactory);
    serverFactory.addTransportFilter(metadataTransportFilter);
    ServerInterceptor transportMetadataServerInterceptor = new TransportMetadataServerInterceptor();
    serverFactory.addInterceptor(transportMetadataServerInterceptor);
}
Also used : TransportMetadataServerInterceptor(com.navercorp.pinpoint.grpc.server.TransportMetadataServerInterceptor) ServerInterceptor(io.grpc.ServerInterceptor) TransportMetadataFactory(com.navercorp.pinpoint.grpc.server.TransportMetadataFactory) TransportMetadataServerInterceptor(com.navercorp.pinpoint.grpc.server.TransportMetadataServerInterceptor) ServerTransportFilter(io.grpc.ServerTransportFilter) MetadataServerTransportFilter(com.navercorp.pinpoint.grpc.server.MetadataServerTransportFilter) MetadataServerTransportFilter(com.navercorp.pinpoint.grpc.server.MetadataServerTransportFilter)

Example 8 with ServerInterceptor

use of io.grpc.ServerInterceptor in project grpc-java by grpc.

the class CascadingTest method startCallTreeServer.

/**
 * Create a tree of client to server calls where each received call on the server
 * fans out to two downstream calls. Uses SimpleRequest.response_size to limit the nodeCount
 * of the tree. One of the leaves will ABORT to trigger cancellation back up to tree.
 */
private void startCallTreeServer(int depthThreshold) throws IOException {
    final AtomicInteger nodeCount = new AtomicInteger((2 << depthThreshold) - 1);
    server = InProcessServerBuilder.forName("channel").executor(otherWork).addService(ServerInterceptors.intercept(service, new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            // Respond with the headers but nothing else.
            call.sendHeaders(new Metadata());
            call.request(1);
            return new ServerCall.Listener<ReqT>() {

                @Override
                public void onMessage(final ReqT message) {
                    Messages.SimpleRequest req = (Messages.SimpleRequest) message;
                    if (nodeCount.decrementAndGet() == 0) {
                        // we are in the final leaf node so trigger an ABORT upwards
                        Context.currentContextExecutor(otherWork).execute(new Runnable() {

                            @Override
                            public void run() {
                                synchronized (call) {
                                    call.close(Status.ABORTED, new Metadata());
                                }
                            }
                        });
                    } else if (req.getResponseSize() != 0) {
                        // We are in a non leaf node so fire off two requests
                        req = req.toBuilder().setResponseSize(req.getResponseSize() - 1).build();
                        for (int i = 0; i < 2; i++) {
                            asyncStub.unaryCall(req, new StreamObserver<Messages.SimpleResponse>() {

                                @Override
                                public void onNext(Messages.SimpleResponse value) {
                                }

                                @Override
                                public void onError(Throwable t) {
                                    Status status = Status.fromThrowable(t);
                                    if (status.getCode() == Status.Code.CANCELLED) {
                                        observedCancellations.countDown();
                                    }
                                    // Propagate closure upwards.
                                    try {
                                        synchronized (call) {
                                            call.close(status, new Metadata());
                                        }
                                    } catch (IllegalStateException t2) {
                                    // Ignore error if already closed.
                                    }
                                }

                                @Override
                                public void onCompleted() {
                                }
                            });
                        }
                    }
                }

                @Override
                public void onCancel() {
                    receivedCancellations.countDown();
                }
            };
        }
    })).build();
    server.start();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Status(io.grpc.Status) ServerCallHandler(io.grpc.ServerCallHandler) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) Metadata(io.grpc.Metadata) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerCall(io.grpc.ServerCall) ServerInterceptor(io.grpc.ServerInterceptor) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse)

Example 9 with ServerInterceptor

use of io.grpc.ServerInterceptor in project grpc-java by grpc.

the class TransportCompressionTest method getServerBuilder.

@Override
protected ServerBuilder<?> getServerBuilder() {
    NettyServerBuilder builder = NettyServerBuilder.forPort(0, InsecureServerCredentials.create()).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).compressorRegistry(compressors).decompressorRegistry(decompressors).intercept(new ServerInterceptor() {

        @Override
        public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            Listener<ReqT> listener = next.startCall(call, headers);
            // TODO(carl-mastrangelo): check that encoding was set.
            call.setMessageCompression(true);
            return listener;
        }
    });
    // Disable the default census stats tracer, use testing tracer instead.
    InternalNettyServerBuilder.setStatsEnabled(builder, false);
    return builder.addStreamTracerFactory(createCustomCensusTracerFactory());
}
Also used : NettyServerBuilder(io.grpc.netty.NettyServerBuilder) InternalNettyServerBuilder(io.grpc.netty.InternalNettyServerBuilder) ForwardingClientCallListener(io.grpc.ForwardingClientCallListener) Listener(io.grpc.ServerCall.Listener) ServerInterceptor(io.grpc.ServerInterceptor) Metadata(io.grpc.Metadata)

Example 10 with ServerInterceptor

use of io.grpc.ServerInterceptor in project grpc-java by grpc.

the class ServerImplTest method interceptors.

@Test
public void interceptors() throws Exception {
    final LinkedList<Context> capturedContexts = new LinkedList<>();
    final Context.Key<String> key1 = Context.key("key1");
    final Context.Key<String> key2 = Context.key("key2");
    final Context.Key<String> key3 = Context.key("key3");
    ServerInterceptor interceptor1 = new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            Context ctx = Context.current().withValue(key1, "value1");
            Context origCtx = ctx.attach();
            try {
                capturedContexts.add(ctx);
                return next.startCall(call, headers);
            } finally {
                ctx.detach(origCtx);
            }
        }
    };
    ServerInterceptor interceptor2 = new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            Context ctx = Context.current().withValue(key2, "value2");
            Context origCtx = ctx.attach();
            try {
                capturedContexts.add(ctx);
                return next.startCall(call, headers);
            } finally {
                ctx.detach(origCtx);
            }
        }
    };
    ServerCallHandler<String, Integer> callHandler = new ServerCallHandler<String, Integer>() {

        @Override
        public ServerCall.Listener<String> startCall(ServerCall<String, Integer> call, Metadata headers) {
            capturedContexts.add(Context.current().withValue(key3, "value3"));
            return callListener;
        }
    };
    mutableFallbackRegistry.addService(ServerServiceDefinition.builder(new ServiceDescriptor("Waiter", METHOD)).addMethod(METHOD, callHandler).build());
    builder.intercept(interceptor2);
    builder.intercept(interceptor1);
    createServer();
    server.start();
    ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
    transportListener.transportReady(Attributes.EMPTY);
    Metadata requestHeaders = new Metadata();
    StatsTraceContext statsTraceCtx = StatsTraceContext.newServerContext(streamTracerFactories, "Waiter/serve", requestHeaders);
    when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
    transportListener.streamCreated(stream, "Waiter/serve", requestHeaders);
    assertEquals(1, executor.runDueTasks());
    Context ctx1 = capturedContexts.poll();
    assertEquals("value1", key1.get(ctx1));
    assertNull(key2.get(ctx1));
    assertNull(key3.get(ctx1));
    Context ctx2 = capturedContexts.poll();
    assertEquals("value1", key1.get(ctx2));
    assertEquals("value2", key2.get(ctx2));
    assertNull(key3.get(ctx2));
    Context ctx3 = capturedContexts.poll();
    assertEquals("value1", key1.get(ctx3));
    assertEquals("value2", key2.get(ctx3));
    assertEquals("value3", key3.get(ctx3));
    assertTrue(capturedContexts.isEmpty());
}
Also used : Context(io.grpc.Context) ServerCallHandler(io.grpc.ServerCallHandler) Metadata(io.grpc.Metadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LinkedList(java.util.LinkedList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerCall(io.grpc.ServerCall) ServiceDescriptor(io.grpc.ServiceDescriptor) ServerInterceptor(io.grpc.ServerInterceptor) Test(org.junit.Test)

Aggregations

ServerInterceptor (io.grpc.ServerInterceptor)37 Metadata (io.grpc.Metadata)23 Test (org.junit.Test)15 ServerCall (io.grpc.ServerCall)10 ServerCallHandler (io.grpc.ServerCallHandler)9 SimpleForwardingServerCallListener (io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener)7 Server (io.grpc.Server)6 IOException (java.io.IOException)6 SimpleForwardingServerCall (io.grpc.ForwardingServerCall.SimpleForwardingServerCall)4 Status (io.grpc.Status)4 ManagedChannel (io.grpc.ManagedChannel)3 Listener (io.grpc.ServerCall.Listener)3 ServerTransportFilter (io.grpc.ServerTransportFilter)3 InetSocketAddress (java.net.InetSocketAddress)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 TraceContext (brave.propagation.TraceContext)2 GrpcStreamConfiguration (com.navercorp.pinpoint.collector.grpc.config.GrpcStreamConfiguration)2 DefaultServerRequestFactory (com.navercorp.pinpoint.collector.receiver.grpc.service.DefaultServerRequestFactory)2 StreamExecutorServerInterceptorFactory (com.navercorp.pinpoint.collector.receiver.grpc.service.StreamExecutorServerInterceptorFactory)2 MetadataServerTransportFilter (com.navercorp.pinpoint.grpc.server.MetadataServerTransportFilter)2