https://github.com/reactor/reactor-pool
https://github.com/reactor/reactor-pool
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/reactor/reactor-pool
- Owner: reactor
- License: apache-2.0
- Created: 2018-10-18T20:34:53.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2025-04-28T11:22:16.000Z (about 1 year ago)
- Last Synced: 2025-04-28T12:31:33.051Z (about 1 year ago)
- Language: Java
- Size: 1.28 MB
- Stars: 143
- Watchers: 15
- Forks: 35
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Reactor-Pool
[](https://gitter.im/reactor/reactor?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://circleci.com/gh/reactor/reactor-pool)
[](https://codecov.io/gh/reactor/reactor-pool)
The `reactor-pool` project aims at providing a generic object pool to reactive application that:
- exposes a reactive API (`Publisher` input types, `Mono` return types)
- is non-blocking (never blocking a user that makes an attempt to `acquire()` a resource)
- has lazy acquire behavior
For use-cases where granular control of the `release()` is needed, the classic path of `acquire()` is offered, which exposes a `PooledRef` wrapper to the resource. This also allows access to statistics about the resource lifecycle in the pool.
```java
// given Pool pool
Mono> grabResource = pool.acquire();
//no resource is actually requested yet at this point
grabResouce.subscribe();
//now one resource is requested from the pool asynchronously
//Another example, this time synchronously acquiring, immediately followed up by a release:
PooledRef ref = grabResource.block(); //second subscription requests a second resource
ref.release().block(); //release() is also asynchronous and lazy
```
For use-cases where the resource itself can be consumed reactively (exposes a reactive API), a scoped mode of acquisition is offered. `withPoolable`:
- let the consumer declaratively use the resource
- provides a scope / closure in which the resource is acquired, used as instructed and released automatically
- avoids dealing with an indirection (the resource is directly exposed)
```java
//given at DbConnection type and a Pool pool
pool.withPoolable(resource -> resource
//we declare using the connection to create a Statement...
.createStatement()
//...then performing a SELECT query...
.flatMapMany(st -> st.query("SELECT * FROM foo"))
//...then marshalling the rows to JSON
.map(row -> rowToJson(row))
//(all of which need the live resource)
)
//at this point the rest of the steps are outside the scope
//so the resource can be released
.map(json -> sanitize(json));
```
_Licensed under [Apache Software License 2.0](www.apache.org/licenses/LICENSE-2.0)_