Skip to content

Commit 7cbd039

Browse files
authored
Merge pull request #1259 from dart-lang/merge-pool-package
Merge `package:pool`
2 parents e8f7d66 + 01ef2b8 commit 7cbd039

File tree

13 files changed

+1485
-0
lines changed

13 files changed

+1485
-0
lines changed

.github/ISSUE_TEMPLATE/pool.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
name: "package:pool"
3+
about: "Create a bug or file a feature request against package:pool."
4+
labels: "package:pool"
5+
---

.github/labeler.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
- changed-files:
8585
- any-glob-to-any-file: 'pkgs/package_config/**'
8686

87+
'package:pool':
88+
- changed-files:
89+
- any-glob-to-any-file: 'pkgs/pool/**'
90+
8791
'package:source_map_stack_trace':
8892
- changed-files:
8993
- any-glob-to-any-file: 'pkgs/source_map_stack_trace/**'

.github/workflows/pool.yaml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: package:pool
2+
3+
on:
4+
# Run on PRs and pushes to the default branch.
5+
push:
6+
branches: [ main ]
7+
paths:
8+
- '.github/workflows/pool.yaml'
9+
- 'pkgs/pool/**'
10+
pull_request:
11+
branches: [ main ]
12+
paths:
13+
- '.github/workflows/pool.yaml'
14+
- 'pkgs/pool/**'
15+
schedule:
16+
- cron: "0 0 * * 0"
17+
18+
env:
19+
PUB_ENVIRONMENT: bot.github
20+
21+
22+
defaults:
23+
run:
24+
working-directory: pkgs/pool/
25+
26+
jobs:
27+
# Check code formatting and static analysis on a single OS (linux)
28+
# against Dart dev.
29+
analyze:
30+
runs-on: ubuntu-latest
31+
strategy:
32+
fail-fast: false
33+
matrix:
34+
sdk: [dev]
35+
steps:
36+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
37+
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
38+
with:
39+
sdk: ${{ matrix.sdk }}
40+
- id: install
41+
name: Install dependencies
42+
run: dart pub get
43+
- name: Check formatting
44+
run: dart format --output=none --set-exit-if-changed .
45+
if: always() && steps.install.outcome == 'success'
46+
- name: Analyze code
47+
run: dart analyze --fatal-infos
48+
if: always() && steps.install.outcome == 'success'
49+
50+
# Run tests on a matrix consisting of two dimensions:
51+
# 1. OS: ubuntu-latest, (macos-latest, windows-latest)
52+
# 2. release channel: dev
53+
test:
54+
needs: analyze
55+
runs-on: ${{ matrix.os }}
56+
strategy:
57+
fail-fast: false
58+
matrix:
59+
# Add macos-latest and/or windows-latest if relevant for this package.
60+
os: [ubuntu-latest]
61+
sdk: [3.4, dev]
62+
steps:
63+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
64+
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
65+
with:
66+
sdk: ${{ matrix.sdk }}
67+
- id: install
68+
name: Install dependencies
69+
run: dart pub get
70+
- name: Run VM tests
71+
run: dart test --platform vm
72+
if: always() && steps.install.outcome == 'success'
73+
- name: Run Chrome tests
74+
run: dart test --platform chrome
75+
if: always() && steps.install.outcome == 'success'
76+
- name: Run Chrome tests - wasm
77+
run: dart test --platform chrome -c dart2wasm
78+
if: always() && steps.install.outcome == 'success' && matrix.sdk == 'dev'

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ don't naturally belong to other topic monorepos (like
3434
| [mime](pkgs/mime/) | Utilities for handling media (MIME) types, including determining a type from a file extension and file contents. | [![package issues](https://img.shields.io/badge/package:mime-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Amime) | [![pub package](https://img.shields.io/pub/v/mime.svg)](https://pub.dev/packages/mime) |
3535
| [oauth2](pkgs/oauth2/) | A client library for authenticating with a remote service via OAuth2 on behalf of a user, and making authorized HTTP requests with the user's OAuth2 credentials. | [![package issues](https://img.shields.io/badge/package:oauth2-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aoauth2) | [![pub package](https://img.shields.io/pub/v/oauth2.svg)](https://pub.dev/packages/oauth2) |
3636
| [package_config](pkgs/package_config/) | Support for reading and writing Dart Package Configuration files. | [![package issues](https://img.shields.io/badge/package:package_config-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Apackage_config) | [![pub package](https://img.shields.io/pub/v/package_config.svg)](https://pub.dev/packages/package_config) |
37+
| [pool](pkgs/pool/) | Manage a finite pool of resources. Useful for controlling concurrent file system or network requests. | [![package issues](https://img.shields.io/badge/package:pool-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Apool) | [![pub package](https://img.shields.io/pub/v/pool.svg)](https://pub.dev/packages/pool) |
3738
| [source_map_stack_trace](pkgs/source_map_stack_trace/) | A package for applying source maps to stack traces. | [![package issues](https://img.shields.io/badge/package:source_map_stack_trace-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Asource_map_stack_trace) | [![pub package](https://img.shields.io/pub/v/source_map_stack_trace.svg)](https://pub.dev/packages/source_map_stack_trace) |
3839
| [unified_analytics](pkgs/unified_analytics/) | A package for logging analytics for all Dart and Flutter related tooling to Google Analytics. | [![package issues](https://img.shields.io/badge/package:unified_analytics-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aunified_analytics) | [![pub package](https://img.shields.io/pub/v/unified_analytics.svg)](https://pub.dev/packages/unified_analytics) |
3940

pkgs/pool/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Don’t commit the following directories created by pub.
2+
.dart_tool/
3+
.packages
4+
.pub/
5+
pubspec.lock

pkgs/pool/CHANGELOG.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
## 1.5.2-wip
2+
3+
* Require Dart 3.4.
4+
* Move to `dart-lang/tools` monorepo.
5+
6+
## 1.5.1
7+
8+
* Populate the pubspec `repository` field.
9+
10+
## 1.5.0
11+
12+
* Stable release for null safety.
13+
14+
## 1.5.0-nullsafety.3
15+
16+
* Update SDK constraints to `>=2.12.0-0 <3.0.0` based on beta release
17+
guidelines.
18+
19+
## 1.5.0-nullsafety.2
20+
21+
* Allow prerelease versions of the 2.12 sdk.
22+
23+
## 1.5.0-nullsafety.1
24+
25+
* Allow 2.10 stable and 2.11.0 dev SDK versions.
26+
27+
## 1.5.0-nullsafety
28+
29+
* Migrate to null safety.
30+
* `forEach`: Avoid `await null` if the `Stream` is not paused.
31+
Improves trivial benchmark by 40%.
32+
33+
## 1.4.0
34+
35+
* Add `forEach` to `Pool` to support efficient async processing of an
36+
`Iterable`.
37+
38+
* Throw ArgumentError if poolSize <= 0
39+
40+
## 1.3.6
41+
42+
* Set max SDK version to `<3.0.0`, and adjust other dependencies.
43+
44+
## 1.3.5
45+
46+
- Updated SDK version to 2.0.0-dev.17.0
47+
48+
## 1.3.4
49+
50+
* Modify code to eliminate Future flattening.
51+
52+
## 1.3.3
53+
54+
* Declare support for `async` 2.0.0.
55+
56+
## 1.3.2
57+
58+
* Update to make the code work with strong-mode clean Zone API.
59+
60+
* Required minimum SDK of 1.23.0.
61+
62+
## 1.3.1
63+
64+
* Fix the type annotation of `Pool.withResource()` to indicate that it takes
65+
`() -> FutureOr<T>`.
66+
67+
## 1.3.0
68+
69+
* Add a `Pool.done` getter that returns the same future returned by
70+
`Pool.close()`.
71+
72+
## 1.2.4
73+
74+
* Fix a strong-mode error.
75+
76+
## 1.2.3
77+
78+
* Fix a bug in which `Pool.withResource()` could throw a `StateError` when
79+
called immediately before closing the pool.
80+
81+
## 1.2.2
82+
83+
* Fix strong mode warnings and add generic method annotations.
84+
85+
## 1.2.1
86+
87+
* Internal changes only.
88+
89+
## 1.2.0
90+
91+
* Add `Pool.close()`, which forbids new resource requests and releases all
92+
releasable resources.
93+
94+
## 1.1.0
95+
96+
* Add `PoolResource.allowRelease()`, which allows a resource to indicate that it
97+
can be released without forcing it to deallocate immediately.
98+
99+
## 1.0.2
100+
101+
* Fixed the homepage.
102+
103+
## 1.0.1
104+
105+
* A `TimeoutException` is now correctly thrown if the pool detects a deadlock.

pkgs/pool/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2014, the Dart project authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following
11+
disclaimer in the documentation and/or other materials provided
12+
with the distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pkgs/pool/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[![Build Status](https://github.com/dart-lang/tools/actions/workflows/pool.yaml/badge.svg)](https://github.com/dart-lang/tools/actions/workflows/pool.yaml)
2+
[![pub package](https://img.shields.io/pub/v/pool.svg)](https://pub.dev/packages/pool)
3+
[![package publisher](https://img.shields.io/pub/publisher/pool.svg)](https://pub.dev/packages/pool/publisher)
4+
5+
The pool package exposes a `Pool` class which makes it easy to manage a limited
6+
pool of resources.
7+
8+
The easiest way to use a pool is by calling `withResource`. This runs a callback
9+
and returns its result, but only once there aren't too many other callbacks
10+
currently running.
11+
12+
```dart
13+
// Create a Pool that will only allocate 10 resources at once. After 30 seconds
14+
// of inactivity with all resources checked out, the pool will throw an error.
15+
final pool = new Pool(10, timeout: new Duration(seconds: 30));
16+
17+
Future<String> readFile(String path) {
18+
// Since the call to [File.readAsString] is within [withResource], no more
19+
// than ten files will be open at once.
20+
return pool.withResource(() => new File(path).readAsString());
21+
}
22+
```
23+
24+
For more fine-grained control, the user can also explicitly request generic
25+
`PoolResource` objects that can later be released back into the pool. This is
26+
what `withResource` does under the covers: requests a resource, then releases it
27+
once the callback completes.
28+
29+
`Pool` ensures that only a limited number of resources are allocated at once.
30+
It's the caller's responsibility to ensure that the corresponding physical
31+
resource is only consumed when a `PoolResource` is allocated.
32+
33+
```dart
34+
class PooledFile implements RandomAccessFile {
35+
final RandomAccessFile _file;
36+
final PoolResource _resource;
37+
38+
static Future<PooledFile> open(String path) {
39+
return pool.request().then((resource) {
40+
return new File(path).open().then((file) {
41+
return new PooledFile._(file, resource);
42+
});
43+
});
44+
}
45+
46+
PooledFile(this._file, this._resource);
47+
48+
// ...
49+
50+
Future<RandomAccessFile> close() {
51+
return _file.close.then((_) {
52+
_resource.release();
53+
return this;
54+
});
55+
}
56+
}
57+
```

pkgs/pool/analysis_options.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include: package:dart_flutter_team_lints/analysis_options.yaml
2+
3+
analyzer:
4+
language:
5+
strict-casts: true
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:pool/pool.dart';
6+
7+
void main(List<String> args) async {
8+
var poolSize = args.isEmpty ? 5 : int.parse(args.first);
9+
print('Pool size: $poolSize');
10+
11+
final pool = Pool(poolSize);
12+
final watch = Stopwatch()..start();
13+
final start = DateTime.now();
14+
15+
DateTime? lastLog;
16+
Duration? fastest;
17+
late int fastestIteration;
18+
var i = 1;
19+
20+
void log(bool force) {
21+
var now = DateTime.now();
22+
if (force ||
23+
lastLog == null ||
24+
now.difference(lastLog!) > const Duration(seconds: 1)) {
25+
lastLog = now;
26+
print([
27+
now.difference(start),
28+
i.toString().padLeft(10),
29+
fastestIteration.toString().padLeft(7),
30+
fastest!.inMicroseconds.toString().padLeft(9)
31+
].join(' '));
32+
}
33+
}
34+
35+
print(['Elapsed ', 'Iterations', 'Fastest', 'Time (us)'].join(' '));
36+
37+
for (;; i++) {
38+
watch.reset();
39+
40+
var sum = await pool
41+
.forEach<int, int>(Iterable<int>.generate(100000), (i) => i)
42+
.reduce((a, b) => a + b);
43+
44+
assert(sum == 4999950000, 'was $sum');
45+
46+
var elapsed = watch.elapsed;
47+
if (fastest == null || fastest > elapsed) {
48+
fastest = elapsed;
49+
fastestIteration = i;
50+
log(true);
51+
} else {
52+
log(false);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)