Java Code Examples for com.linecorp.armeria.common.RequestContext#current()

The following examples show how to use com.linecorp.armeria.common.RequestContext#current() . 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: WatchService.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
/**
 * Awaits and retrieves the latest revision of the commit that changed the file that matches the specified
 * {@link Query} since the specified {@code lastKnownRevision}. This will wait until the specified
 * {@code timeoutMillis} passes. If there's no change during the time, the returned future will be
 * exceptionally completed with the {@link CancellationException}.
 */
public <T> CompletableFuture<Entry<T>> watchFile(Repository repo, Revision lastKnownRevision,
                                                 Query<T> query, long timeoutMillis) {
    final ServiceRequestContext ctx = RequestContext.current();
    updateRequestTimeout(ctx, timeoutMillis);
    final CompletableFuture<Entry<T>> result = repo.watch(lastKnownRevision, query);
    if (result.isDone()) {
        return result;
    }

    scheduleTimeout(ctx, result, timeoutMillis);
    return result;
}
 
Example 2
Source File: ServiceRequestContext.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the server-side context of the {@link Request} that is being handled in the current thread.
 * If the context is a {@link ClientRequestContext}, {@link ClientRequestContext#root()} is returned.
 *
 * @throws IllegalStateException if the context is unavailable in the current thread or
 *                               the current context is a {@link ClientRequestContext} and
 *                               {@link ClientRequestContext#root()} is {@code null}
 */
static ServiceRequestContext current() {
    final RequestContext ctx = RequestContext.current();
    final ServiceRequestContext root = ctx.root();
    if (root != null) {
        return root;
    }

    throw new IllegalStateException(
            "The current context is not a server-side context and does not have a root " +
            "which means that the context is not invoked by a server request. ctx: " + ctx);
}
 
Example 3
Source File: ClientRequestContext.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the client-side context of the {@link Request} that is being handled in the current thread.
 *
 * @throws IllegalStateException if the context is unavailable in the current thread or
 *                               the current context is not a {@link ClientRequestContext}.
 */
static ClientRequestContext current() {
    final RequestContext ctx = RequestContext.current();
    checkState(ctx instanceof ClientRequestContext,
               "The current context is not a client-side context: %s", ctx);
    return (ClientRequestContext) ctx;
}
 
Example 4
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Completable completable(@SuppressWarnings("unused") String input) {
    RequestContext.current();
    return Completable.create(emitter -> {
        RequestContext.current();
        emitter.onComplete();
    });
}
 
Example 5
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Maybe<String> maybe(String input) {
    RequestContext.current();
    return Maybe.create(emitter -> {
        RequestContext.current();
        emitter.onSuccess(input);
    });
}
 
Example 6
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Single<String> single(String input) {
    RequestContext.current();
    return Single.create(emitter -> {
        RequestContext.current();
        emitter.onSuccess(input);
    });
}
 
Example 7
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
static Flowable<String> flowable(int count) {
    RequestContext.current();
    return Flowable.create(emitter -> {
        pool.submit(() -> {
            for (int i = 0; i < count; i++) {
                emitter.onNext(String.valueOf(count));
            }
            emitter.onComplete();
        });
    }, BackpressureStrategy.BUFFER);
}
 
Example 8
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Completable completable(@SuppressWarnings("unused") String input) {
    RequestContext.current();
    return Completable.create(emitter -> {
        RequestContext.current();
        emitter.onComplete();
    });
}
 
Example 9
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Maybe<String> maybe(String input) {
    RequestContext.current();
    return Maybe.create(emitter -> {
        RequestContext.current();
        emitter.onSuccess(input);
    });
}
 
Example 10
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Single<String> single(String input) {
    RequestContext.current();
    return Single.create(emitter -> {
        RequestContext.current();
        emitter.onSuccess(input);
    });
}
 
Example 11
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
Flowable<String> flowable(int count) {
    RequestContext.current();
    return Flowable.create(emitter -> {
        pool.submit(() -> {
            for (int i = 0; i < count; i++) {
                emitter.onNext(String.valueOf(count));
            }
            emitter.onComplete();
        });
    }, BackpressureStrategy.BUFFER);
}
 
Example 12
Source File: WatchService.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
/**
 * Awaits and retrieves the latest revision of the commit that changed the file that matches the specified
 * {@code pathPattern} since the specified {@code lastKnownRevision}. This will wait until the specified
 * {@code timeoutMillis} passes. If there's no change during the time, the returned future will be
 * exceptionally completed with the {@link CancellationException}.
 */
public CompletableFuture<Revision> watchRepository(Repository repo, Revision lastKnownRevision,
                                                   String pathPattern, long timeoutMillis) {
    final ServiceRequestContext ctx = RequestContext.current();
    updateRequestTimeout(ctx, timeoutMillis);
    final CompletableFuture<Revision> result = repo.watch(lastKnownRevision, pathPattern);
    if (result.isDone()) {
        return result;
    }

    scheduleTimeout(ctx, result, timeoutMillis);
    return result;
}
 
Example 13
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static String checkRequestContext(String ignored) {
    RequestContext.current();
    return ignored;
}
 
Example 14
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static Single<String> nestSingle(String input) {
    RequestContext.current();
    return single(input).flatMap(RequestContextAssemblyTest::single);
}
 
Example 15
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static Single<String> nestSingle(String input) {
    RequestContext.current();
    return single(input).flatMap(RequestContextAssemblyTest::single);
}
 
Example 16
Source File: ServerModule.java    From curiostack with MIT License 4 votes vote down vote up
@Provides
static ServiceRequestContext context() {
  return RequestContext.current();
}
 
Example 17
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static String checkRequestContext(String ignored) {
    RequestContext.current();
    return ignored;
}
 
Example 18
Source File: Subscriber.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public void onNext(StreamingPullResponse value) {
  if (ctx == null) {
    ctx = RequestContext.current();
  }

  streamReconnectBackoff = INITIAL_CHANNEL_RECONNECT_BACKOFF;

  receivedMessages.increment(value.getReceivedMessagesCount());

  AtomicInteger pendingAcks = new AtomicInteger(value.getReceivedMessagesCount());

  for (ReceivedMessage message : value.getReceivedMessagesList()) {
    TraceContextOrSamplingFlags contextOrFlags = traceExtractor.extract(message.getMessage());

    // Add an artificial span modeling the time spent within Pub/Sub until getting here.
    Span span =
        contextOrFlags.context() != null
            ? tracer.joinSpan(contextOrFlags.context())
            // We want each message to be a new trace rather than having a long trace for the
            // entire stream.
            : tracer.newTrace();

    span.kind(Kind.SERVER)
        .name("google.pubsub.v1.Publisher.Publish")
        .tag("subscription", options.getSubscription())
        .start(Timestamps.toMicros(message.getMessage().getPublishTime()))
        .finish();

    StreamObserver<StreamingPullRequest> requestObserver = this.requestObserver;

    long startTimeNanos = System.nanoTime();
    options
        .getMessageReceiver()
        .receiveMessage(
            message.getMessage(),
            new AckReplyConsumer() {
              @Override
              public void ack() {
                releaseAndRecord();

                ackedMessages.increment();

                checkNotNull(requestObserver, "onNext called before start()");
                requestObserver.onNext(
                    StreamingPullRequest.newBuilder().addAckIds(message.getAckId()).build());
              }

              @Override
              public void nack() {
                releaseAndRecord();

                nackedMessages.increment();

                checkNotNull(requestObserver, "onNext called before start()");
                requestObserver.onNext(
                    StreamingPullRequest.newBuilder()
                        .addModifyDeadlineAckIds(message.getAckId())
                        .addModifyDeadlineSeconds(0)
                        .build());
              }

              private void releaseAndRecord() {
                if (options.getUnsafeWrapBuffers() && pendingAcks.decrementAndGet() == 0) {
                  GrpcUnsafeBufferUtil.releaseBuffer(value, ctx);
                }

                messageProcessingTime.record(
                    Duration.ofNanos(System.nanoTime() - startTimeNanos));
              }
            });
  }
}