Search in sources :

Example 1 with NonNull

use of reactor.util.annotation.NonNull in project incubator-shenyu by apache.

the class CryptorResponseDecorator method writeWith.

@Override
@NonNull
@SuppressWarnings("unchecked")
public Mono<Void> writeWith(@NonNull final Publisher<? extends DataBuffer> body) {
    ClientResponse clientResponse = ResponseUtils.buildClientResponse(this.getDelegate(), body);
    Mono<String> mono = clientResponse.bodyToMono(String.class).flatMap(originalBody -> strategyMatch(originalBody, this.ruleHandle, this.exchange));
    return ResponseUtils.writeWith(clientResponse, this.exchange, mono, String.class);
}
Also used : ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) NonNull(reactor.util.annotation.NonNull)

Example 2 with NonNull

use of reactor.util.annotation.NonNull in project incubator-shenyu by apache.

the class LoggingServerHttpResponse method appendResponse.

/**
 * append response.
 *
 * @param body publisher
 * @return wrap Flux
 */
@NonNull
private Flux<? extends DataBuffer> appendResponse(final Publisher<? extends DataBuffer> body) {
    ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
    assert shenyuContext != null;
    if (getStatusCode() != null) {
        logInfo.setStatus(getStatusCode().value());
    }
    logInfo.setResponseHeader(LogCollectUtils.getResponseHeaders(getHeaders()));
    BodyWriter writer = new BodyWriter();
    String traceId = (String) exchange.getAttributes().get(SHENYU_AGENT_TRACE_ID);
    logInfo.setTraceId(traceId);
    boolean collectResponseBody = LogCollectConfigUtils.getLogFieldSwitchConfig().isResponseBody();
    return Flux.from(body).doOnNext(buffer -> {
        if (LogCollectUtils.isNotBinaryType(getHeaders()) && collectResponseBody) {
            writer.write(buffer.asByteBuffer().asReadOnlyBuffer());
        }
    }).doFinally(signal -> logResponse(shenyuContext, writer));
}
Also used : Constants(org.apache.shenyu.common.constant.Constants) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) LogCollectConfigUtils(org.apache.shenyu.agent.plugin.logging.common.utils.LogCollectConfigUtils) StringUtils(org.apache.commons.lang3.StringUtils) DateUtils(org.apache.shenyu.common.utils.DateUtils) LogCollector(org.apache.shenyu.agent.plugin.logging.LogCollector) LogCollectUtils(org.apache.shenyu.agent.plugin.logging.common.utils.LogCollectUtils) RpcTypeEnum(org.apache.shenyu.common.enums.RpcTypeEnum) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponseDecorator(org.springframework.http.server.reactive.ServerHttpResponseDecorator) URI(java.net.URI) ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) Logger(org.slf4j.Logger) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) ShenyuRequestLog(org.apache.shenyu.agent.plugin.logging.entity.ShenyuRequestLog) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) InetSocketAddress(java.net.InetSocketAddress) Flux(reactor.core.publisher.Flux) NonNull(reactor.util.annotation.NonNull) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) NonNull(reactor.util.annotation.NonNull)

Example 3 with NonNull

use of reactor.util.annotation.NonNull in project reactor-core by reactor.

the class ExecutorSchedulerTest method failingAndShutDownExecutorServiceIsTerminated.

@Test
public void failingAndShutDownExecutorServiceIsTerminated() {
    final IllegalStateException boom = new IllegalStateException("boom");
    ExecutorService service = new AbstractExecutorService() {

        boolean shutdown;

        @Override
        public void shutdown() {
            shutdown = true;
        }

        @NonNull
        @Override
        public List<Runnable> shutdownNow() {
            return Collections.emptyList();
        }

        @Override
        public boolean isShutdown() {
            return shutdown;
        }

        @Override
        public boolean isTerminated() {
            return shutdown;
        }

        @Override
        public boolean awaitTermination(long timeout, @NonNull TimeUnit unit) throws InterruptedException {
            return false;
        }

        @Override
        public void execute(@NonNull Runnable command) {
            if (shutdown)
                throw boom;
            shutdown = true;
        }
    };
    ExecutorScheduler scheduler = new ExecutorScheduler(service, false);
    assertThatCode(() -> scheduler.schedule(() -> {
    })).as("initial-no rejection").doesNotThrowAnyException();
    assertThatExceptionOfType(RejectedExecutionException.class).as("second-transient rejection").isThrownBy(() -> scheduler.schedule(() -> {
    })).withCause(boom);
    assertThatExceptionOfType(RejectedExecutionException.class).as("third scheduler terminated rejection").isThrownBy(() -> scheduler.schedule(() -> {
    })).isSameAs(Exceptions.failWithRejected()).withNoCause();
}
Also used : NonNull(reactor.util.annotation.NonNull) AbstractExecutorService(java.util.concurrent.AbstractExecutorService) ExecutorService(java.util.concurrent.ExecutorService) TimeUnit(java.util.concurrent.TimeUnit) AbstractExecutorService(java.util.concurrent.AbstractExecutorService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.jupiter.api.Test)

