I'm working in a pet project where I'll serve large files (gif animated files arround 2mg each)
I'm using groovy and Micronaut because it is really very easy to create this kind of application
First version of the controller was very simple:
@Controller('/files')
FileController{
@Get(value = "/{file}", produces = MediaType.IMAGE_GIF)
byte[]image(@PathVariable String file) {
new File(file).bytes
}
}
Main problem of this approach is you are blocking the request thread meanwhile you read the full file and send all the bytes with the consume of memory it implies
So we want to serve the resource in a "flowable" way using RxJava and sending chuncks of bytes without block the controller
In this way what we return to Micronaut is a Promise who will be subscribe and consume in another thread. When the subscriber is consuming the Promise it will send chunck of bytes calling onNext
many times and onComplete
at the end
Using Flowable I've reduced the memory and the time required to show the gif because as soon the browser receives the first bytes it can start to show them
Top comments (0)