OLD | NEW |
---|---|
(Empty) | |
1 import 'dart:async'; | |
2 import 'dart:collection'; | |
3 | |
4 import 'package:stack_trace/stack_trace.dart'; | |
5 | |
6 /// Manages an abstract pool of resources with a limit on how many may be in use | |
7 /// at once. | |
8 /// | |
9 /// When a resource is needed, the user should call [checkOut]. When the | |
10 /// returned future completes with a [PoolResource], the resource may be | |
11 /// allocated. Once the resource has been released, the user should call | |
12 /// [PoolResource.release]. The pool will ensure that only a certain number of | |
13 /// [PoolResource]s may be checked out at once. | |
14 class Pool { | |
15 /// Completers for checkouts beyond the first [_maxCheckedOutResources]. | |
16 /// | |
17 /// When an item is released, the next element of [_pendingResources] will be | |
18 /// completed. | |
19 final _pendingResources = new Queue<Completer<PoolResource>>(); | |
20 | |
21 /// The maximum number of resources that may be checked out at once. | |
22 final int _maxCheckedOutResources; | |
23 | |
24 /// The number of resources that are currently checked out. | |
25 int _checkedOutResources = 0; | |
26 | |
27 /// The timeout timer. | |
28 /// | |
29 /// If [_timeout] isn't null, this timer is set as soon as the resource limit | |
30 /// is reached and is reset every time an resource is released or a new | |
31 /// resource is requested. If it fires, that indicates that the caller became | |
32 /// deadlocked, likely due to files waiting for additional files to be read | |
33 /// before they could be closed. | |
34 Timer _timer; | |
35 | |
36 /// The amount of time to wait before timing out the pending resources. | |
37 Duration _timeout; | |
38 | |
39 /// Creates a new pool with the given limit on how many resources may be | |
40 /// checked out at once. | |
41 /// | |
42 /// If [timeout] is passed, then if that much time passes without any activity | |
43 /// all pending [checkOut] futures will throw an exception. This is indented | |
44 /// to avoid deadlocks. | |
45 Pool(this._maxCheckedOutResources, {Duration timeout}) | |
46 : _timeout = timeout; | |
47 | |
48 /// Check out a [PoolResource]. | |
49 /// | |
50 /// If the maximum number of resources is already checked out, this will delay | |
51 /// until one of them is released. | |
52 Future<PoolResource> checkOut() { | |
53 if (_checkedOutResources < _maxCheckedOutResources) { | |
54 _checkedOutResources++; | |
55 return new Future.value(new PoolResource._(this)); | |
56 } else { | |
57 var completer = new Completer<PoolResource>(); | |
58 _pendingResources.add(completer); | |
59 _heartbeat(); | |
60 return completer.future; | |
61 } | |
62 } | |
63 | |
64 /// If there are any pending checkouts, this will fire the oldest one. | |
65 void _onResourceReleased() { | |
66 if (_pendingResources.isEmpty) { | |
67 _checkedOutResources--; | |
68 if (_timer != null) { | |
69 _timer.cancel(); | |
70 _timer = null; | |
71 } | |
72 return; | |
73 } | |
74 | |
75 _heartbeat(); | |
76 var pending = _pendingResources.removeFirst(); | |
77 pending.complete(new PoolResource._(this)); | |
78 } | |
79 | |
80 /// Indicates that some external action has occurred and the timer should be | |
81 /// restarted. | |
82 void _heartbeat() { | |
83 if (_timer != null) _timer.cancel(); | |
84 if (_timeout == null) { | |
85 _timer = null; | |
86 } else { | |
87 _timer = new Timer(_timeout, _onTimeout); | |
88 } | |
89 } | |
90 | |
91 /// Handles [_timer] timing out by causing all pending resource completers to | |
92 /// emit exceptions. | |
93 void _onTimeout() { | |
94 for (var completer in _pendingResources) { | |
95 completer.completeException("Pool deadlock: all resources have been " | |
96 "checked out for too long.", new Trace.current().vmTrace); | |
97 } | |
98 _pendingResources.clear(); | |
99 _timer = null; | |
100 } | |
101 } | |
102 | |
103 /// A member of a [Pool]. | |
104 /// | |
105 /// A [PoolResource] is a token that indicates that a resource is allocated. Whe n | |
Alan Knight
2013/10/23 00:24:06
80 chars
nweiz
2013/10/23 04:11:25
Done.
nweiz
2013/10/23 04:11:25
Done.
| |
106 /// the associated resource is released, the user should call [release]. | |
107 class PoolResource { | |
108 final Pool _pool; | |
109 | |
110 /// Whether [this] has been released yet. | |
111 bool _released = false; | |
112 | |
113 PoolResource._(this._pool); | |
114 | |
115 /// Tells the parent [Pool] that the resource associated with this resource is | |
116 /// no longer allocated, and that a new [PoolResource] may be checked out. | |
117 void release() { | |
118 if (_released) { | |
119 throw new StateError("A PoolResource may only be released once."); | |
120 } | |
121 _released = true; | |
122 _pool._onResourceReleased(); | |
123 } | |
124 } | |
OLD | NEW |