Search in sources :

Example 1 with ServerTransportFilter

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

the class GrpcReceiver method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    if (Boolean.FALSE == this.enable) {
        logger.warn("{} is {}", this.beanName, enable);
        return;
    }
    Objects.requireNonNull(this.beanName, "beanName");
    Objects.requireNonNull(this.bindAddress, "bindAddress");
    Objects.requireNonNull(this.addressFilter, "addressFilter");
    Assert.isTrue(CollectionUtils.hasLength(this.serviceList), "serviceList must not be empty");
    Objects.requireNonNull(this.serverOption, "serverOption");
    if (grpcSslConfiguration != null) {
        final SslServerConfig sslServerConfig = grpcSslConfiguration.toSslServerConfig();
        this.serverFactory = new ServerFactory(beanName, this.bindAddress.getIp(), this.bindAddress.getPort(), this.executor, serverOption, sslServerConfig);
    } else {
        this.serverFactory = new ServerFactory(beanName, this.bindAddress.getIp(), this.bindAddress.getPort(), this.executor, serverOption);
    }
    ServerTransportFilter permissionServerTransportFilter = new PermissionServerTransportFilter(this.beanName, addressFilter);
    this.serverFactory.addTransportFilter(permissionServerTransportFilter);
    TransportMetadataFactory transportMetadataFactory = new TransportMetadataFactory(beanName);
    // Mandatory interceptor
    final ServerTransportFilter metadataTransportFilter = new MetadataServerTransportFilter(transportMetadataFactory);
    this.serverFactory.addTransportFilter(metadataTransportFilter);
    if (CollectionUtils.hasLength(transportFilterList)) {
        for (ServerTransportFilter transportFilter : transportFilterList) {
            this.serverFactory.addTransportFilter(transportFilter);
        }
    }
    // Mandatory interceptor
    ServerInterceptor transportMetadataServerInterceptor = new TransportMetadataServerInterceptor();
    this.serverFactory.addInterceptor(transportMetadataServerInterceptor);
    if (CollectionUtils.hasLength(serverInterceptorList)) {
        for (ServerInterceptor serverInterceptor : serverInterceptorList) {
            this.serverFactory.addInterceptor(serverInterceptor);
        }
    }
    if (channelzRegistry != null) {
        this.serverFactory.setChannelzRegistry(channelzRegistry);
    }
    // Add service
    addService();
    this.server = serverFactory.build();
    if (logger.isInfoEnabled()) {
        logger.info("Start {} server {}", this.beanName, this.server);
    }
    try {
        this.server.start();
    } catch (Throwable th) {
        final Throwable rootCause = NestedExceptionUtils.getRootCause(th);
        if (rootCause instanceof BindException) {
            logger.error("Server bind failed. {} address:{}", this.beanName, this.bindAddress, rootCause);
        } else {
            logger.error("Server start failed. {} address:{}", this.beanName, this.bindAddress);
        }
        throw th;
    }
}
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) BindException(java.net.BindException) ServerFactory(com.navercorp.pinpoint.grpc.server.ServerFactory) ServerTransportFilter(io.grpc.ServerTransportFilter) MetadataServerTransportFilter(com.navercorp.pinpoint.grpc.server.MetadataServerTransportFilter) MetadataServerTransportFilter(com.navercorp.pinpoint.grpc.server.MetadataServerTransportFilter) SslServerConfig(com.navercorp.pinpoint.grpc.security.SslServerConfig)

Example 2 with ServerTransportFilter

use of io.grpc.ServerTransportFilter 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 3 with ServerTransportFilter

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

the class ServerImplTest method transportFilters.

