How to use NonBlockingFileIO for repeated writes

I've just released Release Refactor request body streaming state handling · vapor/vapor · GitHub which addresses the Precondition failed: blacklisted errno 9 Bad file descriptor error. Request's body.drain method will now wait until the previously returned future has completed before calling the handler again. This means that simply returning the file io write future in the drain handler will now result in correct behavior.

I've also included a streaming file upload example in the Development module: vapor/routes.swift at main · vapor/vapor · GitHub

Copying it here for posterity:

app.on(.POST, "upload", body: .stream) { req -> EventLoopFuture<HTTPStatus> in
    return req.application.fileio.openFile(
        path: "/Users/tanner/Desktop/foo.txt",
        mode: .write,
        flags: .allowFileCreation(),
        eventLoop: req.eventLoop
    ).flatMap { fileHandle in
        let promise = req.eventLoop.makePromise(of: HTTPStatus.self)
        req.body.drain { part in
            switch part {
            case .buffer(let buffer):
                return req.application.fileio.write(
                    fileHandle: fileHandle,
                    buffer: buffer,
                    eventLoop: req.eventLoop
                )
            case .error(let error):
                promise.fail(error)
                try! fileHandle.close()
                return req.eventLoop.makeSucceededFuture(())
            case .end:
                promise.succeed(.ok)
                try! fileHandle.close()
                return req.eventLoop.makeSucceededFuture(())
            }
        }
        return promise.futureResult
    }
}

Thanks to @johannesweiss for sharing swift-nio-examples/backpressure-file-io-channel at main · apple/swift-nio-examples · GitHub which I referenced heavily for Vapor's updated body stream state machine.

3 Likes