Skip to content

Commit ca8a854

Browse files
mrexodialafriks
authored andcommitted
Memory usage improvements (go-gitea#3013)
* govendor update code.gitea.io/git Signed-off-by: Duncan Ogilvie <[email protected]> * Greatly improve memory usage Signed-off-by: Duncan Ogilvie <[email protected]>
1 parent 5ec9c45 commit ca8a854

File tree

9 files changed

+86
-22
lines changed

9 files changed

+86
-22
lines changed

modules/context/repo.go

+3
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) {
143143
if err != nil {
144144
return nil, err
145145
}
146+
if treeEntry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
147+
return nil, git.ErrNotExist{ID: "", RelPath: ".editorconfig"}
148+
}
146149
reader, err := treeEntry.Blob().Data()
147150
if err != nil {
148151
return nil, err

routers/repo/download.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error {
4545

4646
// ServeBlob download a git.Blob
4747
func ServeBlob(ctx *context.Context, blob *git.Blob) error {
48-
dataRc, err := blob.Data()
48+
dataRc, err := blob.DataAsync()
4949
if err != nil {
5050
return err
5151
}
52+
defer dataRc.Close()
5253

5354
return ServeData(ctx, ctx.Repo.TreePath, dataRc)
5455
}

routers/repo/editor.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,16 @@ func editFile(ctx *context.Context, isNewFile bool) {
7373

7474
// No way to edit a directory online.
7575
if entry.IsDir() {
76-
ctx.Handle(404, "", nil)
76+
ctx.Handle(404, "entry.IsDir", nil)
7777
return
7878
}
7979

8080
blob := entry.Blob()
81+
if blob.Size() >= setting.UI.MaxDisplayFileSize {
82+
ctx.Handle(404, "blob.Size", err)
83+
return
84+
}
85+
8186
dataRc, err := blob.Data()
8287
if err != nil {
8388
ctx.Handle(404, "blob.Data", err)
@@ -93,7 +98,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
9398

9499
// Only text file are editable online.
95100
if !base.IsTextFile(buf) {
96-
ctx.Handle(404, "", nil)
101+
ctx.Handle(404, "base.IsTextFile", nil)
97102
return
98103
}
99104

routers/repo/issue.go

+3
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ func getFileContentFromDefaultBranch(ctx *context.Context, filename string) (str
319319
if err != nil {
320320
return "", false
321321
}
322+
if entry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
323+
return "", false
324+
}
322325
r, err = entry.Blob().Data()
323326
if err != nil {
324327
return "", false

routers/repo/view.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ func renderDirectory(ctx *context.Context, treeLink string) {
7676
ctx.Data["ReadmeInList"] = true
7777
ctx.Data["ReadmeExist"] = true
7878

79-
dataRc, err := readmeFile.Data()
79+
dataRc, err := readmeFile.DataAsync()
8080
if err != nil {
8181
ctx.Handle(500, "Data", err)
8282
return
8383
}
84+
defer dataRc.Close()
8485

8586
buf := make([]byte, 1024)
8687
n, _ := dataRc.Read(buf)
@@ -91,14 +92,21 @@ func renderDirectory(ctx *context.Context, treeLink string) {
9192
ctx.Data["FileName"] = readmeFile.Name()
9293
// FIXME: what happens when README file is an image?
9394
if isTextFile {
94-
d, _ := ioutil.ReadAll(dataRc)
95-
buf = append(buf, d...)
96-
if markup.Type(readmeFile.Name()) != "" {
97-
ctx.Data["IsMarkup"] = true
98-
ctx.Data["FileContent"] = string(markup.Render(readmeFile.Name(), buf, treeLink, ctx.Repo.Repository.ComposeMetas()))
95+
if readmeFile.Size() >= setting.UI.MaxDisplayFileSize {
96+
// Pretend that this is a normal text file to display 'This file is too large to be shown'
97+
ctx.Data["IsFileTooLarge"] = true
98+
ctx.Data["IsTextFile"] = true
99+
ctx.Data["FileSize"] = readmeFile.Size()
99100
} else {
100-
ctx.Data["IsRenderedHTML"] = true
101-
ctx.Data["FileContent"] = string(bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1))
101+
d, _ := ioutil.ReadAll(dataRc)
102+
buf = append(buf, d...)
103+
if markup.Type(readmeFile.Name()) != "" {
104+
ctx.Data["IsMarkup"] = true
105+
ctx.Data["FileContent"] = string(markup.Render(readmeFile.Name(), buf, treeLink, ctx.Repo.Repository.ComposeMetas()))
106+
} else {
107+
ctx.Data["IsRenderedHTML"] = true
108+
ctx.Data["FileContent"] = string(bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1))
109+
}
102110
}
103111
}
104112
}
@@ -135,11 +143,12 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
135143
ctx.Data["IsViewFile"] = true
136144

137145
blob := entry.Blob()
138-
dataRc, err := blob.Data()
146+
dataRc, err := blob.DataAsync()
139147
if err != nil {
140-
ctx.Handle(500, "Data", err)
148+
ctx.Handle(500, "DataAsync", err)
141149
return
142150
}
151+
defer dataRc.Close()
143152

144153
ctx.Data["FileSize"] = blob.Size()
145154
ctx.Data["FileName"] = blob.Name()

vendor/code.gitea.io/git/blob.go

+46-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/code.gitea.io/git/commit.go

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/code.gitea.io/git/git.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/vendor.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"ignore": "test appengine",
44
"package": [
55
{
6-
"checksumSHA1": "JN/re4+x/hCzMLGHmieUcykVDAg=",
6+
"checksumSHA1": "vAVjAz7Wpjnu7GGba4JLIDTpQEw=",
77
"path": "code.gitea.io/git",
8-
"revision": "d47b98c44c9a6472e44ab80efe65235e11c6da2a",
9-
"revisionTime": "2017-10-23T00:52:09Z"
8+
"revision": "f9dd6826bbb51c92c6964ce18176c304ea286e54",
9+
"revisionTime": "2017-11-28T15:25:05Z"
1010
},
1111
{
1212
"checksumSHA1": "OICEgmUefW4L4l/FK/NVFnl/aOM=",

0 commit comments

Comments
 (0)