Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Unified Diff: lib/pool.dart

Issue 1415223004: Use the async package's new RestartableTimer class. (Closed) Base URL: git@github.com:dart-lang/pool@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/pool.dart
diff --git a/lib/pool.dart b/lib/pool.dart
index 59b949e0eef0da58ef4d49844e205a46c67dc325..ef38614184badf457535678f6c00cc4e8f7fdc3b 100644
--- a/lib/pool.dart
+++ b/lib/pool.dart
@@ -46,12 +46,14 @@ class Pool {
/// The timeout timer.
///
- /// If [_timeout] isn't null, this timer is set as soon as the resource limit
- /// is reached and is reset every time an resource is released or a new
- /// resource is requested. If it fires, that indicates that the caller became
- /// deadlocked, likely due to files waiting for additional files to be read
- /// before they could be closed.
- Timer _timer;
+ /// This timer is canceled as long as the pool is below the resource limit.
+ /// It's reset once the resource limit is reached and again every time an
+ /// resource is released or a new resource is requested. If it fires, that
+ /// indicates that the caller became deadlocked, likely due to files waiting
+ /// for additional files to be read before they could be closed.
+ ///
+ /// This is `null` if this pool shouldn't time out.
+ RestartableTimer _timer;
/// The amount of time to wait before timing out the pending resources.
final Duration _timeout;
@@ -72,7 +74,13 @@ class Pool {
/// all pending [request] futures will throw a [TimeoutException]. This is
/// intended to avoid deadlocks.
Pool(this._maxAllocatedResources, {Duration timeout})
- : _timeout = timeout;
+ : _timeout = timeout {
+ if (timeout != null) {
+ // Start the timer canceled since we only want to start counting down once
+ // we've run out of available resources.
+ _timer = new RestartableTimer(timeout, _onTimeout)..cancel();
Bob Nystrom 2015/10/28 21:40:27 "cancel" is a weird name for this. To me, it impli
nweiz 2015/10/28 21:47:36 It's an inherited name from Timer. I wanted to kee
+ }
+ }
/// Request a [PoolResource].
///
@@ -190,11 +198,12 @@ class Pool {
/// A resource has been requested, allocated, or released.
void _resetTimer() {
- if (_timer != null) _timer.cancel();
- if (_timeout == null || _requestedResources.isEmpty) {
- _timer = null;
+ if (_timer == null) return;
+
+ if (_requestedResources.isEmpty) {
+ _timer.cancel();
} else {
- _timer = new Timer(_timeout, _onTimeout);
+ _timer.reset();
}
}
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698