Search in sources :

Example 1 with NettyDataBufferFactory

use of cn.taketoday.core.io.buffer.NettyDataBufferFactory in project today-infrastructure by TAKETODAY.

the class AbstractDataBufferAllocatingTests method verifyAllocations.

private void verifyAllocations() {
    if (this.bufferFactory instanceof NettyDataBufferFactory) {
        ByteBufAllocator allocator = ((NettyDataBufferFactory) this.bufferFactory).getByteBufAllocator();
        if (allocator instanceof PooledByteBufAllocator) {
            Instant start = Instant.now();
            while (true) {
                PooledByteBufAllocatorMetric metric = ((PooledByteBufAllocator) allocator).metric();
                long total = getAllocations(metric.directArenas()) + getAllocations(metric.heapArenas());
                if (total == 0) {
                    return;
                }
                if (Instant.now().isBefore(start.plus(Duration.ofSeconds(5)))) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException ex) {
                    // ignore
                    }
                    continue;
                }
                assertThat(total).as("ByteBuf Leak: " + total + " unreleased allocations").isEqualTo(0);
            }
        }
    }
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Instant(java.time.Instant) PooledByteBufAllocatorMetric(io.netty.buffer.PooledByteBufAllocatorMetric) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 2 with NettyDataBufferFactory

use of cn.taketoday.core.io.buffer.NettyDataBufferFactory in project today-framework by TAKETODAY.

the class BodyExtractorsTests method unsupportedMediaTypeShouldConsumeAndCancel.

// SPR-17054
@Test
public void unsupportedMediaTypeShouldConsumeAndCancel() {
    NettyDataBufferFactory factory = new NettyDataBufferFactory(new PooledByteBufAllocator(true));
    NettyDataBuffer buffer = factory.wrap(ByteBuffer.wrap("spring".getBytes(StandardCharsets.UTF_8)));
    TestPublisher<DataBuffer> body = TestPublisher.create();
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    response.getHeaders().setContentType(MediaType.APPLICATION_PDF);
    response.setBody(body.flux());
    BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
    StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
        body.assertWasSubscribed();
        body.emit(buffer);
    }).expectErrorSatisfies(throwable -> {
        boolean condition = throwable instanceof UnsupportedMediaTypeException;
        assertThat(condition).isTrue();
        assertThatExceptionOfType(IllegalReferenceCountException.class).isThrownBy(buffer::release);
        body.assertCancelled();
    }).verify();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) TestPublisher(reactor.test.publisher.TestPublisher) JsonView(com.fasterxml.jackson.annotation.JsonView) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) StepVerifier(reactor.test.StepVerifier) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) NettyDataBuffer(cn.taketoday.core.io.buffer.NettyDataBuffer) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) FilePart(cn.taketoday.http.codec.multipart.FilePart) FormFieldPart(cn.taketoday.http.codec.multipart.FormFieldPart) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) DefaultPartHttpMessageReader(cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MultiValueMap(cn.taketoday.core.MultiValueMap) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) MultipartHttpMessageReader(cn.taketoday.http.codec.multipart.MultipartHttpMessageReader) TypeReference(cn.taketoday.core.TypeReference) StringDecoder(cn.taketoday.core.codec.StringDecoder) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) Mono(reactor.core.publisher.Mono) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) ServerHttpRequest(cn.taketoday.http.server.reactive.ServerHttpRequest) Optional(java.util.Optional) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) HttpStatus(cn.taketoday.http.HttpStatus) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) Part(cn.taketoday.http.codec.multipart.Part) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) JSON_VIEW_HINT(cn.taketoday.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT) Mono(reactor.core.publisher.Mono) NettyDataBuffer(cn.taketoday.core.io.buffer.NettyDataBuffer) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) NettyDataBuffer(cn.taketoday.core.io.buffer.NettyDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with NettyDataBufferFactory

use of cn.taketoday.core.io.buffer.NettyDataBufferFactory in project today-framework by TAKETODAY.

the class ReactorHttpHandlerAdapter method apply.

@Override
public Mono<Void> apply(HttpServerRequest reactorRequest, HttpServerResponse reactorResponse) {
    NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(reactorResponse.alloc());
    try {
        ServerHttpResponse response = new ReactorServerHttpResponse(reactorResponse, bufferFactory);
        ReactorServerHttpRequest request = new ReactorServerHttpRequest(reactorRequest, bufferFactory);
        if (request.getMethod() == HttpMethod.HEAD) {
            response = new HttpHeadResponseDecorator(response);
        }
        if (isDebugEnabled) {
            return httpHandler.handle(request, response).doOnError(ex -> log.trace("{}Failed to complete: {}", request.getLogPrefix(), ex.getMessage())).doOnSuccess(aVoid -> log.trace("{}Handling completed", request.getLogPrefix()));
        }
        return httpHandler.handle(request, response);
    } catch (URISyntaxException ex) {
        if (isDebugEnabled) {
            log.debug("Failed to get request URI: {}", ex.getMessage());
        }
        reactorResponse.status(HttpResponseStatus.BAD_REQUEST);
        return Mono.empty();
    }
}
Also used : HttpMethod(cn.taketoday.http.HttpMethod) HttpServerRequest(reactor.netty.http.server.HttpServerRequest) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) Assert(cn.taketoday.lang.Assert) HttpLogging(cn.taketoday.http.HttpLogging) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) Logger(cn.taketoday.logging.Logger) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Mono(reactor.core.publisher.Mono) URISyntaxException(java.net.URISyntaxException) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory)

