简体   繁体   中英

gzip compression to http responseWriter

I'm new to Go. But am playing with a REST Api. I cant get the same behavior out of json.Marshal as json.Encoder in two functions i have

I wanted to use this function to gzip my responses:

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()
}

But this function returns this:

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

Here is the go function:

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()

}

But when i uncomment:

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

it works fine.

I think the problem is the use of fmt.Fprint(w, gzipFast(&b)) .

If you look to the definition of gzipFast it returns a []byte . You are putting this slice into the print function, which is "printing" everything into w .

If you look at the definition of the io.Writer:

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

You see that the writer can handle []byte as input.

Instead of fmt.Fprint(w, gzipFast(&b)) you should use w.Write(gzipFast(&b)) . Then you don't need to uncomment:

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

Everything as a small example, which shows what is happening in your code:

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

You need to flush or close your gzip writer before accessing the underlying bytes, eg

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()
}

Otherwise what's been buffer in the gzip writer, but not yet written out to the final stream isn't getting collected up.

I would avoid gzip-ing []byte manually. You can easily use already existing writers from standard library. Additionally, take a look on compress/flate which I think should be use instead of 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))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM