Commit 2ee8ec83 authored by Nick Thomas's avatar Nick Thomas

Merge branch 'ban-context-background' into 'master'

Ban context.Background

See merge request gitlab-org/gitlab-workhorse!201
parents bf1d9467 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
......
#!/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
}'
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)
ctx2, 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)
......
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
}
......
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
}
......
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)
}
......
......@@ -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
}
......
......@@ -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
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment