Description
Brian Clozel opened SPR-16782 and commented
DataBufferUtils
can write DataBuffer
streams to file channels; the current API does leave full control to the developer and the javadoc is pretty clear on the fact that developers are in charge of releasing DataBuffer
instances.
Now let's take an example; we'd like to fetch a large file using the WebClient
and pipe it into a file on disk. An initial approach could be:
Flux<DataBuffer> data = WebClient.create().get()
.uri("http://example.org/largefile.bin")
.retrieve()
.bodyToFlux(DataBuffer.class);
// We'd like to write it to a file on disk
Path file = Files.createTempFile("spring", null);
WritableByteChannel channel = Files.newByteChannel(file, StandardOpenOption.WRITE);
DataBufferUtils.write(data, channel)
// the release consumer releases buffer written on disk, one by one
.subscribe(DataBufferUtils.releaseConsumer(), throwable -> {
// when an error occurs, we don't have access to the current buffer
// nor the following ones: we can't release anything properly.
});
}
As explained in the comments, if an IOException
happens the current DataBuffer
is not released, neither are the ones that might be instantiated already and about to be published to the pipeline.
I think we should document this or assist developers here, as there is no obvious workaround here. DataBufferUtils
is an utils class, so we need to make sure to strike the right balance between doing the right thing automatically and providing flexibility.
Issue Links:
- DataBufferUtils#join could leak buffers in case of error from the source [SPR-17025] #21563 DataBufferUtils#join could leak buffers in case of error from the source
- Review DataBuffer handling code for proper release in case of error or cancellation [SPR-17408] #21941 Review DataBuffer handling code for proper release in case of error or cancellation
Referenced from: commits 1a0522b, 952315c
Backported to: 5.0.10
4 votes, 4 watchers