-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Pooled and buffered gzip implementation #5722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lafriks
merged 6 commits into
go-gitea:master
from
zeripath:issue-5182-better-gzip-encoder-management
Jan 23, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2c2393c
Pooled and buffered gzip implementation
zeripath d7e67c2
Add test for gzip
zeripath 9618f24
Add integration test
zeripath 2456cb6
Ensure lfs check within transaction
zeripath 20aef08
Try to avoid primary key problem in postgres
zeripath 49fc075
Merge branch 'master' into issue-5182-better-gzip-encoder-management
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/gzip" | ||
"code.gitea.io/gitea/modules/lfs" | ||
"code.gitea.io/gitea/modules/setting" | ||
"github.com/stretchr/testify/assert" | ||
|
||
gzipp "github.com/klauspost/compress/gzip" | ||
) | ||
|
||
func GenerateLFSOid(content io.Reader) (string, error) { | ||
h := sha256.New() | ||
if _, err := io.Copy(h, content); err != nil { | ||
return "", err | ||
} | ||
sum := h.Sum(nil) | ||
return hex.EncodeToString(sum), nil | ||
} | ||
|
||
var lfsID = int64(20000) | ||
|
||
func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string { | ||
oid, err := GenerateLFSOid(bytes.NewReader(*content)) | ||
assert.NoError(t, err) | ||
var lfsMetaObject *models.LFSMetaObject | ||
|
||
if setting.UsePostgreSQL { | ||
lfsMetaObject = &models.LFSMetaObject{ID: lfsID, Oid: oid, Size: int64(len(*content)), RepositoryID: repositoryID} | ||
} else { | ||
lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(*content)), RepositoryID: repositoryID} | ||
} | ||
|
||
lfsID = lfsID + 1 | ||
lfsMetaObject, err = models.NewLFSMetaObject(lfsMetaObject) | ||
assert.NoError(t, err) | ||
contentStore := &lfs.ContentStore{BasePath: setting.LFS.ContentPath} | ||
if !contentStore.Exists(lfsMetaObject) { | ||
err := contentStore.Put(lfsMetaObject, bytes.NewReader(*content)) | ||
assert.NoError(t, err) | ||
} | ||
return oid | ||
} | ||
|
||
func doLfs(t *testing.T, content *[]byte, expectGzip bool) { | ||
prepareTestEnv(t) | ||
repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1") | ||
assert.NoError(t, err) | ||
oid := storeObjectInRepo(t, repo.ID, content) | ||
defer repo.RemoveLFSMetaObjectByOid(oid) | ||
|
||
session := loginUser(t, "user2") | ||
|
||
// Request OID | ||
req := NewRequest(t, "GET", "/user2/repo1.git/info/lfs/objects/"+oid+"/test") | ||
req.Header.Set("Accept-Encoding", "gzip") | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
||
contentEncoding := resp.Header().Get("Content-Encoding") | ||
if !expectGzip || !setting.EnableGzip { | ||
assert.NotContains(t, contentEncoding, "gzip") | ||
|
||
result := resp.Body.Bytes() | ||
assert.Equal(t, *content, result) | ||
} else { | ||
assert.Contains(t, contentEncoding, "gzip") | ||
gzippReader, err := gzipp.NewReader(resp.Body) | ||
assert.NoError(t, err) | ||
result, err := ioutil.ReadAll(gzippReader) | ||
assert.NoError(t, err) | ||
assert.Equal(t, *content, result) | ||
} | ||
|
||
} | ||
|
||
func TestGetLFSSmall(t *testing.T) { | ||
content := []byte("A very small file\n") | ||
doLfs(t, &content, false) | ||
} | ||
|
||
func TestGetLFSLarge(t *testing.T) { | ||
content := make([]byte, gzip.MinSize*10) | ||
for i := range content { | ||
content[i] = byte(i % 256) | ||
} | ||
doLfs(t, &content, true) | ||
} | ||
|
||
func TestGetLFSGzip(t *testing.T) { | ||
b := make([]byte, gzip.MinSize*10) | ||
for i := range b { | ||
b[i] = byte(i % 256) | ||
} | ||
outputBuffer := bytes.NewBuffer([]byte{}) | ||
gzippWriter := gzipp.NewWriter(outputBuffer) | ||
gzippWriter.Write(b) | ||
gzippWriter.Close() | ||
content := outputBuffer.Bytes() | ||
doLfs(t, &content, false) | ||
} | ||
|
||
func TestGetLFSZip(t *testing.T) { | ||
b := make([]byte, gzip.MinSize*10) | ||
for i := range b { | ||
b[i] = byte(i % 256) | ||
} | ||
outputBuffer := bytes.NewBuffer([]byte{}) | ||
zipWriter := zip.NewWriter(outputBuffer) | ||
fileWriter, err := zipWriter.Create("default") | ||
assert.NoError(t, err) | ||
fileWriter.Write(b) | ||
zipWriter.Close() | ||
content := outputBuffer.Bytes() | ||
doLfs(t, &content, false) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.