Skip to content

Commit

Permalink
File handles can use NIOLoopBound
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTim committed Apr 18, 2023
1 parent 0bbf86e commit 89cdbcf
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Sources/Vapor/Environment/Environment+Secret.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension Environment {
return fileIO
.openFile(path: path, eventLoop: eventLoop)
.flatMap { handle, region in
let handleWrapper = FileHandleWrapper(value: handle)
let handleWrapper = NIOLoopBound(handle, eventLoop: eventLoop)
return fileIO
.read(fileRegion: region, allocator: .init(), eventLoop: eventLoop)
.always { _ in try? handleWrapper.value.close() }
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Utilities/DotEnv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public struct DotEnvFile: Sendable {
on eventLoop: EventLoop
) -> EventLoopFuture<DotEnvFile> {
return fileio.openFile(path: path, eventLoop: eventLoop).flatMap { arg -> EventLoopFuture<ByteBuffer> in
let fileHandleWrapper = FileHandleWrapper(value: arg.0)
let fileHandleWrapper = NIOLoopBound(arg.0, eventLoop: eventLoop)
return fileio.read(fileRegion: arg.1, allocator: .init(), eventLoop: eventLoop)
.flatMapThrowing
{ buffer in
Expand Down
12 changes: 2 additions & 10 deletions Sources/Vapor/Utilities/FileIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public struct FileIO: Sendable {
) -> EventLoopFuture<Void> {
do {
let fd = try NIOFileHandle(path: path)
let fdWrapper = FileHandleWrapper(value: fd)
let fdWrapper = NIOLoopBound(fd, eventLoop: self.request.eventLoop)
let done = self.io.readChunked(
fileHandle: fd,
fromOffset: offset,
Expand Down Expand Up @@ -267,7 +267,7 @@ public struct FileIO: Sendable {
public func writeFile(_ buffer: ByteBuffer, at path: String) -> EventLoopFuture<Void> {
do {
let fd = try NIOFileHandle(path: path, mode: .write, flags: .allowFileCreation())
let fdWrapper = FileHandleWrapper(value: fd)
let fdWrapper = NIOLoopBound(fd, eventLoop: self.request.eventLoop)
let done = io.write(fileHandle: fd, buffer: buffer, eventLoop: self.request.eventLoop)
done.whenComplete { _ in
try? fdWrapper.value.close()
Expand Down Expand Up @@ -309,11 +309,3 @@ extension HTTPHeaders.Range.Value {
}
}
}

final class FileHandleWrapper: @unchecked Sendable {
var value: NIOFileHandle

init(value: NIOFileHandle) {
self.value = value
}
}
2 changes: 1 addition & 1 deletion Sources/Vapor/View/PlaintextRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct PlaintextRenderer: ViewRenderer, Sendable {
? name
: self.viewsDirectory + name
return self.fileio.openFile(path: path, eventLoop: eventLoop).flatMap { (handle, region) in
let fileHandleWrapper = FileHandleWrapper(value: handle)
let fileHandleWrapper = NIOLoopBound(handle, eventLoop: eventLoop)
return self.fileio.read(fileRegion: region, allocator: .init(), eventLoop: eventLoop).flatMapThrowing { buffer in
try fileHandleWrapper.value.close()
return buffer
Expand Down

0 comments on commit 89cdbcf

Please sign in to comment.