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

affected/package: io.copy() ; did not achieve the expected result. #50090

Closed
imkos opened this issue Dec 10, 2021 · 1 comment
Closed

affected/package: io.copy() ; did not achieve the expected result. #50090

imkos opened this issue Dec 10, 2021 · 1 comment

Comments

@imkos
Copy link

imkos commented Dec 10, 2021

code1:

func UploadFileV3(url string, fileName string, fsr io.Reader) ([]byte, error) {
	pR, pW := io.Pipe()
	// Set up multipart body for reading
	multipartW := multipart.NewWriter(pW)
	go func() {
		partW, err0 := multipartW.CreateFormFile("file", fileName)
		if err0 != nil {
			log.Info("multipartW CreateFormFile err,", err0)
			pW.CloseWithError(err0)
			return
		}
		tr := io.TeeReader(fsr, partW)
		// 256kb
		buf := make([]byte, 256<<10)
		for {
			// fsr -> partW -> multipartW -> pW -> pR
			_, err := tr.Read(buf)
			if err == io.EOF {
				multipartW.Close()
				pW.Close()
				break
			}
			if err != nil {
				log.Errorf("The error reading from connector: %v", err)
				break
			}
		}
	}()
	// Send http request chunk encoding the multipart message
	req, err := http.NewRequest("POST", url, pR)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", multipartW.FormDataContentType())
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Error("POST err1,", err)
		return nil, err
	}
	defer resp.Body.Close()

	content, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	log.Info("content:", string(content))
	return content, nil
}

code2:

func UploadFileV3(url string, fileName string, fsr io.Reader) ([]byte, error) {
	pR, pW := io.Pipe()
	// Set up multipart body for reading
	multipartW := multipart.NewWriter(pW)
	go func() {
		partW, err0 := multipartW.CreateFormFile("file", fileName)
		if err0 != nil {
			log.Info("multipartW CreateFormFile err,", err0)
			pW.CloseWithError(err0)
			return
		}
		io.Copy(partW, fsr)
	}()
	// Send http request chunk encoding the multipart message
	req, err := http.NewRequest("POST", url, pR)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", multipartW.FormDataContentType())
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Error("POST err1,", err)
		return nil, err
	}
	defer resp.Body.Close()

	content, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	log.Info("content:", string(content))
	return content, nil
}

What did you expect to see?

code1: ok
code2: ok

What did you see instead?

code1: ok
code2: hang

@seankhliao
Copy link
Member

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@golang golang locked and limited conversation to collaborators Dec 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants