Java Code Examples for io.grpc.Context#ROOT

The following examples show how to use io.grpc.Context#ROOT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GrpcContextRule.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            // Reset the gRPC context between test executions
            Context prev = Context.ROOT.attach();
            try {
                base.evaluate();
                if (Context.current() != Context.ROOT) {
                    Assert.fail("Test is leaking context state between tests! Ensure proper " +
                            "attach()/detach() pairing.");
                }
            } finally {
                Context.ROOT.detach(prev);
            }
        }
    };
}
 
Example 2
Source File: AbstractServerImplBuilder.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public Server build() {
  ServerImpl server = new ServerImpl(
      this,
      buildTransportServer(Collections.unmodifiableList(getTracerFactories())),
      Context.ROOT);
  for (InternalNotifyOnServerBuild notifyTarget : notifyOnBuildList) {
    notifyTarget.notifyOnBuild(server);
  }
  return server;
}
 
Example 3
Source File: SampleContextServerInterceptor.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
public static SampleContext serverResolve() {
    if (Context.current() == Context.ROOT) {
        return CONTEXT_UNDEFINED;
    }

    SampleContext context = CALLER_ID_CONTEXT_KEY.get();
    return context == null ? CONTEXT_UNDEFINED : context;
}
 
Example 4
Source File: CallMetricRecorderTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getCurrent_blankContext() {
  Context blankCtx = Context.ROOT;
  Context origCtx = blankCtx.attach();
  try {
    assertThat(CallMetricRecorder.getCurrent().isDisabled()).isTrue();
  } finally {
    blankCtx.detach(origCtx);
  }
}
 
Example 5
Source File: SimpleGrpcCallMetadataResolver.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<CallMetadata> resolve() {
    if (Context.current() == Context.ROOT) {
        // Not in GRPC server call.
        return Optional.empty();
    }

    CallMetadata forwardedCallMetadata = V3HeaderInterceptor.CALL_METADATA_CONTEXT_KEY.get();
    Caller directCaller = resolveDirectCallerInternal();

    // If we have CallMetadata instance, we can safely ignore other headers, except the direct caller.
    if (forwardedCallMetadata != null) {
        return Optional.of(forwardedCallMetadata.toBuilder()
                .withCallPath(CollectionsExt.copyAndAdd(forwardedCallMetadata.getCallPath(), directCaller.getId()))
                .withCallers(CollectionsExt.copyAndAdd(forwardedCallMetadata.getCallers(), directCaller))
                .build());
    }

    // No CellMetadata in header, so we must built it here.
    String callerId = V3HeaderInterceptor.CALLER_ID_CONTEXT_KEY.get();
    String callReason = getOrDefault(V3HeaderInterceptor.CALL_REASON_CONTEXT_KEY.get(), "reason not given");

    CallMetadata.Builder callMetadataBuilder = CallMetadata.newBuilder().withCallReason(callReason);

    if (callerId == null) {
        callMetadataBuilder
                .withCallerId(directCaller.getId())
                .withCallPath(Collections.singletonList(directCaller.getId()))
                .withCallers(Collections.singletonList(directCaller));
    } else {
        Caller originalCaller = Caller.newBuilder()
                .withId(callerId)
                .withCallerType(CallerType.parseCallerType(callerId, V3HeaderInterceptor.CALLER_TYPE_CONTEXT_KEY.get()))
                .build();

        callMetadataBuilder
                .withCallerId(callerId)
                .withCallPath(asList(callerId, directCaller.getId()))
                .withCallers(asList(originalCaller, directCaller));
    }

    return Optional.of(callMetadataBuilder.build());
}
 
Example 6
Source File: AbstractServerImplBuilder.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public final Server build() {
  return new ServerImpl(this, buildTransportServers(getTracerFactories()), Context.ROOT);
}