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