-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1259 from dart-lang/merge-pool-package
Merge `package:pool`
- Loading branch information
Showing
13 changed files
with
1,485 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
name: "package:pool" | ||
about: "Create a bug or file a feature request against package:pool." | ||
labels: "package:pool" | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
name: package:pool | ||
|
||
on: | ||
# Run on PRs and pushes to the default branch. | ||
push: | ||
branches: [ main ] | ||
paths: | ||
- '.github/workflows/pool.yaml' | ||
- 'pkgs/pool/**' | ||
pull_request: | ||
branches: [ main ] | ||
paths: | ||
- '.github/workflows/pool.yaml' | ||
- 'pkgs/pool/**' | ||
schedule: | ||
- cron: "0 0 * * 0" | ||
|
||
env: | ||
PUB_ENVIRONMENT: bot.github | ||
|
||
|
||
defaults: | ||
run: | ||
working-directory: pkgs/pool/ | ||
|
||
jobs: | ||
# Check code formatting and static analysis on a single OS (linux) | ||
# against Dart dev. | ||
analyze: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
sdk: [dev] | ||
steps: | ||
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 | ||
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94 | ||
with: | ||
sdk: ${{ matrix.sdk }} | ||
- id: install | ||
name: Install dependencies | ||
run: dart pub get | ||
- name: Check formatting | ||
run: dart format --output=none --set-exit-if-changed . | ||
if: always() && steps.install.outcome == 'success' | ||
- name: Analyze code | ||
run: dart analyze --fatal-infos | ||
if: always() && steps.install.outcome == 'success' | ||
|
||
# Run tests on a matrix consisting of two dimensions: | ||
# 1. OS: ubuntu-latest, (macos-latest, windows-latest) | ||
# 2. release channel: dev | ||
test: | ||
needs: analyze | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
# Add macos-latest and/or windows-latest if relevant for this package. | ||
os: [ubuntu-latest] | ||
sdk: [3.4, dev] | ||
steps: | ||
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 | ||
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94 | ||
with: | ||
sdk: ${{ matrix.sdk }} | ||
- id: install | ||
name: Install dependencies | ||
run: dart pub get | ||
- name: Run VM tests | ||
run: dart test --platform vm | ||
if: always() && steps.install.outcome == 'success' | ||
- name: Run Chrome tests | ||
run: dart test --platform chrome | ||
if: always() && steps.install.outcome == 'success' | ||
- name: Run Chrome tests - wasm | ||
run: dart test --platform chrome -c dart2wasm | ||
if: always() && steps.install.outcome == 'success' && matrix.sdk == 'dev' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Don’t commit the following directories created by pub. | ||
.dart_tool/ | ||
.packages | ||
.pub/ | ||
pubspec.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
## 1.5.2-wip | ||
|
||
* Require Dart 3.4. | ||
* Move to `dart-lang/tools` monorepo. | ||
|
||
## 1.5.1 | ||
|
||
* Populate the pubspec `repository` field. | ||
|
||
## 1.5.0 | ||
|
||
* Stable release for null safety. | ||
|
||
## 1.5.0-nullsafety.3 | ||
|
||
* Update SDK constraints to `>=2.12.0-0 <3.0.0` based on beta release | ||
guidelines. | ||
|
||
## 1.5.0-nullsafety.2 | ||
|
||
* Allow prerelease versions of the 2.12 sdk. | ||
|
||
## 1.5.0-nullsafety.1 | ||
|
||
* Allow 2.10 stable and 2.11.0 dev SDK versions. | ||
|
||
## 1.5.0-nullsafety | ||
|
||
* Migrate to null safety. | ||
* `forEach`: Avoid `await null` if the `Stream` is not paused. | ||
Improves trivial benchmark by 40%. | ||
|
||
## 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright 2014, the Dart project authors. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
* Neither the name of Google LLC nor the names of its | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
[![Build Status](https://github.com/dart-lang/tools/actions/workflows/pool.yaml/badge.svg)](https://github.com/dart-lang/tools/actions/workflows/pool.yaml) | ||
[![pub package](https://img.shields.io/pub/v/pool.svg)](https://pub.dev/packages/pool) | ||
[![package publisher](https://img.shields.io/pub/publisher/pool.svg)](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<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. | ||
|
||
```dart | ||
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((_) { | ||
_resource.release(); | ||
return this; | ||
}); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
include: package:dart_flutter_team_lints/analysis_options.yaml | ||
|
||
analyzer: | ||
language: | ||
strict-casts: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:pool/pool.dart'; | ||
|
||
void main(List<String> args) async { | ||
var poolSize = args.isEmpty ? 5 : int.parse(args.first); | ||
print('Pool size: $poolSize'); | ||
|
||
final pool = Pool(poolSize); | ||
final watch = Stopwatch()..start(); | ||
final start = DateTime.now(); | ||
|
||
DateTime? lastLog; | ||
Duration? fastest; | ||
late int fastestIteration; | ||
var i = 1; | ||
|
||
void log(bool force) { | ||
var now = DateTime.now(); | ||
if (force || | ||
lastLog == null || | ||
now.difference(lastLog!) > const Duration(seconds: 1)) { | ||
lastLog = now; | ||
print([ | ||
now.difference(start), | ||
i.toString().padLeft(10), | ||
fastestIteration.toString().padLeft(7), | ||
fastest!.inMicroseconds.toString().padLeft(9) | ||
].join(' ')); | ||
} | ||
} | ||
|
||
print(['Elapsed ', 'Iterations', 'Fastest', 'Time (us)'].join(' ')); | ||
|
||
for (;; i++) { | ||
watch.reset(); | ||
|
||
var sum = await pool | ||
.forEach<int, int>(Iterable<int>.generate(100000), (i) => i) | ||
.reduce((a, b) => a + b); | ||
|
||
assert(sum == 4999950000, 'was $sum'); | ||
|
||
var elapsed = watch.elapsed; | ||
if (fastest == null || fastest > elapsed) { | ||
fastest = elapsed; | ||
fastestIteration = i; | ||
log(true); | ||
} else { | ||
log(false); | ||
} | ||
} | ||
} |
Oops, something went wrong.