Skip to content

Commit 1cc1f52

Browse files
committed
Merge package:pool into the tools monorepo
2 parents faf2b86 + ecc44a1 commit 1cc1f52

File tree

13 files changed

+1528
-0
lines changed

13 files changed

+1528
-0
lines changed

pkgs/pool/.github/dependabot.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Dependabot configuration file.
2+
# See https://docs.github.com/en/code-security/dependabot/dependabot-version-updates
3+
version: 2
4+
5+
updates:
6+
- package-ecosystem: github-actions
7+
directory: /
8+
schedule:
9+
interval: monthly
10+
labels:
11+
- autosubmit
12+
groups:
13+
github-actions:
14+
patterns:
15+
- "*"

pkgs/pool/.github/workflows/ci.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: CI
2+
3+
on:
4+
# Run on PRs and pushes to the default branch.
5+
push:
6+
branches: [ master ]
7+
pull_request:
8+
branches: [ master ]
9+
schedule:
10+
- cron: "0 0 * * 0"
11+
12+
env:
13+
PUB_ENVIRONMENT: bot.github
14+
15+
jobs:
16+
# Check code formatting and static analysis on a single OS (linux)
17+
# against Dart dev.
18+
analyze:
19+
runs-on: ubuntu-latest
20+
strategy:
21+
fail-fast: false
22+
matrix:
23+
sdk: [dev]
24+
steps:
25+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
26+
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
27+
with:
28+
sdk: ${{ matrix.sdk }}
29+
- id: install
30+
name: Install dependencies
31+
run: dart pub get
32+
- name: Check formatting
33+
run: dart format --output=none --set-exit-if-changed .
34+
if: always() && steps.install.outcome == 'success'
35+
- name: Analyze code
36+
run: dart analyze --fatal-infos
37+
if: always() && steps.install.outcome == 'success'
38+
39+
# Run tests on a matrix consisting of two dimensions:
40+
# 1. OS: ubuntu-latest, (macos-latest, windows-latest)
41+
# 2. release channel: dev
42+
test:
43+
needs: analyze
44+
runs-on: ${{ matrix.os }}
45+
strategy:
46+
fail-fast: false
47+
matrix:
48+
# Add macos-latest and/or windows-latest if relevant for this package.
49+
os: [ubuntu-latest]
50+
sdk: [3.4, dev]
51+
steps:
52+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
53+
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
54+
with:
55+
sdk: ${{ matrix.sdk }}
56+
- id: install
57+
name: Install dependencies
58+
run: dart pub get
59+
- name: Run VM tests
60+
run: dart test --platform vm
61+
if: always() && steps.install.outcome == 'success'
62+
- name: Run Chrome tests
63+
run: dart test --platform chrome
64+
if: always() && steps.install.outcome == 'success'
65+
- name: Run Chrome tests - wasm
66+
run: dart test --platform chrome -c dart2wasm
67+
if: always() && steps.install.outcome == 'success' && matrix.sdk == 'dev'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# A workflow to close issues where the author hasn't responded to a request for
2+
# more information; see https://github.com/actions/stale.
3+
4+
name: No Response
5+
6+
# Run as a daily cron.
7+
on:
8+
schedule:
9+
# Every day at 8am
10+
- cron: '0 8 * * *'
11+
12+
# All permissions not specified are set to 'none'.
13+
permissions:
14+
issues: write
15+
pull-requests: write
16+
17+
jobs:
18+
no-response:
19+
runs-on: ubuntu-latest
20+
if: ${{ github.repository_owner == 'dart-lang' }}
21+
steps:
22+
- uses: actions/stale@28ca1036281a5e5922ead5184a1bbf96e5fc984e
23+
with:
24+
# Don't automatically mark inactive issues+PRs as stale.
25+
days-before-stale: -1
26+
# Close needs-info issues and PRs after 14 days of inactivity.
27+
days-before-close: 14
28+
stale-issue-label: "needs-info"
29+
close-issue-message: >
30+
Without additional information we're not able to resolve this issue.
31+
Feel free to add more info or respond to any questions above and we
32+
can reopen the case. Thanks for your contribution!
33+
stale-pr-label: "needs-info"
34+
close-pr-message: >
35+
Without additional information we're not able to resolve this PR.
36+
Feel free to add more info or respond to any questions above.
37+
Thanks for your contribution!
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# A CI configuration to auto-publish pub packages.
2+
3+
name: Publish
4+
5+
on:
6+
pull_request:
7+
branches: [ master ]
8+
push:
9+
tags: [ 'v[0-9]+.[0-9]+.[0-9]+' ]
10+
11+
jobs:
12+
publish:
13+
if: ${{ github.repository_owner == 'dart-lang' }}
14+
uses: dart-lang/ecosystem/.github/workflows/publish.yaml@main
15+
permissions:
16+
id-token: write # Required for authentication using OIDC
17+
pull-requests: write # Required for writing the pull request note

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: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
## 1.5.2-wip
2+
3+
* Require Dart 3.4.
4+
5+
## 1.5.1
6+
7+
* Populate the pubspec `repository` field.
8+
9+
## 1.5.0
10+
11+
* Stable release for null safety.
12+
13+
## 1.5.0-nullsafety.3
14+
15+
* Update SDK constraints to `>=2.12.0-0 <3.0.0` based on beta release
16+
guidelines.
17+
18+
## 1.5.0-nullsafety.2
19+
20+
* Allow prerelease versions of the 2.12 sdk.
21+
22+
## 1.5.0-nullsafety.1
23+
24+
* Allow 2.10 stable and 2.11.0 dev SDK versions.
25+
26+
## 1.5.0-nullsafety
27+
28+
* Migrate to null safety.
29+
* `forEach`: Avoid `await null` if the `Stream` is not paused.
30+
Improves trivial benchmark by 40%.
31+
32+
## 1.4.0
33+
34+
* Add `forEach` to `Pool` to support efficient async processing of an
35+
`Iterable`.
36+
37+
* Throw ArgumentError if poolSize <= 0
38+
39+
## 1.3.6
40+
41+
* Set max SDK version to `<3.0.0`, and adjust other dependencies.
42+
43+
## 1.3.5
44+
45+
- Updated SDK version to 2.0.0-dev.17.0
46+
47+
## 1.3.4
48+
49+
* Modify code to eliminate Future flattening.
50+
51+
## 1.3.3
52+
53+
* Declare support for `async` 2.0.0.
54+
55+
## 1.3.2
56+
57+
* Update to make the code work with strong-mode clean Zone API.
58+
59+
* Required minimum SDK of 1.23.0.
60+
61+
## 1.3.1
62+
63+
* Fix the type annotation of `Pool.withResource()` to indicate that it takes
64+
`() -> FutureOr<T>`.
65+
66+
## 1.3.0
67+
68+
* Add a `Pool.done` getter that returns the same future returned by
69+
`Pool.close()`.
70+
71+
## 1.2.4
72+
73+
* Fix a strong-mode error.
74+
75+
## 1.2.3
76+
77+
* Fix a bug in which `Pool.withResource()` could throw a `StateError` when
78+
called immediately before closing the pool.
79+
80+
## 1.2.2
81+
82+
* Fix strong mode warnings and add generic method annotations.
83+
84+
## 1.2.1
85+
86+
* Internal changes only.
87+
88+
## 1.2.0
89+
90+
* Add `Pool.close()`, which forbids new resource requests and releases all
91+
releasable resources.
92+
93+
## 1.1.0
94+
95+
* Add `PoolResource.allowRelease()`, which allows a resource to indicate that it
96+
can be released without forcing it to deallocate immediately.
97+
98+
## 1.0.2
99+
100+
* Fixed the homepage.
101+
102+
## 1.0.1
103+
104+
* 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+
[![Dart CI](https://github.com/dart-lang/pool/actions/workflows/ci.yml/badge.svg)](https://github.com/dart-lang/pool/actions/workflows/ci.yml)
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'package:pool/pool.dart';
2+
3+
void main(List<String> args) async {
4+
var poolSize = args.isEmpty ? 5 : int.parse(args.first);
5+
print('Pool size: $poolSize');
6+
7+
final pool = Pool(poolSize);
8+
final watch = Stopwatch()..start();
9+
final start = DateTime.now();
10+
11+
DateTime? lastLog;
12+
Duration? fastest;
13+
late int fastestIteration;
14+
var i = 1;
15+
16+
void log(bool force) {
17+
var now = DateTime.now();
18+
if (force ||
19+
lastLog == null ||
20+
now.difference(lastLog!) > const Duration(seconds: 1)) {
21+
lastLog = now;
22+
print([
23+
now.difference(start),
24+
i.toString().padLeft(10),
25+
fastestIteration.toString().padLeft(7),
26+
fastest!.inMicroseconds.toString().padLeft(9)
27+
].join(' '));
28+
}
29+
}
30+
31+
print(['Elapsed ', 'Iterations', 'Fastest', 'Time (us)'].join(' '));
32+
33+
for (;; i++) {
34+
watch.reset();
35+
36+
var sum = await pool
37+
.forEach<int, int>(Iterable<int>.generate(100000), (i) => i)
38+
.reduce((a, b) => a + b);
39+
40+
assert(sum == 4999950000, 'was $sum');
41+
42+
var elapsed = watch.elapsed;
43+
if (fastest == null || fastest > elapsed) {
44+
fastest = elapsed;
45+
fastestIteration = i;
46+
log(true);
47+
} else {
48+
log(false);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)