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