Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-workhorse
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-workhorse
Commits
c609e4fa
Commit
c609e4fa
authored
Oct 06, 2017
by
Jacob Vosmaer (GitLab)
Committed by
Nick Thomas
Oct 06, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ban context.Background
parent
bf1d9467
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
39 additions
and
27 deletions
+39
-27
Makefile
Makefile
+1
-0
_support/detect-context.sh
_support/detect-context.sh
+7
-0
internal/artifacts/artifacts_store.go
internal/artifacts/artifacts_store.go
+5
-6
internal/artifacts/artifacts_upload.go
internal/artifacts/artifacts_upload.go
+5
-4
internal/upload/accelerate.go
internal/upload/accelerate.go
+5
-4
internal/upload/rewrite.go
internal/upload/rewrite.go
+7
-6
internal/upload/uploads.go
internal/upload/uploads.go
+5
-4
internal/upload/uploads_test.go
internal/upload/uploads_test.go
+4
-3
No files found.
Makefile
View file @
c609e4fa
...
...
@@ -30,6 +30,7 @@ ${BUILD_DIR}/_build:
.PHONY
:
test
test
:
clean-build clean-workhorse all govendor
go
fmt
${PKG_ALL}
|
awk
'{ print } END { if (NR > 0) { print "Please run go fmt"; exit 1 } }'
_support/detect-context.sh
cd
${GOPATH}
/src/
${PKG}
&&
govendor
sync
go
test
${PKG_ALL}
@
echo
SUCCESS
...
...
_support/detect-context.sh
0 → 100755
View file @
c609e4fa
#!/bin/sh
git
grep
'context.\(Background\|TODO\)'
|
grep
-v
-e
'^[^:]*_test.go:'
-e
'^vendor/'
-e
'^_support/'
|
awk
'{
print "Found disallowed use of context.Background or TODO"
print
exit 1
}'
internal/artifacts/artifacts_store.go
View file @
c609e4fa
package
artifacts
import
(
"context"
"fmt"
"mime/multipart"
"net/http"
"os"
"time"
"golang.org/x/net/context"
"golang.org/x/net/context/ctxhttp"
"github.com/prometheus/client_golang/prometheus"
)
...
...
@@ -60,7 +58,7 @@ func init() {
objectStorageUploadBytes
)
}
func
(
a
*
artifactsUploadProcessor
)
storeFile
(
formName
,
fileName
string
,
writer
*
multipart
.
Writer
)
error
{
func
(
a
*
artifactsUploadProcessor
)
storeFile
(
ctx
context
.
Context
,
formName
,
fileName
string
,
writer
*
multipart
.
Writer
)
error
{
if
a
.
ObjectStore
.
StoreURL
==
""
{
return
nil
}
...
...
@@ -104,10 +102,11 @@ func (a *artifactsUploadProcessor) storeFile(formName, fileName string, writer *
timeout
=
a
.
ObjectStore
.
Timeout
}
ctx
,
cancelFn
:=
context
.
WithTimeout
(
context
.
Background
()
,
time
.
Duration
(
timeout
)
*
time
.
Second
)
ctx
2
,
cancelFn
:=
context
.
WithTimeout
(
ctx
,
time
.
Duration
(
timeout
)
*
time
.
Second
)
defer
cancelFn
()
req
=
req
.
WithContext
(
ctx2
)
resp
,
err
:=
ctxhttp
.
Do
(
ctx
,
http
.
DefaultClient
,
req
)
resp
,
err
:=
http
.
DefaultClient
.
Do
(
req
)
if
err
!=
nil
{
objectStorageUploadRequestsRequestFailed
.
Inc
()
return
fmt
.
Errorf
(
"PUT request %q: %v"
,
a
.
ObjectStore
.
StoreURL
,
err
)
...
...
internal/artifacts/artifacts_upload.go
View file @
c609e4fa
package
artifacts
import
(
"context"
"fmt"
"io"
"io/ioutil"
...
...
@@ -44,7 +45,7 @@ func (a *artifactsUploadProcessor) generateMetadataFromZip(fileName string, meta
return
true
,
nil
}
func
(
a
*
artifactsUploadProcessor
)
ProcessFile
(
formName
,
fileName
string
,
writer
*
multipart
.
Writer
)
error
{
func
(
a
*
artifactsUploadProcessor
)
ProcessFile
(
ctx
context
.
Context
,
formName
,
fileName
string
,
writer
*
multipart
.
Writer
)
error
{
// ProcessFile for artifacts requires file form-data field name to eq `file`
if
formName
!=
"file"
{
...
...
@@ -74,17 +75,17 @@ func (a *artifactsUploadProcessor) ProcessFile(formName, fileName string, writer
writer
.
WriteField
(
"metadata.name"
,
"metadata.gz"
)
}
if
err
:=
a
.
storeFile
(
formName
,
fileName
,
writer
);
err
!=
nil
{
if
err
:=
a
.
storeFile
(
ctx
,
formName
,
fileName
,
writer
);
err
!=
nil
{
return
fmt
.
Errorf
(
"storeFile: %v"
,
err
)
}
return
nil
}
func
(
a
*
artifactsUploadProcessor
)
ProcessField
(
formName
string
,
writer
*
multipart
.
Writer
)
error
{
func
(
a
*
artifactsUploadProcessor
)
ProcessField
(
ctx
context
.
Context
,
formName
string
,
writer
*
multipart
.
Writer
)
error
{
return
nil
}
func
(
a
*
artifactsUploadProcessor
)
Finalize
()
error
{
func
(
a
*
artifactsUploadProcessor
)
Finalize
(
ctx
context
.
Context
)
error
{
return
nil
}
...
...
internal/upload/accelerate.go
View file @
c609e4fa
package
upload
import
(
"context"
"fmt"
"mime/multipart"
"net/http"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/secret"
"github.com/dgrijalva/jwt-go"
jwt
"github.com/dgrijalva/jwt-go"
)
const
RewrittenFieldsHeader
=
"Gitlab-Workhorse-Multipart-Fields"
...
...
@@ -29,7 +30,7 @@ func Accelerate(tempDir string, h http.Handler) http.Handler {
})
}
func
(
s
*
savedFileTracker
)
ProcessFile
(
fieldName
,
fileName
string
,
_
*
multipart
.
Writer
)
error
{
func
(
s
*
savedFileTracker
)
ProcessFile
(
_
context
.
Context
,
fieldName
,
fileName
string
,
_
*
multipart
.
Writer
)
error
{
if
s
.
rewrittenFields
==
nil
{
s
.
rewrittenFields
=
make
(
map
[
string
]
string
)
}
...
...
@@ -37,11 +38,11 @@ func (s *savedFileTracker) ProcessFile(fieldName, fileName string, _ *multipart.
return
nil
}
func
(
_
*
savedFileTracker
)
ProcessField
(
_
string
,
_
*
multipart
.
Writer
)
error
{
func
(
_
*
savedFileTracker
)
ProcessField
(
_
context
.
Context
,
_
string
,
_
*
multipart
.
Writer
)
error
{
return
nil
}
func
(
s
*
savedFileTracker
)
Finalize
()
error
{
func
(
s
*
savedFileTracker
)
Finalize
(
_
context
.
Context
)
error
{
if
s
.
rewrittenFields
==
nil
{
return
nil
}
...
...
internal/upload/rewrite.go
View file @
c609e4fa
package
upload
import
(
"context"
"fmt"
"io"
"io/ioutil"
...
...
@@ -101,10 +102,10 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te
// Copy form field
if
p
.
FileName
()
!=
""
{
err
=
rew
.
handleFilePart
(
name
,
p
)
err
=
rew
.
handleFilePart
(
r
.
Context
(),
name
,
p
)
}
else
{
err
=
rew
.
copyPart
(
name
,
p
)
err
=
rew
.
copyPart
(
r
.
Context
(),
name
,
p
)
}
if
err
!=
nil
{
...
...
@@ -115,7 +116,7 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te
return
cleanup
,
nil
}
func
(
rew
*
rewriter
)
handleFilePart
(
name
string
,
p
*
multipart
.
Part
)
error
{
func
(
rew
*
rewriter
)
handleFilePart
(
ctx
context
.
Context
,
name
string
,
p
*
multipart
.
Part
)
error
{
multipartFiles
.
WithLabelValues
(
rew
.
filter
.
Name
())
.
Inc
()
filename
:=
p
.
FileName
()
...
...
@@ -153,14 +154,14 @@ func (rew *rewriter) handleFilePart(name string, p *multipart.Part) error {
file
.
Close
()
if
err
:=
rew
.
filter
.
ProcessFile
(
name
,
file
.
Name
(),
rew
.
writer
);
err
!=
nil
{
if
err
:=
rew
.
filter
.
ProcessFile
(
ctx
,
name
,
file
.
Name
(),
rew
.
writer
);
err
!=
nil
{
return
err
}
return
nil
}
func
(
rew
*
rewriter
)
copyPart
(
name
string
,
p
*
multipart
.
Part
)
error
{
func
(
rew
*
rewriter
)
copyPart
(
ctx
context
.
Context
,
name
string
,
p
*
multipart
.
Part
)
error
{
np
,
err
:=
rew
.
writer
.
CreatePart
(
p
.
Header
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"create multipart field: %v"
,
err
)
...
...
@@ -170,7 +171,7 @@ func (rew *rewriter) copyPart(name string, p *multipart.Part) error {
return
fmt
.
Errorf
(
"duplicate multipart field: %v"
,
err
)
}
if
err
:=
rew
.
filter
.
ProcessField
(
name
,
rew
.
writer
);
err
!=
nil
{
if
err
:=
rew
.
filter
.
ProcessField
(
ctx
,
name
,
rew
.
writer
);
err
!=
nil
{
return
fmt
.
Errorf
(
"process multipart field: %v"
,
err
)
}
...
...
internal/upload/uploads.go
View file @
c609e4fa
...
...
@@ -2,6 +2,7 @@ package upload
import
(
"bytes"
"context"
"fmt"
"io/ioutil"
"mime/multipart"
...
...
@@ -12,9 +13,9 @@ import (
// These methods are allowed to have thread-unsafe implementations.
type
MultipartFormProcessor
interface
{
ProcessFile
(
formName
,
fileName
string
,
writer
*
multipart
.
Writer
)
error
ProcessField
(
formName
string
,
writer
*
multipart
.
Writer
)
error
Finalize
()
error
ProcessFile
(
ctx
context
.
Context
,
formName
,
fileName
string
,
writer
*
multipart
.
Writer
)
error
ProcessField
(
ctx
context
.
Context
,
formName
string
,
writer
*
multipart
.
Writer
)
error
Finalize
(
ctx
context
.
Context
)
error
Name
()
string
}
...
...
@@ -51,7 +52,7 @@ func HandleFileUploads(w http.ResponseWriter, r *http.Request, h http.Handler, t
r
.
ContentLength
=
int64
(
body
.
Len
())
r
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
if
err
:=
filter
.
Finalize
();
err
!=
nil
{
if
err
:=
filter
.
Finalize
(
r
.
Context
()
);
err
!=
nil
{
helper
.
Fail500
(
w
,
r
,
fmt
.
Errorf
(
"handleFileUploads: Finalize: %v"
,
err
))
return
}
...
...
internal/upload/uploads_test.go
View file @
c609e4fa
...
...
@@ -2,6 +2,7 @@ package upload
import
(
"bytes"
"context"
"errors"
"fmt"
"io"
...
...
@@ -24,21 +25,21 @@ var nilHandler = http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
type
testFormProcessor
struct
{}
func
(
a
*
testFormProcessor
)
ProcessFile
(
formName
,
fileName
string
,
writer
*
multipart
.
Writer
)
error
{
func
(
a
*
testFormProcessor
)
ProcessFile
(
ctx
context
.
Context
,
formName
,
fileName
string
,
writer
*
multipart
.
Writer
)
error
{
if
formName
!=
"file"
&&
fileName
!=
"my.file"
{
return
errors
.
New
(
"illegal file"
)
}
return
nil
}
func
(
a
*
testFormProcessor
)
ProcessField
(
formName
string
,
writer
*
multipart
.
Writer
)
error
{
func
(
a
*
testFormProcessor
)
ProcessField
(
ctx
context
.
Context
,
formName
string
,
writer
*
multipart
.
Writer
)
error
{
if
formName
!=
"token"
{
return
errors
.
New
(
"illegal field"
)
}
return
nil
}
func
(
a
*
testFormProcessor
)
Finalize
()
error
{
func
(
a
*
testFormProcessor
)
Finalize
(
ctx
context
.
Context
)
error
{
return
nil
}
...
...
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