OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library pool; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:collection'; |
| 9 |
| 10 import 'package:stack_trace/stack_trace.dart'; |
| 11 |
| 12 /// Manages an abstract pool of resources with a limit on how many may be in use |
| 13 /// at once. |
| 14 /// |
| 15 /// When a resource is needed, the user should call [request]. When the returned |
| 16 /// future completes with a [PoolResource], the resource may be allocated. Once |
| 17 /// the resource has been released, the user should call [PoolResource.release]. |
| 18 /// The pool will ensure that only a certain number of [PoolResource]s may be |
| 19 /// allocated at once. |
| 20 class Pool { |
| 21 /// Completers for requests beyond the first [_maxAllocatedResources]. |
| 22 /// |
| 23 /// When an item is released, the next element of [_requestedResources] will |
| 24 /// be completed. |
| 25 final _requestedResources = new Queue<Completer<PoolResource>>(); |
| 26 |
| 27 /// The maximum number of resources that may be allocated at once. |
| 28 final int _maxAllocatedResources; |
| 29 |
| 30 /// The number of resources that are currently allocated. |
| 31 int _allocatedResources = 0; |
| 32 |
| 33 /// The timeout timer. |
| 34 /// |
| 35 /// If [_timeout] isn't null, this timer is set as soon as the resource limit |
| 36 /// is reached and is reset every time an resource is released or a new |
| 37 /// resource is requested. If it fires, that indicates that the caller became |
| 38 /// deadlocked, likely due to files waiting for additional files to be read |
| 39 /// before they could be closed. |
| 40 Timer _timer; |
| 41 |
| 42 /// The amount of time to wait before timing out the pending resources. |
| 43 final Duration _timeout; |
| 44 |
| 45 /// Creates a new pool with the given limit on how many resources may be |
| 46 /// allocated at once. |
| 47 /// |
| 48 /// If [timeout] is passed, then if that much time passes without any activity |
| 49 /// all pending [request] futures will throw a [TimeoutException]. This is |
| 50 /// intended to avoid deadlocks. |
| 51 Pool(this._maxAllocatedResources, {Duration timeout}) |
| 52 : _timeout = timeout; |
| 53 |
| 54 /// Request a [PoolResource]. |
| 55 /// |
| 56 /// If the maximum number of resources is already allocated, this will delay |
| 57 /// until one of them is released. |
| 58 Future<PoolResource> request() { |
| 59 if (_allocatedResources < _maxAllocatedResources) { |
| 60 _allocatedResources++; |
| 61 return new Future.value(new PoolResource._(this)); |
| 62 } else { |
| 63 var completer = new Completer<PoolResource>(); |
| 64 _requestedResources.add(completer); |
| 65 _resetTimer(); |
| 66 return completer.future; |
| 67 } |
| 68 } |
| 69 |
| 70 /// Requests a resource for the duration of [callback], which may return a |
| 71 /// Future. |
| 72 /// |
| 73 /// The return value of [callback] is piped to the returned Future. |
| 74 Future withResource(callback()) { |
| 75 return request().then((resource) => |
| 76 Chain.track(new Future.sync(callback)).whenComplete(resource.release)); |
| 77 } |
| 78 |
| 79 /// If there are any pending requests, this will fire the oldest one. |
| 80 void _onResourceReleased() { |
| 81 if (_requestedResources.isEmpty) { |
| 82 _allocatedResources--; |
| 83 if (_timer != null) { |
| 84 _timer.cancel(); |
| 85 _timer = null; |
| 86 } |
| 87 return; |
| 88 } |
| 89 |
| 90 _resetTimer(); |
| 91 var pending = _requestedResources.removeFirst(); |
| 92 pending.complete(new PoolResource._(this)); |
| 93 } |
| 94 |
| 95 /// A resource has been requested, allocated, or released. |
| 96 void _resetTimer() { |
| 97 if (_timer != null) _timer.cancel(); |
| 98 if (_timeout == null) { |
| 99 _timer = null; |
| 100 } else { |
| 101 _timer = new Timer(_timeout, _onTimeout); |
| 102 } |
| 103 } |
| 104 |
| 105 /// Handles [_timer] timing out by causing all pending resource completers to |
| 106 /// emit exceptions. |
| 107 void _onTimeout() { |
| 108 for (var completer in _requestedResources) { |
| 109 completer.completeError( |
| 110 new TimeoutException("Pool deadlock: all resources have been " |
| 111 "allocated for too long.", |
| 112 _timeout), |
| 113 new Chain.current()); |
| 114 } |
| 115 _requestedResources.clear(); |
| 116 _timer = null; |
| 117 } |
| 118 } |
| 119 |
| 120 /// A member of a [Pool]. |
| 121 /// |
| 122 /// A [PoolResource] is a token that indicates that a resource is allocated. |
| 123 /// When the associated resource is released, the user should call [release]. |
| 124 class PoolResource { |
| 125 final Pool _pool; |
| 126 |
| 127 /// Whether [this] has been released yet. |
| 128 bool _released = false; |
| 129 |
| 130 PoolResource._(this._pool); |
| 131 |
| 132 /// Tells the parent [Pool] that the resource associated with this resource is |
| 133 /// no longer allocated, and that a new [PoolResource] may be allocated. |
| 134 void release() { |
| 135 if (_released) { |
| 136 throw new StateError("A PoolResource may only be released once."); |
| 137 } |
| 138 _released = true; |
| 139 _pool._onResourceReleased(); |
| 140 } |
| 141 } |
| 142 |
OLD | NEW |