Skip to content

Commit

Permalink
Merge pull request #1259 from dart-lang/merge-pool-package
Browse files Browse the repository at this point in the history
Merge `package:pool`
  • Loading branch information
mosuem authored Dec 11, 2024
2 parents e8f7d66 + 01ef2b8 commit 7cbd039
Show file tree
Hide file tree
Showing 13 changed files with 1,485 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/ISSUE_TEMPLATE/pool.md
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"
---
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
- changed-files:
- any-glob-to-any-file: 'pkgs/package_config/**'

'package:pool':
- changed-files:
- any-glob-to-any-file: 'pkgs/pool/**'

'package:source_map_stack_trace':
- changed-files:
- any-glob-to-any-file: 'pkgs/source_map_stack_trace/**'
Expand Down
78 changes: 78 additions & 0 deletions .github/workflows/pool.yaml
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'
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ don't naturally belong to other topic monorepos (like
| [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) |
| [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) |
| [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) |
| [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) |
| [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) |
| [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) |

Expand Down
5 changes: 5 additions & 0 deletions pkgs/pool/.gitignore
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
105 changes: 105 additions & 0 deletions pkgs/pool/CHANGELOG.md
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.
27 changes: 27 additions & 0 deletions pkgs/pool/LICENSE
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.
57 changes: 57 additions & 0 deletions pkgs/pool/README.md
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;
});
}
}
```
5 changes: 5 additions & 0 deletions pkgs/pool/analysis_options.yaml
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
55 changes: 55 additions & 0 deletions pkgs/pool/benchmark/for_each_benchmark.dart
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);
}
}
}
Loading

0 comments on commit 7cbd039

Please sign in to comment.