OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library barback.pool; | 5 library pool; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
11 | 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 | 12 /// Manages an abstract pool of resources with a limit on how many may be in use |
16 /// at once. | 13 /// at once. |
17 /// | 14 /// |
18 /// When a resource is needed, the user should call [request]. When the returned | 15 /// 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 | 16 /// future completes with a [PoolResource], the resource may be allocated. Once |
20 /// the resource has been released, the user should call [PoolResource.release]. | 17 /// 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 | 18 /// The pool will ensure that only a certain number of [PoolResource]s may be |
22 /// allocated at once. | 19 /// allocated at once. |
23 class Pool { | 20 class Pool { |
24 /// Completers for requests beyond the first [_maxAllocatedResources]. | 21 /// Completers for requests beyond the first [_maxAllocatedResources]. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 return completer.future; | 66 return completer.future; |
70 } | 67 } |
71 } | 68 } |
72 | 69 |
73 /// Requests a resource for the duration of [callback], which may return a | 70 /// Requests a resource for the duration of [callback], which may return a |
74 /// Future. | 71 /// Future. |
75 /// | 72 /// |
76 /// The return value of [callback] is piped to the returned Future. | 73 /// The return value of [callback] is piped to the returned Future. |
77 Future withResource(callback()) { | 74 Future withResource(callback()) { |
78 return request().then((resource) => | 75 return request().then((resource) => |
79 syncFuture(callback).whenComplete(resource.release)); | 76 Chain.track(new Future.sync(callback)).whenComplete(resource.release)); |
80 } | 77 } |
81 | 78 |
82 /// If there are any pending requests, this will fire the oldest one. | 79 /// If there are any pending requests, this will fire the oldest one. |
83 void _onResourceReleased() { | 80 void _onResourceReleased() { |
84 if (_requestedResources.isEmpty) { | 81 if (_requestedResources.isEmpty) { |
85 _allocatedResources--; | 82 _allocatedResources--; |
86 if (_timer != null) { | 83 if (_timer != null) { |
87 _timer.cancel(); | 84 _timer.cancel(); |
88 _timer = null; | 85 _timer = null; |
89 } | 86 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 | 128 |
132 /// Tells the parent [Pool] that the resource associated with this resource is | 129 /// Tells the parent [Pool] that the resource associated with this resource is |
133 /// no longer allocated, and that a new [PoolResource] may be allocated. | 130 /// no longer allocated, and that a new [PoolResource] may be allocated. |
134 void release() { | 131 void release() { |
135 if (_released) { | 132 if (_released) { |
136 throw new StateError("A PoolResource may only be released once."); | 133 throw new StateError("A PoolResource may only be released once."); |
137 } | 134 } |
138 _released = true; | 135 _released = true; |
139 _pool._onResourceReleased(); | 136 _pool._onResourceReleased(); |
140 } | 137 } |
141 } | 138 } |
| 139 |
OLD | NEW |