Skip to content

[Bug] When uploading normative routing files, the memory overhead is too large #2938

@wlynxg

Description

@wlynxg

When standard routing is used, gf will read the entire body content first, and then do parameter binding. Therefore, when there is a file parameter in the canonical route, gf will first read the content of the file into memory. When there are multiple file upload requests at the same time, it is easy to blow up the system memory.

1. What version of Go and system type/arch are you using?

go version go1.20 linux/amd64

2. What version of GoFrame are you using?

v2.5.2

3. Can this issue be re-produced with the latest release?

yes

4. What did you do?

package main

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

type UploadReq struct {
	g.Meta `path:"/upload" method:"POST" tags:"Upload" mime:"multipart/form-data" summary:"上传文件"`
	File   *ghttp.UploadFile `p:"file" type:"file" dc:"选择上传文件"`
}
type UploadRes struct{}

type cUpload struct{}

func (u cUpload) Upload(ctx context.Context, req *UploadReq) (*UploadRes, error) {
	return nil, nil
}

func main() {
	s := g.Server()
	s.Group("/", func(group *ghttp.RouterGroup) {
		group.Bind(cUpload{})
	})
        s.SetClientMaxBodySize(100 * 1024 * 1024)
	s.Run()
}

5. What did you expect to see?

When there are multiple upload requests at the same time, the memory overhead will not be too large.

6. What did you see instead?

Read the file data and write the data to the disk at the same time, instead of reading all the data and then writing it to the disk.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIt is confirmed a bug, but don't worry, we'll handle it.help wantedplannedThis issue/proposal is planned into our next steps.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions