Skip to content

Make WebFlux multipart support fully Reactive [SPR-17122] #21659

Closed

Description

@spring-projects-issues

Sébastien Deleuze opened SPR-17122 and commented

Spring Framework 5.0 and 5.1 provides support for reactive multipart leveraging Synchronoss nio-multipart library which comes with several limitations. Spring WebFlux feeds data to the parser, which then provides a callback when the entire content for the part is ready, potentially creating temporary files when the content is too big to avoid consuming too much memory. These limitations mainly comes from the fact that their StreamStorage abstraction is based on InputStream / OutputStream.

As proposed initially by Arjen Poutsma, we should probably write our own reactive multipart implementation to overcome these limitation and provide a fully reactive behavior where the content of the file is Reactive Streams compliant (bytes comes to the user as they are received) in order to give more control to the user. If we provide this, I tend to think that we don't have to create temporary files.


Issue Links:

Activity

  1. spring-projects-issues commented on Aug 6, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    There are two styles of usage, one via Flux<Part> with @RequestBody and other is through a MultiValueMap<String, Part> which could happen through data binding, or via @RequestPart.

    For reading via Flux using memory with back pressure makes sense. The MultiValueMap usage however could put put a burden on memory if content is over a certain size. We could recommend using Flux for reading larger content, and otherwise put a limit on input buffering. However there might still be a case for temp files, e.g. data binding with a named FilePart bound onto a field.

  2. added
    type: enhancement
    in: web
    on Jan 11, 2019
  3. modified the milestones: 5.x Backlog, 5.2 M1 on Jan 30, 2019
  4. self-assigned this
    on Jan 30, 2019
  5. modified the milestones: 5.2 M1, 5.2 M2 on Apr 8, 2019
  6. prog110 commented on Jun 22, 2019

    @prog110

    Can someone correct me if I am wrong

    I see that the current implementation uses bufferUntil operator which will buffer all the data in memory till the end of each part. So, even if I use Flux<Part> variation, we are dealing with a faulty implementation

    This means, if I am uploading a file of size 2GB, a proportional amount of RAM is used up by databuffers, which is a recipe for Out Of Memory errors

    This is what I am referring to, DefaultMultipartMessageReader#read calls DataBufferUtils.split(body, boundaryNeedle)

    and the implementation of split is

    public static Flux<DataBuffer> split(Publisher<DataBuffer> dataBuffers, byte[] delimiter,
      // a lot of code
      return Flux.from(dataBuffers)
        .flatMap(buffer -> endFrameOnDelimiter(buffer, matcher))
        .bufferUntil(buffer -> buffer == END_FRAME)
      // a lot more code
    }

    Since we are doing buffering, we are collecting all buffers into a List<DataBuffer>s. Which further means, users are forced to cache the whole file content (alteast one full part) in RAM before its consumed. What if I as a user dont want to cache/even write temporarily to disk by framework to reduce memory pressure (which adds to the latency), but want to have backpressure (whole point of using reactive streams) & want the framework not to do pre read & cache everything in memory

    Here are my two cents on how the implementation might needs to be changed to

    split should be return Flux<Flux<DataBuffer>> instead of Flux<DataBuffer> & each of the element in the top level Flux corresponds to the part from http request

    This way, we don't buffer the data in memory & allow the backpressure be properly cascaded

    Please let me know your thoughts

  7. prog110 commented on Jun 22, 2019

    @prog110

    Can we use concatMap instead of flatMap inside split method as we care about the order

    Since the mapping inside flatMap in this specific case is synchronous, we are not seeing the issue with out of order completion of inner publishers, but using concatMap makes both the behaviour & intent clear

    I am referring to

    Flux.from(dataBuffers)
      .flatMap(buffer -> endFrameOnDelimiter(buffer, matcher))
  8. prog110 commented on Jun 22, 2019

    @prog110

    Have deleted few comments & corrected few comments above as they were wrong/misleading in earlier versions

  9. 4 remaining items

  10. added a commit that references this issue on Jul 10, 2019
  11. poutsma commented on Jul 11, 2019

    @poutsma
    Contributor

    The refactoring of the DefaultMultipartParser proved to be too ambitious for the 5.2 release deadline, so we are pushing this issue back to the next release.

  12. removed this from the 5.2 M2 milestone on Jul 11, 2019
  13. mdzhigarov commented on Nov 20, 2019

    @mdzhigarov

    Any update on this? Are there plans to move away from Synchronoss nio-multipart in one of the next releases?

  14. poutsma commented on Nov 20, 2019

    @poutsma
    Contributor
  15. added this to the 5.3 M1 milestone on Nov 21, 2019
  16. modified the milestones: 5.3 M1, 5.3 M2 on May 11, 2020
  17. added a commit that references this issue on Jun 3, 2020
    a351733
  18. modified the milestones: 5.3 M2, 5.3 M1 on Jun 22, 2020
  19. added 2 commits that reference this issue on Sep 17, 2026
    fb642ce
    973ee9b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions