SoftInstigate / restheart

Open Source Low-code API development framework Featuring ready-to-go Security and MongoDB API
http://softinstigate.github.io/restheart/
GNU Affero General Public License v3.0
792 stars 170 forks source link

Lazy-load request content #507

Closed ujibang closed 1 month ago

ujibang commented 1 month ago

Brief Overview

Up to RESTHeart v7, requests processed by the MongoService are designed to lazy-load their content. This means that the request body is only read when MongoRequest.getContent() is invoked for the first time.

This lazy-loading approach significantly improves performance across various scenarios. For instance, it speeds up the request validation process and ensures that interceptors that don't need to access the content can execute faster.

Our aim is to extend this behavior to all request types.

Rationale

Optimizing request handling can enhance performance in cases where the request content is unnecessary. A common example includes situations where a request is denied due to insufficient permissions.

Detailed Documentation

The ServiceRequest class now features a new abstract method to read and parse the request content:

    /**
     * Parses the content from the exchange and converts it into an instance of the specified type {@code T}.
     *
     * This method retrieves data from the exchange, interprets it according to the expected format, and converts
     * this data into an object of type {@code T}.
     *
     * @return an instance of {@code T} representing the parsed content
     * @throws IOException if an I/O error occurs
     * @throws BadRequestException if the content doesn't conform to the expected format of type {@code T}
     */
    public abstract T parseContent() throws IOException, BadRequestException;

ServiceRequest.parseContent() is called by ServiceRequest.getContent() on its first invocation. The parsed content is then cached and linked to the request, ensuring that any subsequent calls will reuse the already parsed content object.

This approach makes handling request content more efficient by reducing unnecessary parsing and processing overhead.