doucha4054 2016-11-18 10:30
浏览 65
已采纳

Golang 1.7.3中的gzip Reader行为更改(与1.6.3相比)

I have a program that reads gzip compressed json packets from a network connection. The sender does not close the connection after sending the gzip packet. In go1.6.3 this works perfectly, i.e., the gzip packet is decoded after the gzip end-sequence is received, but in go1.7.3 the reader blocks as there is no io.EOF.

Here is a sample that simulates the network connection using a pipe (note that the writer stays open to simulate the open connection):

package main

import (
    "fmt"
    "encoding/json"
    "compress/gzip"
    "io"
    "runtime"
)

type TestJSON struct {
  TestString string `json:"test"`
}

func main() {
    fmt.Printf("Version: %s
", runtime.Version())
    pipeReader, pipeWriter := io.Pipe();

    go writeTo(pipeWriter);
    readFrom(pipeReader);
}

func writeTo(pipeWriter *io.PipeWriter){
    // marshall and compress
    testJSON := TestJSON{TestString: "test",}

    jsonString, err := json.Marshal(testJSON)
    if err != nil {
      fmt.Printf("Marshalling Error: %s
", err)
      return
    }

    gzipOut := gzip.NewWriter(pipeWriter)
    _, err = gzipOut.Write(jsonString)
    if err != nil {
      fmt.Printf("Error Writing: %s
", err)
      return
    }
    gzipOut.Close()
    //pipeWriter.Close()
}

func readFrom(pipeReader *io.PipeReader){
    // decompress and unmarshall
    gzipIn, err := gzip.NewReader(pipeReader)
    if err != nil {
      fmt.Printf("Error creating reader: %s
", err)
      return
    }
    defer gzipIn.Close()

    jsonDecoder := json.NewDecoder(gzipIn)
    msg := new(TestJSON)
    err = jsonDecoder.Decode(msg)
    if err != nil {
      fmt.Printf("Error decoding: %s
", err)
      return
    }
    fmt.Printf("Recived: %v
", msg)
}

Based on this situation I have 2 questions:

  1. Which is the correct behavior?
  2. If go1.7.3 behaves correctly, how can I decode incoming gzip packets on an open network connection?
  • 写回答

1条回答 默认 最新

  • dongxie8906 2016-11-18 14:58
    关注

    What you're seeing is the correct behavior. The old behavior of the gzip.Reader was a side effect of it being able to return a final read without io.EOF, then (0, io.EOF) in a subsequent call. Now that it properly waits for io.EOF, it will block waiting for the next gzip header or io.EOF.

    If you don't expect more files, and you want to have the gzip trailer indicate the end of the file regardless of io.EOF, you have to set Reader.Multistream(false).

    Your example works with that addition:

    func readFrom(pipeReader *io.PipeReader) {
        // decompress and unmarshall
        gzipIn, err := gzip.NewReader(pipeReader)
        if err != nil {
            fmt.Printf("Error creating reader: %s
    ", err)
            return
        }
        gzipIn.Multistream(false)
    

    https://play.golang.org/p/BdaulxMza0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?