pool 1.4.0

  • Readme
  • Changelog
  • Installing
  • 89

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.

// 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<String> 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.

class PooledFile implements RandomAccessFile {
  final RandomAccessFile _file;
  final PoolResource _resource;

  static Future<PooledFile> 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<RandomAccessFile> close() {
    return _file.close.then((_) {
      return this;

1.4.0 #

  • Add forEach to Pool to support efficient async processing of an Iterable.

  • Throw ArgumentError if poolSize <= 0

1.3.6 #

  • Set max SDK version to <3.0.0, and adjust other dependencies.

1.3.5 #

  • Updated SDK version to 2.0.0-dev.17.0

1.3.4 #

  • Modify code to eliminate Future flattening.

1.3.3 #

  • Declare support for async 2.0.0.

1.3.2 #

  • Update to make the code work with strong-mode clean Zone API.

  • Required minimum SDK of 1.23.0.

1.3.1 #

  • Fix the type annotation of Pool.withResource() to indicate that it takes () -> FutureOr<T>.

1.3.0 #

  • Add a Pool.done getter that returns the same future returned by Pool.close().

1.2.4 #

  • Fix a strong-mode error.

1.2.3 #

  • Fix a bug in which Pool.withResource() could throw a StateError when called immediately before closing the pool.

1.2.2 #

  • Fix strong mode warnings and add generic method annotations.

1.2.1 #

  • Internal changes only.

1.2.0 #

  • Add Pool.close(), which forbids new resource requests and releases all releasable resources.

1.1.0 #

  • Add PoolResource.allowRelease(), which allows a resource to indicate that it can be released without forcing it to deallocate immediately.

1.0.2 #

  • Fixed the homepage.

1.0.1 #

  • A TimeoutException is now correctly thrown if the pool detects a deadlock.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  pool: ^1.4.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:pool/pool.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jun 5, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Maintenance suggestions

Package is getting outdated. (-40.55 points)

The package was last published 73 weeks ago.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and pool.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
async >=1.4.0 <3.0.0 2.4.1
stack_trace >=0.9.2 <2.0.0 1.9.3
Transitive dependencies
collection 1.14.12
path 1.7.0
Dev dependencies
fake_async ^1.0.0
pedantic ^1.4.0
test ^1.0.0