Example 4 with NonNull

use of reactor.util.annotation.NonNull in project reactor-core by reactor.

the class FluxIterableTest method infiniteGeneratorDoesntHangFusedDiscard.

@Test
void infiniteGeneratorDoesntHangFusedDiscard() {
    class Generator implements Iterable<Integer> {

        final int seed;

        Generator(int seed) {
            this.seed = seed;
        }

        @NonNull
        @Override
        public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {

                int value = seed;

                @Override
                public boolean hasNext() {
                    return true;
                }

                @Override
                public Integer next() {
                    return value++;
                }
            };
        }
    }
    Generator one = new Generator(1);
    // smoke test: this Iterable is indeed NOT SIZED
    assertThat(one.spliterator().hasCharacteristics(Spliterator.SIZED)).as("spliterator not sized").isFalse();
    AtomicInteger discardCount = new AtomicInteger();
    Flux.fromIterable(one).publishOn(Schedulers.single()).take(10, false).doOnDiscard(Integer.class, i -> discardCount.incrementAndGet()).blockLast(Duration.ofSeconds(1));
    assertThat(discardCount).as("discardCount").hasValue(0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) MockUtils(reactor.test.MockUtils) Subscriber(org.reactivestreams.Subscriber) Iterator(java.util.Iterator) Context(reactor.util.context.Context) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) NonNull(reactor.util.annotation.NonNull) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Collections(java.util.Collections) Spliterator(java.util.Spliterator) Timeout(org.junit.jupiter.api.Timeout) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) Test(org.junit.jupiter.api.Test)

Example 5 with NonNull

use of reactor.util.annotation.NonNull in project reactor-core by reactor.

the class ExecutorSchedulerTest method failingExecutorServiceIsNotTerminated.

@Test
public void failingExecutorServiceIsNotTerminated() {
    AtomicInteger count = new AtomicInteger();
    final IllegalStateException boom = new IllegalStateException("boom");
    ExecutorService service = new AbstractExecutorService() {

        boolean shutdown;

        @Override
        public void shutdown() {
            shutdown = true;
        }

        @NonNull
        @Override
        public List<Runnable> shutdownNow() {
            return Collections.emptyList();
        }

        @Override
        public boolean isShutdown() {
            return shutdown;
        }

        @Override
        public boolean isTerminated() {
            return shutdown;
        }

        @Override
        public boolean awaitTermination(long timeout, @NonNull TimeUnit unit) throws InterruptedException {
            return false;
        }

        @Override
        public void execute(@NonNull Runnable command) {
            if (count.incrementAndGet() % 2 == 0)
                throw boom;
        }
    };
    ExecutorScheduler scheduler = new ExecutorScheduler(service, false);
    assertThatCode(() -> scheduler.schedule(() -> {
    })).as("initial-no rejection").doesNotThrowAnyException();
    assertThatExceptionOfType(RejectedExecutionException.class).as("second-transient rejection").isThrownBy(() -> scheduler.schedule(() -> {
    })).withCause(boom);
    assertThatCode(() -> scheduler.schedule(() -> {
    })).as("third-no rejection").doesNotThrowAnyException();
    assertThat(count).hasValue(3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NonNull(reactor.util.annotation.NonNull) AbstractExecutorService(java.util.concurrent.AbstractExecutorService) ExecutorService(java.util.concurrent.ExecutorService) TimeUnit(java.util.concurrent.TimeUnit) AbstractExecutorService(java.util.concurrent.AbstractExecutorService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

NonNull (reactor.util.annotation.NonNull)7 TimeUnit (java.util.concurrent.TimeUnit)3 Test (org.junit.jupiter.api.Test)3 AbstractExecutorService (java.util.concurrent.AbstractExecutorService)2 ExecutorService (java.util.concurrent.ExecutorService)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Type (java.lang.reflect.Type)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 URL (java.net.URL)1 Duration (java.time.Duration)1 LocalDateTime (java.time.LocalDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Optional (java.util.Optional)1