OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, 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 barback.utils.pool; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:collection'; | |
9 | |
10 import 'package:stack_trace/stack_trace.dart'; | |
11 | |
12 import '../utils.dart'; | |
13 | |
14 // TODO(nweiz): put this somewhere that it can be shared between packages. | |
15 /// Manages an abstract pool of resources with a limit on how many may be in use | |
16 /// at once. | |
17 /// | |
18 /// When a resource is needed, the user should call [request]. When the returned | |
19 /// future completes with a [PoolResource], the resource may be allocated. Once | |
20 /// the resource has been released, the user should call [PoolResource.release]. | |
21 /// The pool will ensure that only a certain number of [PoolResource]s may be | |
22 /// allocated at once. | |
23 class Pool { | |
24 /// Completers for requests beyond the first [_maxAllocatedResources]. | |
25 /// | |
26 /// When an item is released, the next element of [_requestedResources] will | |
27 /// be completed. | |
28 final _requestedResources = new Queue<Completer<PoolResource>>(); | |
29 | |
30 /// The maximum number of resources that may be allocated at once. | |
31 final int _maxAllocatedResources; | |
32 | |
33 /// The number of resources that are currently allocated. | |
34 int _allocatedResources = 0; | |
35 | |
36 /// The timeout timer. | |
37 /// | |
38 /// If [_timeout] isn't null, this timer is set as soon as the resource limit | |
39 /// is reached and is reset every time an resource is released or a new | |
40 /// resource is requested. If it fires, that indicates that the caller became | |
41 /// deadlocked, likely due to files waiting for additional files to be read | |
42 /// before they could be closed. | |
43 Timer _timer; | |
44 | |
45 /// The amount of time to wait before timing out the pending resources. | |
46 Duration _timeout; | |
47 | |
48 /// Creates a new pool with the given limit on how many resources may be | |
49 /// allocated at once. | |
50 /// | |
51 /// If [timeout] is passed, then if that much time passes without any activity | |
52 /// all pending [request] futures will throw an exception. This is indented | |
53 /// to avoid deadlocks. | |
54 Pool(this._maxAllocatedResources, {Duration timeout}) | |
55 : _timeout = timeout; | |
56 | |
57 /// Request a [PoolResource]. | |
58 /// | |
59 /// If the maximum number of resources is already allocated, this will delay | |
60 /// until one of them is released. | |
61 Future<PoolResource> request() { | |
62 if (_allocatedResources < _maxAllocatedResources) { | |
63 _allocatedResources++; | |
64 return new Future.value(new PoolResource._(this)); | |
65 } else { | |
66 var completer = new Completer<PoolResource>(); | |
67 _requestedResources.add(completer); | |
68 _resetTimer(); | |
69 return completer.future; | |
70 } | |
71 } | |
72 | |
73 /// Requests a resource for the duration of [callback], which may return a | |
74 /// Future. | |
75 /// | |
76 /// The return value of [callback] is piped to the returned Future. | |
77 Future withResource(callback()) { | |
78 return request().then((resource) => | |
79 syncFuture(callback).whenComplete(resource.release)); | |
80 } | |
81 | |
82 /// If there are any pending requests, this will fire the oldest one. | |
83 void _onResourceReleased() { | |
84 if (_requestedResources.isEmpty) { | |
85 _allocatedResources--; | |
86 if (_timer != null) { | |
87 _timer.cancel(); | |
88 _timer = null; | |
89 } | |
90 return; | |
91 } | |
92 | |
93 _resetTimer(); | |
94 var pending = _requestedResources.removeFirst(); | |
95 pending.complete(new PoolResource._(this)); | |
96 } | |
97 | |
98 /// A resource has been requested, allocated, or released. | |
99 void _resetTimer() { | |
100 if (_timer != null) _timer.cancel(); | |
101 if (_timeout == null) { | |
102 _timer = null; | |
103 } else { | |
104 _timer = new Timer(_timeout, _onTimeout); | |
105 } | |
106 } | |
107 | |
108 /// Handles [_timer] timing out by causing all pending resource completers to | |
109 /// emit exceptions. | |
110 void _onTimeout() { | |
111 for (var completer in _requestedResources) { | |
112 completer.completeError("Pool deadlock: all resources have been " | |
113 "allocated for too long.", 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 } | |
OLD | NEW |