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;
}
}
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);
}
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());
}
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;
}
Aggregations