You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make WebFlux multipart support fully Reactive [SPR-17122] #21659
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.
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.
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
publicstaticFlux<DataBuffer> split(Publisher<DataBuffer> dataBuffers, byte[] delimiter,
// a lot of codereturnFlux.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
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
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.
Activity
spring-projects-issues commentedon Aug 6, 2018
Rossen Stoyanchev commented
There are two styles of usage, one via
Flux<Part>with@RequestBodyand other is through aMultiValueMap<String, Part>which could happen through data binding, or via@RequestPart.For reading via
Fluxusing memory with back pressure makes sense. TheMultiValueMapusage however could put put a burden on memory if content is over a certain size. We could recommend usingFluxfor 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.prog110 commentedon Jun 22, 2019
Can someone correct me if I am wrong
I see that the current implementation uses
bufferUntiloperator which will buffer all the data in memory till the end of each part. So, even if I useFlux<Part>variation, we are dealing with a faulty implementationThis 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#readcallsDataBufferUtils.split(body, boundaryNeedle)and the implementation of
splitisSince 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 memoryHere are my two cents on how the implementation might needs to be changed to
splitshould be returnFlux<Flux<DataBuffer>>instead ofFlux<DataBuffer>& each of the element in the top levelFluxcorresponds to thepartfrom http requestThis way, we don't buffer the data in memory & allow the backpressure be properly cascaded
Please let me know your thoughts
prog110 commentedon Jun 22, 2019
Can we use
concatMapinstead offlatMapinsidesplitmethod as we care about the orderSince the mapping inside
flatMapin this specific case is synchronous, we are not seeing the issue with out of order completion of inner publishers, but usingconcatMapmakes both the behaviour & intent clearI am referring to
prog110 commentedon Jun 22, 2019
Have deleted few comments & corrected few comments above as they were wrong/misleading in earlier versions
4 remaining items
Remove DefaultMultipartMessageReader
poutsma commentedon Jul 11, 2019
The refactoring of the
DefaultMultipartParserproved to be too ambitious for the 5.2 release deadline, so we are pushing this issue back to the next release.mdzhigarov commentedon Nov 20, 2019
Any update on this? Are there plans to move away from Synchronoss nio-multipart in one of the next releases?
poutsma commentedon Nov 20, 2019
(Re)introduce DefaultMultipartMessageReader
Introduce DefaultMultipartMessageReader
(Re)introduce DefaultMultipartMessageReader