reactor / reactor-core

Non-Blocking Reactive Foundation for the JVM
http://projectreactor.io
Apache License 2.0
4.91k stars 1.19k forks source link

API for graceful disposal in Disposable #3173

Open chemicL opened 2 years ago

chemicL commented 2 years ago

Motivation

Disposable interface defines the void dispose() method, which is of eager nature and in most cases assumes non graceful, forceful release of underlying resources. There are cases where such an attempt is not desired. Other projects, like reactor-netty, or rsocket-java, have come up with their own abstractions for disposing resources related to connection and request management. In reactor-core, it was also necessary to support graceful disposal of implementations of Scheduler interface (#3089).

Desired solution

It looks that from the existing implementations, the following properties are desired:

To list some examples:

reactor-netty in DisposableChannel:

reactor-netty in LoopResources:

rsocket-java uses the DisposableChannel from reactor-netty with its own Closeable interface:

I would argue it's possible to unify these efforts using following methods:

The current Disposable already has

Together, they would form a base for extension using concepts specific to a particular use case, e.g. the quietPeriod and deadline in case of reactor-netty, could be expressed as Mono<Void> disposeGracefully(Duration deadline, Duration quietPeriod).

Considered alternatives

One alternative would be to assume different use cases have different needs and the unification effort is pointless. However, without a common denominator, the existing APIs differ even within the same project, potentially leading to inconsistencies and lack of easy-to-predict behaviors.

chemicL commented 1 year ago

Scheduler restarts make it harder to implement having onDispose(). We should consider imposing a contract there to disallow restarts.

chemicL commented 1 year ago

The deprecation of restarts is already happening via deprecation of the start() method and introduction of init() via https://github.com/reactor/reactor-core/issues/3202