Chromium Code Reviews| 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(); |
| } |
| } |