【问题标题】:Golang writing to http response breaks input reading?Golang 写入 http 响应会中断输入读取?
【发布时间】:2015-02-05 04:36:38
【问题描述】:

我正在尝试在 Go 中编写一个小型 Web 应用程序,用户可以在其中以多部分形式上传一个 gzip 压缩文件。该应用程序解压缩并解析文件并将一些输出写入响应。但是,当我开始写入响应时,我一直遇到输入流看起来已损坏的错误。不写入响应可以解决问题,就像从非压缩输入流中读取一样。这是一个示例 http 处理程序:

func(w http.ResponseWriter, req *http.Request) {

//Get an input stream from the multipart reader
//and read it using a scanner
multiReader, _ := req.MultipartReader()
part, _ := multiReader.NextPart()
gzipReader, _ := gzip.NewReader(part)
scanner := bufio.NewScanner(gzipReader)

//Strings read from the input stream go to this channel     
inputChan := make(chan string, 1000)

//Signal completion on this channel 
donechan := make(chan bool, 1)

//This goroutine just reads text from the input scanner
//and sends it into the channel 
go func() {
    for scanner.Scan() {
        inputChan <- scanner.Text()
    }       
    close(inputChan)
}()

//Read lines from input channel. They all either start with #
//or have ten tab-separated columns
go func() {
    for line := range inputChan {
        toks := strings.Split(line, "\t")
        if len(toks) != 10 && line[0] != '#' {
            panic("Dang.")
        }
    }
    donechan <- true 
}()

//periodically write some random text to the response
go func() {
    for {
        time.Sleep(10*time.Millisecond)     
        w.Write([]byte("write\n some \n output\n"))
    }
}()

//wait until we're done to return
<-donechan
}

奇怪的是,这段代码每次都会出现恐慌,因为它总是遇到少于 10 个标记的行,尽管每次都在不同的位置。注释掉写入响应的行可以解决问题,就像从非压缩输入流中读取一样。我错过了一些明显的东西吗?如果从 gzip 文件而不是纯文本格式的文件中读取,为什么写入响应会中断?为什么它会完全破裂?

【问题讨论】:

    标签: http go gzip


    【解决方案1】:

    HTTP 协议不是全双工的:它是基于请求-响应的。只应在完成读取输入后发送输出。

    在您的代码中,您在频道上使用forrange。这将尝试读取频道直到关闭,但您永远不会关闭inputChan

    如果您从不关闭inputChan,则永远不会到达以下行:

    donechan <- true 
    

    因此从donechan 接收块:

    <-donechan
    

    当到达 EOF 时,您必须关闭 inputChan

    go func() {
        for scanner.Scan() {
            inputChan <- scanner.Text()
        }       
        close(inputChan) // THIS IS NEEDED
    }()
    

    【讨论】:

    • 谢谢!关于关闭 inputChan 的要点。该代码只是我可以生成的最小示例来证明该问题。我会编辑帖子...
    猜你喜欢
    • 2020-09-27
    • 2018-07-08
    • 1970-01-01
    • 2017-12-18
    • 2018-02-05
    • 1970-01-01
    • 2015-07-24
    • 2012-02-25
    • 1970-01-01
    相关资源
    最近更新 更多