valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.94k stars 1.76k forks source link

The fasthttp fs module does not provide a separate cache container for the Zstandard (zst) compression algorithm. #1827

Closed newacorn closed 3 months ago

newacorn commented 3 months ago

Issue Test

func TestAcceptEncodingIsOnlyZstd(t *testing.T) {
    const fileName = "test.txt"
    f, err := os.Create("/tmp/" + fileName)
    if err != nil {
        t.Fatal(err)
    }
    defer func() {
        os.Remove("/tmp/" + fileName)
    }()
    _, err = f.Write(bytes.Repeat([]byte("1"), 1000))
    if err != nil {
        t.Fatal(err)
    }
    f.Close()
    fs := FS{Root: "/tmp", Compress: true, CacheDuration: time.Hour * 20}
    h := fs.NewRequestHandler()
    server := Server{
        Handler: h,
    }
    pcs := fasthttputil.NewPipeConns()
    cliCon, serCon := pcs.Conn1(), pcs.Conn2()
    go func() {
        defer func() {
            _ = cliCon.Close()
        }()
        req := AcquireRequest()
        resp := AcquireResponse()
        defer func() {
            ReleaseRequest(req)
            ReleaseResponse(resp)
        }()
        req.SetRequestURI("http://127.0.0.1:7070/" + fileName)
        req.Header.Set(HeaderAcceptEncoding, "zstd")
        _, err = req.WriteTo(cliCon)
        if err != nil {
            t.Error(err)
            return
        }
        err = resp.Read(bufio.NewReader(cliCon))
        if err != nil {
            t.Error(err)
            return
        }
        if !bytes.Equal(resp.Header.ContentEncoding(), []byte("zstd")) {
            t.Error("not zstd encoding")
            return
        }
        resp.Reset()
        req.Header.Del(HeaderAcceptEncoding)
        _, err = req.WriteTo(cliCon)
        if err != nil {
            t.Error(err)
            return
        }
        err = resp.Read(bufio.NewReader(cliCon))
        if err != nil {
            t.Error(err)
            return
        }
        if len(resp.Header.ContentEncoding()) != 0 {
            t.Error("has content encoding header")
            return
        }
        if utf8.Valid(resp.Body()) != true {
            t.Error("invalid utf8 bytes sequence")
            return
        }
        err = cliCon.Close()
        if err != nil {
            t.Error(err)
        }
    }()
    err = server.ServeConn(serCon)
    if err != nil {
        t.Fatal(err)
    }

Result

=== RUN   TestAcceptEncodingIsOnlyZstd
    fs_fs_test.go:787: invalid utf8 bytes sequence
--- FAIL: TestAcceptEncodingIsOnlyZstd (0.00s)

Let's simply add a separate cache container for zstd.

erikdubbelboer commented 3 months ago

Good one, I'm open for a pull request.