Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close Open File Handles When Cached #220

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ actor MultiplexedAsyncSequence<Base: AsyncSequence & Sendable>: AsyncSequence wh
typealias Element = Base.Element

private var cachedEntries: [Task<Element?, Error>] = []
private var baseIterator: Base.AsyncIterator
private var baseIterator: Base.AsyncIterator?

struct AsyncIterator: AsyncIteratorProtocol & Sendable {
let base: MultiplexedAsyncSequence
Expand Down Expand Up @@ -206,10 +206,11 @@ actor MultiplexedAsyncSequence<Base: AsyncSequence & Sendable>: AsyncSequence wh
}
}

nonisolated func nextBase(iterator: Base.AsyncIterator) async throws -> (Element?, Base.AsyncIterator) {
/// Return the next base iterator to use along with the current entry, or nil if we've reached the end, so we don't retail the open file handles in our memory caches.
nonisolated func nextBase(iterator: Base.AsyncIterator?) async throws -> (Element?, Base.AsyncIterator?) {
var iteratorCopy = iterator
let nextEntry = try await iteratorCopy.next()
return (nextEntry, iteratorCopy)
let nextEntry = try await iteratorCopy?.next()
return (nextEntry, nextEntry.flatMap { _ in iteratorCopy })
}

nonisolated func makeAsyncIterator() -> AsyncIterator {
Expand Down
Loading