Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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.file_pool; | 5 library barback.file_pool; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 import 'package:stack_trace/stack_trace.dart'; | |
| 12 | |
| 11 import 'pool.dart'; | 13 import 'pool.dart'; |
| 12 import 'utils.dart'; | 14 import 'utils.dart'; |
| 13 | 15 |
| 14 /// Manages a pool of files that are opened for reading to cope with maximum | 16 /// Manages a pool of files that are opened for reading to cope with maximum |
| 15 /// file descriptor limits. | 17 /// file descriptor limits. |
| 16 /// | 18 /// |
| 17 /// If a file cannot be opened because too many files are already open, this | 19 /// If a file cannot be opened because too many files are already open, this |
| 18 /// will defer the open until a previously opened file is closed and then try | 20 /// will defer the open until a previously opened file is closed and then try |
| 19 /// again. If this doesn't succeed after a certain amount of time, the open | 21 /// again. If this doesn't succeed after a certain amount of time, the open |
| 20 /// will fail and the original "too many files" exception will be thrown. | 22 /// will fail and the original "too many files" exception will be thrown. |
| 21 class FilePool { | 23 class FilePool { |
| 22 /// The underlying pool. | 24 /// The underlying pool. |
| 23 /// | 25 /// |
| 24 /// The maximum number of allocated descriptors is based on empirical tests | 26 /// The maximum number of allocated descriptors is based on empirical tests |
| 25 /// that indicate that beyond 32, additional file reads don't provide | 27 /// that indicate that beyond 32, additional file reads don't provide |
| 26 /// substantial additional throughput. | 28 /// substantial additional throughput. |
| 27 final Pool _pool = new Pool(32, timeout: new Duration(seconds: 60)); | 29 final Pool _pool = new Pool(32, timeout: new Duration(seconds: 60)); |
| 28 | 30 |
| 29 /// Opens the file at [path] for reading. | 31 /// Opens the file at [path] for reading. |
| 30 /// | 32 /// |
| 31 /// When the returned stream is listened to, if there are too many files | 33 /// When the returned stream is listened to, if there are too many files |
| 32 /// open, this will wait for a previously opened file to be closed and then | 34 /// open, this will wait for a previously opened file to be closed and then |
| 33 /// try again. | 35 /// try again. |
| 34 Stream<List<int>> openRead(String path) { | 36 Stream<List<int>> openRead(String path) { |
| 35 return futureStream(_pool.request().then((resource) { | 37 return futureStream(_pool.request().then((resource) { |
| 36 return new File(path).openRead().transform( | 38 return Chain.track(new File(path).openRead()).transform( |
|
Siggi Cherem (dart-lang)
2013/12/09 18:58:34
I just noticed that this breaks polymer build: thi
| |
| 37 new StreamTransformer.fromHandlers(handleDone: (sink) { | 39 new StreamTransformer.fromHandlers(handleDone: (sink) { |
| 38 sink.close(); | 40 sink.close(); |
| 39 resource.release(); | 41 resource.release(); |
| 40 })); | 42 })); |
| 41 })); | 43 })); |
| 42 } | 44 } |
| 43 | 45 |
| 44 /// Reads [path] as a string using [encoding]. | 46 /// Reads [path] as a string using [encoding]. |
| 45 /// | 47 /// |
| 46 /// If there are too many files open and the read fails, this will wait for | 48 /// If there are too many files open and the read fails, this will wait for |
| 47 /// a previously opened file to be closed and then try again. | 49 /// a previously opened file to be closed and then try again. |
| 48 Future<String> readAsString(String path, Encoding encoding) { | 50 Future<String> readAsString(String path, Encoding encoding) { |
| 49 return _readAsBytes(path).then(encoding.decode); | 51 return _readAsBytes(path).then(encoding.decode); |
| 50 } | 52 } |
| 51 | 53 |
| 52 /// Reads [path] as a list of bytes, using [openRead] to retry if there are | 54 /// Reads [path] as a list of bytes, using [openRead] to retry if there are |
| 53 /// failures. | 55 /// failures. |
| 54 Future<List<int>> _readAsBytes(String path) { | 56 Future<List<int>> _readAsBytes(String path) { |
| 55 var completer = new Completer<List<int>>(); | 57 var completer = new Completer<List<int>>(); |
| 56 var builder = new BytesBuilder(); | 58 var builder = new BytesBuilder(); |
| 57 | 59 |
| 58 openRead(path).listen(builder.add, onDone: () { | 60 openRead(path).listen(builder.add, onDone: () { |
| 59 completer.complete(builder.takeBytes()); | 61 completer.complete(builder.takeBytes()); |
| 60 }, onError: completer.completeError, cancelOnError: true); | 62 }, onError: completer.completeError, cancelOnError: true); |
| 61 | 63 |
| 62 return completer.future; | 64 return completer.future; |
| 63 } | 65 } |
| 64 } | 66 } |
| OLD | NEW |