Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
1369f1d4
Commit
1369f1d4
authored
Jan 09, 2018
by
Jacob Vosmaer (GitLab)
Committed by
Nick Thomas
Jan 09, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add option to disable archive caching
parent
1faea24a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
31 deletions
+66
-31
gitaly_test.go
gitaly_test.go
+27
-8
internal/git/archive.go
internal/git/archive.go
+39
-23
No files found.
gitaly_test.go
View file @
1369f1d4
...
...
@@ -3,6 +3,7 @@ package main
import
(
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
...
...
@@ -448,19 +449,37 @@ func TestGetArchiveProxiedToGitalySuccessfully(t *testing.T) {
repoStorage
:=
"default"
oid
:=
"54fcc214b94e78d7a41a9a8fe6d87a5e59500e51"
repoRelativePath
:=
"foo/bar.git"
archivePath
:=
"my/path"
archivePrefix
:=
"repo-1"
jsonParams
:=
fmt
.
Sprintf
(
`{"GitalyServer":{"Address":"%s","Token":""},"GitalyRepository":{"storage_name":"%s","relative_path":"%s"},"ArchivePath":"%s","ArchivePrefix":"%s","CommitId":"%s"}`
,
gitalyAddress
,
repoStorage
,
repoRelativePath
,
archivePath
,
archivePrefix
,
oid
)
expectedBody
:=
testhelper
.
GitalyGetArchiveResponseMock
archiveLength
:=
len
(
expectedBody
)
testCases
:=
[]
struct
{
archivePath
string
cacheDisabled
bool
}{
{
archivePath
:
path
.
Join
(
scratchDir
,
"my/path"
),
cacheDisabled
:
false
},
{
archivePath
:
"/var/empty/my/path"
,
cacheDisabled
:
true
},
}
for
_
,
tc
:=
range
testCases
{
jsonParams
:=
fmt
.
Sprintf
(
`{"GitalyServer":{"Address":"%s","Token":""},"GitalyRepository":{"storage_name":"%s","relative_path":"%s"},"ArchivePath":"%s","ArchivePrefix":"%s","CommitId":"%s","DisableCache":%v}`
,
gitalyAddress
,
repoStorage
,
repoRelativePath
,
tc
.
archivePath
,
archivePrefix
,
oid
,
tc
.
cacheDisabled
)
resp
,
body
,
err
:=
doSendDataRequest
(
"/archive.tar.gz"
,
"git-archive"
,
jsonParams
)
require
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"GET %q: status code"
,
resp
.
Request
.
URL
)
assert
.
Equal
(
t
,
expectedBody
,
string
(
body
),
"GET %q: response body"
,
resp
.
Request
.
URL
)
assert
.
Equal
(
t
,
archiveLength
,
len
(
body
),
"GET %q: body size"
,
resp
.
Request
.
URL
)
if
tc
.
cacheDisabled
{
_
,
err
:=
os
.
Stat
(
tc
.
archivePath
)
require
.
True
(
t
,
os
.
IsNotExist
(
err
),
"expected 'does not exist', got: %v"
,
err
)
}
else
{
cachedArchive
,
err
:=
ioutil
.
ReadFile
(
tc
.
archivePath
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
expectedBody
,
string
(
cachedArchive
))
}
}
}
func
TestGetArchiveProxiedToGitalyInterruptedStream
(
t
*
testing
.
T
)
{
...
...
internal/git/archive.go
View file @
1369f1d4
...
...
@@ -30,6 +30,7 @@ type archiveParams struct {
CommitId
string
GitalyServer
gitaly
.
Server
GitalyRepository
pb
.
Repository
DisableCache
bool
}
var
(
...
...
@@ -61,9 +62,12 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
return
}
cacheEnabled
:=
!
params
.
DisableCache
archiveFilename
:=
path
.
Base
(
params
.
ArchivePath
)
if
cachedArchive
,
err
:=
os
.
Open
(
params
.
ArchivePath
);
err
==
nil
{
if
cacheEnabled
{
cachedArchive
,
err
:=
os
.
Open
(
params
.
ArchivePath
)
if
err
==
nil
{
defer
cachedArchive
.
Close
()
gitArchiveCache
.
WithLabelValues
(
"hit"
)
.
Inc
()
setArchiveHeaders
(
w
,
format
,
archiveFilename
)
...
...
@@ -73,20 +77,26 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
http
.
ServeContent
(
w
,
r
,
""
,
time
.
Unix
(
0
,
0
),
cachedArchive
)
return
}
}
gitArchiveCache
.
WithLabelValues
(
"miss"
)
.
Inc
()
var
tempFile
*
os
.
File
var
err
error
if
cacheEnabled
{
// We assume the tempFile has a unique name so that concurrent requests are
// safe. We create the tempfile in the same directory as the final cached
// archive we want to create so that we can use an atomic link(2) operation
// to finalize the cached archive.
tempFile
,
err
:
=
prepareArchiveTempfile
(
path
.
Dir
(
params
.
ArchivePath
),
archiveFilename
)
tempFile
,
err
=
prepareArchiveTempfile
(
path
.
Dir
(
params
.
ArchivePath
),
archiveFilename
)
if
err
!=
nil
{
helper
.
Fail500
(
w
,
r
,
fmt
.
Errorf
(
"SendArchive: create tempfile: %v"
,
err
))
return
}
defer
tempFile
.
Close
()
defer
os
.
Remove
(
tempFile
.
Name
())
}
var
archiveReader
io
.
Reader
if
params
.
GitalyServer
.
Address
!=
""
{
...
...
@@ -103,7 +113,10 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
return
}
reader
:=
io
.
TeeReader
(
archiveReader
,
tempFile
)
reader
:=
archiveReader
if
cacheEnabled
{
reader
=
io
.
TeeReader
(
archiveReader
,
tempFile
)
}
// Start writing the response
setArchiveHeaders
(
w
,
format
,
archiveFilename
)
...
...
@@ -113,10 +126,13 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
return
}
if
err
:=
finalizeCachedArchive
(
tempFile
,
params
.
ArchivePath
);
err
!=
nil
{
if
cacheEnabled
{
err
:=
finalizeCachedArchive
(
tempFile
,
params
.
ArchivePath
)
if
err
!=
nil
{
helper
.
LogError
(
r
,
fmt
.
Errorf
(
"SendArchive: finalize cached archive: %v"
,
err
))
return
}
}
}
func
handleArchiveWithGitaly
(
r
*
http
.
Request
,
params
archiveParams
,
format
pb
.
GetArchiveRequest_Format
)
(
io
.
Reader
,
error
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment