Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform a zero-copy upload and download with WebClient?

Can you perform a zero-copy upload and download with Spring 5 WebFlux using org.springframework.web.reactive.function.client.WebClient?

like image 719
CoryO Avatar asked Oct 14 '17 01:10

CoryO


1 Answers

You're right, zero-copy is supported for now when posting data from a File-based Resource.

So the following looks right:

client.post()
      .body(BodyInserters.fromResource(new FileSystemResource(new File("file.txt"))));

Now for the reading part, zero-copy is not supported on the reading side right now in Spring Framework; you could create an enhancement issue on jira.spring.io for that.

Your code sample should look like:

Flux<DataBuffer> incoming = client.post()
      .retrieve().bodyToFlux(DataBuffer.class);
Mono<Void> writeOperation = DataBufferUtils.write(incoming, channel)
      .map(DataBufferUtils::release)
      .then();
// subscribe to the returned value, which will complete when writing is done

Unfortunately, reading data to DataBuffer won't do zero-copy as data will be copied in memory. I don't think zero-copy is supported right know on the reading side, so this could be an enhancement request on https://jira.spring.io.

like image 65
Brian Clozel Avatar answered Nov 14 '22 22:11

Brian Clozel