@Test
public void transportFilters() throws Exception {
    final SocketAddress remoteAddr = mock(SocketAddress.class);
    final Attributes.Key<String> key1 = Attributes.Key.create("test-key1");
    final Attributes.Key<String> key2 = Attributes.Key.create("test-key2");
    final Attributes.Key<String> key3 = Attributes.Key.create("test-key3");
    final AtomicReference<Attributes> filter1TerminationCallbackArgument = new AtomicReference<>();
    final AtomicReference<Attributes> filter2TerminationCallbackArgument = new AtomicReference<>();
    final AtomicInteger readyCallbackCalled = new AtomicInteger(0);
    final AtomicInteger terminationCallbackCalled = new AtomicInteger(0);
    builder.addTransportFilter(new ServerTransportFilter() {

        @Override
        public Attributes transportReady(Attributes attrs) {
            assertEquals(Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, remoteAddr).build(), attrs);
            readyCallbackCalled.incrementAndGet();
            return attrs.toBuilder().set(key1, "yalayala").set(key2, "blabla").build();
        }

        @Override
        public void transportTerminated(Attributes attrs) {
            terminationCallbackCalled.incrementAndGet();
            filter1TerminationCallbackArgument.set(attrs);
        }
    });
    builder.addTransportFilter(new ServerTransportFilter() {

        @Override
        public Attributes transportReady(Attributes attrs) {
            assertEquals(Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, remoteAddr).set(key1, "yalayala").set(key2, "blabla").build(), attrs);
            readyCallbackCalled.incrementAndGet();
            return attrs.toBuilder().set(key1, "ouch").set(key3, "puff").build();
        }

        @Override
        public void transportTerminated(Attributes attrs) {
            terminationCallbackCalled.incrementAndGet();
            filter2TerminationCallbackArgument.set(attrs);
        }
    });
    Attributes expectedTransportAttrs = Attributes.newBuilder().set(key1, "ouch").set(key2, "blabla").set(key3, "puff").set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, remoteAddr).build();
    createAndStartServer();
    ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
    Attributes transportAttrs = transportListener.transportReady(Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, remoteAddr).build());
    assertEquals(expectedTransportAttrs, transportAttrs);
    server.shutdown();
    server.awaitTermination();
    assertEquals(expectedTransportAttrs, filter1TerminationCallbackArgument.get());
    assertEquals(expectedTransportAttrs, filter2TerminationCallbackArgument.get());
    assertEquals(2, readyCallbackCalled.get());
    assertEquals(2, terminationCallbackCalled.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Attributes(io.grpc.Attributes) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServerTransportFilter(io.grpc.ServerTransportFilter) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 4 with ServerTransportFilter

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

the class ServerFactory method build.

public Server build() throws SSLException {
    InetSocketAddress bindAddress = new InetSocketAddress(this.hostname, this.port);
    PinpointNettyServerBuilder serverBuilder = PinpointNettyServerBuilder.forAddress(bindAddress);
    serverBuilder.serverListenerDelegator(new LogIdServerListenerDelegator());
    logger.info("ChannelType:{}", channelType.getSimpleName());
    serverBuilder.channelType(channelType);
    serverBuilder.bossEventLoopGroup(bossEventLoopGroup);
    serverBuilder.workerEventLoopGroup(workerEventLoopGroup);
    setupInternal(serverBuilder);
    for (Object service : this.bindableServices) {
        if (service instanceof BindableService) {
            logger.info("Add BindableService={}, server={}", service, name);
            serverBuilder.addService((BindableService) service);
        } else if (service instanceof ServerServiceDefinition) {
            final ServerServiceDefinition definition = (ServerServiceDefinition) service;
            logger.info("Add ServerServiceDefinition={}, server={}", definition.getServiceDescriptor(), name);
            serverBuilder.addService(definition);
        }
    }
    for (ServerTransportFilter transportFilter : this.serverTransportFilters) {
        logger.info("Add transportFilter={}, server={}", transportFilter, name);
        serverBuilder.addTransportFilter(transportFilter);
    }
    for (ServerInterceptor serverInterceptor : this.serverInterceptors) {
        logger.info("Add intercept={}, server={}", serverInterceptor, name);
        serverBuilder.intercept(serverInterceptor);
    }
    serverBuilder.executor(serverExecutor);
    setupServerOption(serverBuilder);
    if (sslServerConfig.isEnable()) {
        logger.debug("Enable sslConfig.({})", sslServerConfig);
        SslContext sslContext = SslContextFactory.create(sslServerConfig);
        serverBuilder.sslContext(sslContext);
    }
    Server server = serverBuilder.build();
    if (server instanceof InternalWithLogId) {
        final InternalWithLogId logId = (InternalWithLogId) server;
        final long serverLogId = logId.getLogId().getId();
        logger.info("{} serverLogId:{}", name, serverLogId);
        if (channelzRegistry != null) {
            channelzRegistry.addServer(serverLogId, name);
        }
    }
    return server;
}
Also used : Server(io.grpc.Server) PinpointNettyServerBuilder(io.grpc.netty.PinpointNettyServerBuilder) InetSocketAddress(java.net.InetSocketAddress) ServerServiceDefinition(io.grpc.ServerServiceDefinition) ServerInterceptor(io.grpc.ServerInterceptor) InternalWithLogId(io.grpc.InternalWithLogId) ServerTransportFilter(io.grpc.ServerTransportFilter) LogIdServerListenerDelegator(io.grpc.netty.LogIdServerListenerDelegator) BindableService(io.grpc.BindableService) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

ServerTransportFilter (io.grpc.ServerTransportFilter)4 ServerInterceptor (io.grpc.ServerInterceptor)3 MetadataServerTransportFilter (com.navercorp.pinpoint.grpc.server.MetadataServerTransportFilter)2 TransportMetadataFactory (com.navercorp.pinpoint.grpc.server.TransportMetadataFactory)2 TransportMetadataServerInterceptor (com.navercorp.pinpoint.grpc.server.TransportMetadataServerInterceptor)2 InetSocketAddress (java.net.InetSocketAddress)2 SslServerConfig (com.navercorp.pinpoint.grpc.security.SslServerConfig)1 ServerFactory (com.navercorp.pinpoint.grpc.server.ServerFactory)1 Attributes (io.grpc.Attributes)1 BindableService (io.grpc.BindableService)1 InternalWithLogId (io.grpc.InternalWithLogId)1 Server (io.grpc.Server)1 ServerServiceDefinition (io.grpc.ServerServiceDefinition)1 LogIdServerListenerDelegator (io.grpc.netty.LogIdServerListenerDelegator)1 PinpointNettyServerBuilder (io.grpc.netty.PinpointNettyServerBuilder)1 SslContext (io.netty.handler.ssl.SslContext)1 BindException (java.net.BindException)1 SocketAddress (java.net.SocketAddress)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1