Example 4 with NettyDataBufferFactory

use of cn.taketoday.core.io.buffer.NettyDataBufferFactory in project today-framework by TAKETODAY.

the class AbstractDataBufferAllocatingTests method verifyAllocations.

private void verifyAllocations() {
    if (this.bufferFactory instanceof NettyDataBufferFactory) {
        ByteBufAllocator allocator = ((NettyDataBufferFactory) this.bufferFactory).getByteBufAllocator();
        if (allocator instanceof PooledByteBufAllocator) {
            Instant start = Instant.now();
            while (true) {
                PooledByteBufAllocatorMetric metric = ((PooledByteBufAllocator) allocator).metric();
                long total = getAllocations(metric.directArenas()) + getAllocations(metric.heapArenas());
                if (total == 0) {
                    return;
                }
                if (Instant.now().isBefore(start.plus(Duration.ofSeconds(5)))) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException ex) {
                    // ignore
                    }
                    continue;
                }
                assertThat(total).as("ByteBuf Leak: " + total + " unreleased allocations").isEqualTo(0);
            }
        }
    }
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Instant(java.time.Instant) PooledByteBufAllocatorMetric(io.netty.buffer.PooledByteBufAllocatorMetric) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 5 with NettyDataBufferFactory

use of cn.taketoday.core.io.buffer.NettyDataBufferFactory in project today-infrastructure by TAKETODAY.

the class ReactorHttpHandlerAdapter method apply.

@Override
public Mono<Void> apply(HttpServerRequest reactorRequest, HttpServerResponse reactorResponse) {
    NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(reactorResponse.alloc());
    try {
        ReactorServerHttpRequest request = new ReactorServerHttpRequest(reactorRequest, bufferFactory);
        ServerHttpResponse response = new ReactorServerHttpResponse(reactorResponse, bufferFactory);
        if (request.getMethod() == HttpMethod.HEAD) {
            response = new HttpHeadResponseDecorator(response);
        }
        return this.httpHandler.handle(request, response).doOnError(ex -> logger.trace("{}Failed to complete: {}", request.getLogPrefix(), ex.getMessage())).doOnSuccess(aVoid -> logger.trace("{}Handling completed", request.getLogPrefix()));
    } catch (URISyntaxException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to get request URI: {}", ex.getMessage());
        }
        reactorResponse.status(HttpResponseStatus.BAD_REQUEST);
        return Mono.empty();
    }
}
Also used : HttpMethod(cn.taketoday.http.HttpMethod) HttpServerRequest(reactor.netty.http.server.HttpServerRequest) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) Assert(cn.taketoday.lang.Assert) HttpLogging(cn.taketoday.http.HttpLogging) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) Logger(cn.taketoday.logging.Logger) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Mono(reactor.core.publisher.Mono) URISyntaxException(java.net.URISyntaxException) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory)

Aggregations

NettyDataBufferFactory (cn.taketoday.core.io.buffer.NettyDataBufferFactory)6 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)4 Mono (reactor.core.publisher.Mono)3 MultiValueMap (cn.taketoday.core.MultiValueMap)2 TypeReference (cn.taketoday.core.TypeReference)2 ByteBufferDecoder (cn.taketoday.core.codec.ByteBufferDecoder)2 StringDecoder (cn.taketoday.core.codec.StringDecoder)2 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)2 DefaultDataBuffer (cn.taketoday.core.io.buffer.DefaultDataBuffer)2 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)2 NettyDataBuffer (cn.taketoday.core.io.buffer.NettyDataBuffer)2 HttpLogging (cn.taketoday.http.HttpLogging)2 HttpMethod (cn.taketoday.http.HttpMethod)2 HttpStatus (cn.taketoday.http.HttpStatus)2 MediaType (cn.taketoday.http.MediaType)2 ReactiveHttpInputMessage (cn.taketoday.http.ReactiveHttpInputMessage)2 DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)2 FormHttpMessageReader (cn.taketoday.http.codec.FormHttpMessageReader)2 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)2 JSON_VIEW_HINT (cn.taketoday.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT)2