Skip to content

Take up double the memory when encode File. #3036

Open
@iWECon

Description

@iWECon

Describe the bug

It seems to take up double the memory when use try clientRequest.content.encode(Encodable.self, as: .formData).

In addition,
when the request fails, the memory will not be released normally (still taking up double the memory).
After the request is successful, the memory will be released normally.

import Vapor

struct SendFile {
    private static func generateVideoFile(_ filePath: String) throws -> File {
        let videoFileURL = URL(fileURLWithPath: filePath)
        var videoData = try Data(contentsOf: videoFileURL)
        return File(data: ByteBuffer(data: videoData), filename: (filePath as NSString).lastPathComponent)
    }
    
    struct SendFile: Codable {
        let file: File
        let userID: String
    }

    static func sendVideo(_ filePath: String, to userID: String, app: Application) async throws {
        let file = try generateVideoFile(filePath)
        
        let sendFile = SendFile(file: file, userID: userID)
        
        let response = try await app.client.post("URI") { clientRequest in
            try clientRequest.content.encode(sendFile, as: .formData)
        }
        // ...
    }
}

Environment

  • Vapor 4
  • Ubuntu 20.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions