Commit dea4edff authored by Nick Thomas's avatar Nick Thomas

Merge branch 'use-contexts' into 'master'

Use http.Request contexts for Gitaly calls

See merge request !179
parents fadf660e 2b93db7a
...@@ -44,7 +44,7 @@ func handleSendBlobWithGitaly(w http.ResponseWriter, r *http.Request, params *bl ...@@ -44,7 +44,7 @@ func handleSendBlobWithGitaly(w http.ResponseWriter, r *http.Request, params *bl
helper.Fail500(w, r, fmt.Errorf("commit.GetBlob: %v", err)) helper.Fail500(w, r, fmt.Errorf("commit.GetBlob: %v", err))
} }
if err := commitClient.SendBlob(w, &params.TreeEntryRequest); err != nil { if err := commitClient.SendBlob(r.Context(), w, &params.TreeEntryRequest); err != nil {
helper.Fail500(w, r, fmt.Errorf("commit.GetBlob: %v", err)) helper.Fail500(w, r, fmt.Errorf("commit.GetBlob: %v", err))
} }
} }
......
package git package git
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
...@@ -8,8 +9,6 @@ import ( ...@@ -8,8 +9,6 @@ import (
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api" "gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/gitaly" "gitlab.com/gitlab-org/gitlab-workhorse/internal/gitaly"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper" "gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"golang.org/x/net/context"
) )
func GetInfoRefsHandler(a *api.API) http.Handler { func GetInfoRefsHandler(a *api.API) http.Handler {
...@@ -35,7 +34,7 @@ func handleGetInfoRefs(rw http.ResponseWriter, r *http.Request, a *api.Response) ...@@ -35,7 +34,7 @@ func handleGetInfoRefs(rw http.ResponseWriter, r *http.Request, a *api.Response)
if a.GitalyServer.Address == "" { if a.GitalyServer.Address == "" {
err = handleGetInfoRefsLocally(w, a, rpc) err = handleGetInfoRefsLocally(w, a, rpc)
} else { } else {
err = handleGetInfoRefsWithGitaly(w, a, rpc) err = handleGetInfoRefsWithGitaly(r.Context(), w, a, rpc)
} }
if err != nil { if err != nil {
...@@ -64,14 +63,12 @@ func handleGetInfoRefsLocally(w http.ResponseWriter, a *api.Response, rpc string ...@@ -64,14 +63,12 @@ func handleGetInfoRefsLocally(w http.ResponseWriter, a *api.Response, rpc string
return nil return nil
} }
func handleGetInfoRefsWithGitaly(w http.ResponseWriter, a *api.Response, rpc string) error { func handleGetInfoRefsWithGitaly(ctx context.Context, w http.ResponseWriter, a *api.Response, rpc string) error {
smarthttp, err := gitaly.NewSmartHTTPClient(a.GitalyServer) smarthttp, err := gitaly.NewSmartHTTPClient(a.GitalyServer)
if err != nil { if err != nil {
return fmt.Errorf("GetInfoRefsHandler: %v", err) return fmt.Errorf("GetInfoRefsHandler: %v", err)
} }
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
infoRefsResponseReader, err := smarthttp.InfoRefsResponseReader(ctx, &a.Repository, rpc) infoRefsResponseReader, err := smarthttp.InfoRefsResponseReader(ctx, &a.Repository, rpc)
if err != nil { if err != nil {
return fmt.Errorf("GetInfoRefsHandler: %v", err) return fmt.Errorf("GetInfoRefsHandler: %v", err)
......
package git package git
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
...@@ -23,7 +24,7 @@ func handleReceivePack(w *GitHttpResponseWriter, r *http.Request, a *api.Respons ...@@ -23,7 +24,7 @@ func handleReceivePack(w *GitHttpResponseWriter, r *http.Request, a *api.Respons
if a.GitalyServer.Address == "" { if a.GitalyServer.Address == "" {
err = handleReceivePackLocally(a, r, cr, cw, action) err = handleReceivePackLocally(a, r, cr, cw, action)
} else { } else {
err = handleReceivePackWithGitaly(a, cr, cw) err = handleReceivePackWithGitaly(r.Context(), a, cr, cw)
} }
return err return err
...@@ -45,13 +46,13 @@ func handleReceivePackLocally(a *api.Response, r *http.Request, stdin io.Reader, ...@@ -45,13 +46,13 @@ func handleReceivePackLocally(a *api.Response, r *http.Request, stdin io.Reader,
return nil return nil
} }
func handleReceivePackWithGitaly(a *api.Response, clientRequest io.Reader, clientResponse io.Writer) error { func handleReceivePackWithGitaly(ctx context.Context, a *api.Response, clientRequest io.Reader, clientResponse io.Writer) error {
smarthttp, err := gitaly.NewSmartHTTPClient(a.GitalyServer) smarthttp, err := gitaly.NewSmartHTTPClient(a.GitalyServer)
if err != nil { if err != nil {
return fmt.Errorf("smarthttp.ReceivePack: %v", err) return fmt.Errorf("smarthttp.ReceivePack: %v", err)
} }
if err := smarthttp.ReceivePack(&a.Repository, a.GL_ID, a.GL_REPOSITORY, clientRequest, clientResponse); err != nil { if err := smarthttp.ReceivePack(ctx, &a.Repository, a.GL_ID, a.GL_REPOSITORY, clientRequest, clientResponse); err != nil {
return fmt.Errorf("smarthttp.ReceivePack: %v", err) return fmt.Errorf("smarthttp.ReceivePack: %v", err)
} }
......
package git package git
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
...@@ -30,7 +31,7 @@ func handleUploadPack(w *GitHttpResponseWriter, r *http.Request, a *api.Response ...@@ -30,7 +31,7 @@ func handleUploadPack(w *GitHttpResponseWriter, r *http.Request, a *api.Response
if a.GitalyServer.Address == "" { if a.GitalyServer.Address == "" {
err = handleUploadPackLocally(a, r, buffer, w, action) err = handleUploadPackLocally(a, r, buffer, w, action)
} else { } else {
err = handleUploadPackWithGitaly(a, buffer, w) err = handleUploadPackWithGitaly(r.Context(), a, buffer, w)
} }
return err return err
...@@ -57,13 +58,13 @@ func handleUploadPackLocally(a *api.Response, r *http.Request, stdin *os.File, s ...@@ -57,13 +58,13 @@ func handleUploadPackLocally(a *api.Response, r *http.Request, stdin *os.File, s
return nil return nil
} }
func handleUploadPackWithGitaly(a *api.Response, clientRequest io.Reader, clientResponse io.Writer) error { func handleUploadPackWithGitaly(ctx context.Context, a *api.Response, clientRequest io.Reader, clientResponse io.Writer) error {
smarthttp, err := gitaly.NewSmartHTTPClient(a.GitalyServer) smarthttp, err := gitaly.NewSmartHTTPClient(a.GitalyServer)
if err != nil { if err != nil {
return fmt.Errorf("smarthttp.UploadPack: %v", err) return fmt.Errorf("smarthttp.UploadPack: %v", err)
} }
if err := smarthttp.UploadPack(&a.Repository, clientRequest, clientResponse); err != nil { if err := smarthttp.UploadPack(ctx, &a.Repository, clientRequest, clientResponse); err != nil {
return fmt.Errorf("smarthttp.UploadPack: %v", err) return fmt.Errorf("smarthttp.UploadPack: %v", err)
} }
......
package gitaly package gitaly
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
...@@ -8,18 +9,13 @@ import ( ...@@ -8,18 +9,13 @@ import (
pb "gitlab.com/gitlab-org/gitaly-proto/go" pb "gitlab.com/gitlab-org/gitaly-proto/go"
"gitlab.com/gitlab-org/gitaly/streamio" "gitlab.com/gitlab-org/gitaly/streamio"
"golang.org/x/net/context"
) )
type CommitClient struct { type CommitClient struct {
pb.CommitClient pb.CommitClient
} }
func (client *CommitClient) SendBlob(w http.ResponseWriter, request *pb.TreeEntryRequest) error { func (client *CommitClient) SendBlob(ctx context.Context, w http.ResponseWriter, request *pb.TreeEntryRequest) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
c, err := client.TreeEntry(ctx, request) c, err := client.TreeEntry(ctx, request)
if err != nil { if err != nil {
return fmt.Errorf("rpc failed: %v", err) return fmt.Errorf("rpc failed: %v", err)
......
package gitaly package gitaly
import ( import (
"context"
"fmt" "fmt"
"io" "io"
pb "gitlab.com/gitlab-org/gitaly-proto/go" pb "gitlab.com/gitlab-org/gitaly-proto/go"
"gitlab.com/gitlab-org/gitaly/streamio" "gitlab.com/gitlab-org/gitaly/streamio"
"golang.org/x/net/context"
) )
type SmartHTTPClient struct { type SmartHTTPClient struct {
...@@ -40,10 +39,7 @@ func infoRefsReader(stream infoRefsClient) io.Reader { ...@@ -40,10 +39,7 @@ func infoRefsReader(stream infoRefsClient) io.Reader {
}) })
} }
func (client *SmartHTTPClient) ReceivePack(repo *pb.Repository, glId string, glRepository string, clientRequest io.Reader, clientResponse io.Writer) error { func (client *SmartHTTPClient) ReceivePack(ctx context.Context, repo *pb.Repository, glId string, glRepository string, clientRequest io.Reader, clientResponse io.Writer) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
stream, err := client.PostReceivePack(ctx) stream, err := client.PostReceivePack(ctx)
if err != nil { if err != nil {
return err return err
...@@ -89,10 +85,7 @@ func (client *SmartHTTPClient) ReceivePack(repo *pb.Repository, glId string, glR ...@@ -89,10 +85,7 @@ func (client *SmartHTTPClient) ReceivePack(repo *pb.Repository, glId string, glR
return nil return nil
} }
func (client *SmartHTTPClient) UploadPack(repo *pb.Repository, clientRequest io.Reader, clientResponse io.Writer) error { func (client *SmartHTTPClient) UploadPack(ctx context.Context, repo *pb.Repository, clientRequest io.Reader, clientResponse io.Writer) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
stream, err := client.PostUploadPack(ctx) stream, err := client.PostUploadPack(ctx)
if err != nil { if err != nil {
return err return err
......
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