Skip to content
Permalink
Browse files

internal/code: add a timeout for git operations

We expect normal git operations to complete well within 45 seconds.
Add the timeout as a sanity check. It can help in a situation where
the server is overloaded and some git operations take too long to
complete.
  • Loading branch information...
dmitshur committed May 6, 2019
1 parent b8c8010 commit 1c38c563509be2a81f27e658317c8e9161619cd9
Showing with 17 additions and 4 deletions.
  1. +17 −4 internal/code/git.go
@@ -2,6 +2,7 @@ package code

import (
"bytes"
"context"
"fmt"
"io"
"log"
@@ -24,6 +25,10 @@ import (
"sourcegraph.com/sourcegraph/go-vcs/vcs/gitcmd"
)

const (
gitTimeout = 45 * time.Second
)

// NewGitHandler creates a gitHandler.
// TODO: Consider moving it into Service.
func NewGitHandler(code *Service, reposDir string, events events.ExternalService, users users.Service, gitUsers map[string]users.User, authenticate func(*http.Request) *http.Request) (*gitHandler, error) {
@@ -118,7 +123,9 @@ func (h *gitHandler) serveGitInfoRefsUploadPack(w http.ResponseWriter, req *http
httperror.HandleMethod(w, httperror.Method{Allowed: []string{http.MethodGet}})
return
}
cmd := exec.CommandContext(req.Context(), h.gitUploadPack, "--strict", "--advertise-refs", ".")
ctx, cancel := context.WithTimeout(req.Context(), gitTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, h.gitUploadPack, "--strict", "--advertise-refs", ".")
cmd.Dir = repo.Dir
if v := req.Header.Get("Git-Protocol"); v != "" {
env := osutil.Environ(os.Environ())
@@ -162,7 +169,9 @@ func (h *gitHandler) serveGitUploadPack(w http.ResponseWriter, req *http.Request
httperror.HandleBadRequest(w, httperror.BadRequest{Err: err})
return
}
cmd := exec.CommandContext(req.Context(), h.gitUploadPack, "--strict", "--stateless-rpc", ".")
ctx, cancel := context.WithTimeout(req.Context(), gitTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, h.gitUploadPack, "--strict", "--stateless-rpc", ".")
cmd.Dir = repo.Dir
if v := req.Header.Get("Git-Protocol"); v != "" {
env := osutil.Environ(os.Environ())
@@ -220,7 +229,9 @@ func (h *gitHandler) serveGitInfoRefsReceivePack(w http.ResponseWriter, req *htt
return
}

cmd := exec.CommandContext(req.Context(), h.gitReceivePack, "--advertise-refs", ".")
ctx, cancel := context.WithTimeout(req.Context(), gitTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, h.gitReceivePack, "--advertise-refs", ".")
cmd.Dir = repo.Dir
if v := req.Header.Get("Git-Protocol"); v != "" {
env := osutil.Environ(os.Environ())
@@ -283,7 +294,9 @@ func (h *gitHandler) serveGitReceivePack(w http.ResponseWriter, req *http.Reques
return
}

cmd := exec.CommandContext(req.Context(), h.gitReceivePack, "--stateless-rpc", ".")
ctx, cancel := context.WithTimeout(req.Context(), gitTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, h.gitReceivePack, "--stateless-rpc", ".")
cmd.Dir = repo.Dir
if v := req.Header.Get("Git-Protocol"); v != "" {
env := osutil.Environ(os.Environ())

0 comments on commit 1c38c56

Please sign in to comment.
You can’t perform that action at this time.