I have a Flowable that receives filesystem events (e.g: file updated, using inotify), buffers the files while the filesystem edits complete, and then hands these events off for processing.
This downstream processing, which occurs in different threads, can potentially update the files that are updated. So that creates new filesystem events.
fileUpdateFlowable <- Flowable<FileEvent>
.onBackPressureDrop(...)
.flatMap(fileEvent ->
Flowable.just(fileEvent)
.map(fileEvent -> {
if(fileIsStable) return file;
else throw new UnstableFileException();
})
.retryWhen(f -> {
f.flatMap(error ->
if(error instanceof UnstableFileException) return Flowable.timer(delay);
else throw error;
)
})
How can I make the "processing" respect the state at the top of the Flowable? The buffering of files means that I can run into backpressure if:
- There are too many threads processing downstream.
- The threads happen to edit a lot of files.
- The types of file edits happen to create more filesystem events (this depends on what is done to the files).
I'm looking for a way for the processing threads to delay writing while there are a lot of filesystem events backed up.
I looked into running the edits within the same Scheduler
as the filesystem event processing, however the way the buffer currently works is to retryWhen
with a Flowable.timer
- so there is no obvious way to delay this work.