https://github.com/dart-lang/pool
A class for managing a finite pool of resources.
https://github.com/dart-lang/pool
Last synced: 5 months ago
JSON representation
A class for managing a finite pool of resources.
- Host: GitHub
- URL: https://github.com/dart-lang/pool
- Owner: dart-lang
- License: bsd-3-clause
- Created: 2014-12-18T00:23:46.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-12-11T09:20:14.000Z (6 months ago)
- Last Synced: 2025-01-12T07:06:14.703Z (5 months ago)
- Language: Dart
- Homepage: https://pub.dev/packages/pool
- Size: 143 KB
- Stars: 50
- Watchers: 37
- Forks: 16
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
> [!IMPORTANT]
> This repo has moved to https://github.com/dart-lang/tools/tree/main/pkgs/pool[](https://github.com/dart-lang/pool/actions/workflows/ci.yml)
[](https://pub.dev/packages/pool)
[](https://pub.dev/packages/pool/publisher)The pool package exposes a `Pool` class which makes it easy to manage a limited
pool of resources.The easiest way to use a pool is by calling `withResource`. This runs a callback
and returns its result, but only once there aren't too many other callbacks
currently running.```dart
// Create a Pool that will only allocate 10 resources at once. After 30 seconds
// of inactivity with all resources checked out, the pool will throw an error.
final pool = new Pool(10, timeout: new Duration(seconds: 30));Future readFile(String path) {
// Since the call to [File.readAsString] is within [withResource], no more
// than ten files will be open at once.
return pool.withResource(() => new File(path).readAsString());
}
```For more fine-grained control, the user can also explicitly request generic
`PoolResource` objects that can later be released back into the pool. This is
what `withResource` does under the covers: requests a resource, then releases it
once the callback completes.`Pool` ensures that only a limited number of resources are allocated at once.
It's the caller's responsibility to ensure that the corresponding physical
resource is only consumed when a `PoolResource` is allocated.```dart
class PooledFile implements RandomAccessFile {
final RandomAccessFile _file;
final PoolResource _resource;static Future open(String path) {
return pool.request().then((resource) {
return new File(path).open().then((file) {
return new PooledFile._(file, resource);
});
});
}PooledFile(this._file, this._resource);
// ...
Future close() {
return _file.close.then((_) {
_resource.release();
return this;
});
}
}
```