【问题标题】:gzip compression to http responseWritergzip 压缩到 http responseWriter
【发布时间】:2016-08-24 01:06:59
【问题描述】:

我是 Go 新手。但是我在玩 REST Api。在我拥有的两个函数中,我无法从 json.Marshal 中获得与 json.Encoder 相同的行为

我想用这个函数压缩我的回复:

func gzipFast(a *[]byte) []byte {
    var b bytes.Buffer
    gz := gzip.NewWriter(&b)
    defer gz.Close()
    if _, err := gz.Write(*a); err != nil {
        panic(err)
    }
    return b.Bytes()
}

但是这个函数返回这个:

curl http://localhost:8081/compressedget --compressed --verbose
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8081 (#0)
> GET /compressedget HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.47.0
> Accept: */*
> Accept-Encoding: deflate, gzip
> 
< HTTP/1.1 200 OK
< Content-Encoding: gzip
< Content-Type: application/json
< Date: Wed, 24 Aug 2016 00:59:38 GMT
< Content-Length: 30
< 
* Connection #0 to host localhost left intact

这里是go函数:

func CompressedGet(w http.ResponseWriter, r *http.Request, ps  httprouter.Params) {

    box := Box{Width: 10, Height: 20, Color: "gree", Open: false}
    box.ars = make([]int, 100)
    for i := range box.ars {
        box.ars[i] = i
    }
    //fmt.Println(r.Header.Get("Content-Encoding"))

    w.Header().Set("Content-Type", "application/json")
    w.Header().Set("Content-Encoding", "gzip")
    b, _ := json.Marshal(box)
    //fmt.Println(len(b))
    //fmt.Println(len(gzipFast(&b)))

    fmt.Fprint(w, gzipFast(&b))
    //fmt.Println(len(gzipSlow(b)))
    //gz := gzip.NewWriter(w)
    //defer gz.Close()
    //json.NewEncoder(gz).Encode(box)
    r.Body.Close()

}

但是当我取消注释时:

//gz := gzip.NewWriter(w)
//defer gz.Close()
//json.NewEncoder(gz).Encode(box)

效果很好。

【问题讨论】:

  • 也许是因为这 3 行是不必要的? gzipFast 函数已经在对 json 编码框进行 gzip 压缩。

标签: json rest http go gzip


【解决方案1】:

您需要在访问底层字节之前刷新或关闭您的 gzip 编写器,例如

func gzipFast(a *[]byte) []byte {
    var b bytes.Buffer
    gz := gzip.NewWriter(&b)
    if _, err := gz.Write(*a); err != nil {
        gz.Close()
        panic(err)
    }
    gz.Close()
    return b.Bytes()
}

否则,gzip 编写器中的缓冲区,但尚未写入最终流的内容不会被收集起来。

【讨论】:

  • 这修复了它。谢谢!
【解决方案2】:

我认为问题在于fmt.Fprint(w, gzipFast(&amp;b)) 的使用。

如果您查看 gzipFast 的定义,它会返回 []byte。您正在将此切片放入打印功能中,这会将所有内容“打印”到w

如果你看一下io.Writer的定义:

type Writer interface {
        Write(p []byte) (n int, err error) }

您看到作者可以处理[]byte 作为输入。

您应该使用w.Write(gzipFast(&amp;b)) 而不是fmt.Fprint(w, gzipFast(&amp;b))。那么你就不需要取消注释了:

//gz := gzip.NewWriter(w)
//defer gz.Close()
//json.NewEncoder(gz).Encode(box)

一切都作为一个小例子,它显示了你的代码中发生了什么:

https://play.golang.org/p/6rzqLWTGiI

【讨论】:

    【解决方案3】:

    我会避免手动压缩[]byte。您可以轻松地使用标准库中现有的编写器。此外,请查看compress/flate,我认为应该使用它来代替 gzip。

    package main
    
    import (
        "net/http"
        "encoding/json"
        "compress/gzip"
        "log"
    )
    
    type Box struct {
        Width int `json:"width"`
    }
    
    func writeJsonResponseCompressed(w http.ResponseWriter, r *http.Request) {
    
        box := &Box{Width: 10}
    
        w.Header().Set("Content-Type", "application/json")
        w.Header().Set("Content-Encoding", "gzip")
    
        body, err := json.Marshal(box)
        if err != nil {
            // Your error handling
            return
        }
    
        writer, err := gzip.NewWriterLevel(w, gzip.BestCompression)
        if err != nil {
            // Your error handling
            return
        }
    
        defer writer.Close()
    
        writer.Write(body)
    }
    
    func main() {
        http.HandleFunc("/compressedget", writeJsonResponseCompressed)
        log.Fatal(http.ListenAndServe(":8081", nil))
    }
    

    【讨论】:

    • 我同意,这真的只是对我学习一些东西的考验。如果此 api 是生产应用程序,那么您的解决方案是最好的。
    猜你喜欢
    • 2012-02-12
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2012-12-02
    • 2011-06-11
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多