Skip to content

Commit

Permalink
builder: increase git check timeout exponentially
Browse files Browse the repository at this point in the history
The check will either succeed or the build will timeout
  • Loading branch information
csrwng committed Jun 2, 2016
1 parent f4e025b commit 2902d08
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pkg/build/builder/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewDockerBuilder(dockerClient DockerClient, buildsClient client.BuildInterf
build: build,
gitClient: gitClient,
tar: tar.New(),
urlTimeout: urlCheckTimeout,
urlTimeout: initialURLCheckTimeout,
client: buildsClient,
cgLimits: cgLimits,
}
Expand Down
60 changes: 36 additions & 24 deletions pkg/build/builder/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ import (
)

const (
// urlCheckTimeout is the timeout used to check the source URL
// If fetching the URL exceeds the timeout, then the build will
// not proceed further and stop
urlCheckTimeout = 16 * time.Second
// initialURLCheckTimeout is the initial timeout used to check the
// source URL. If fetching the URL exceeds the timeout, then a longer
// timeout will be tried until the fetch either succeeds or the build
// itself times out.
initialURLCheckTimeout = 16 * time.Second

// timeoutIncrementFactor is the factor to use when increasing
// the timeout after each unsuccessful try
timeoutIncrementFactor = 4
)

type gitAuthError string
Expand Down Expand Up @@ -104,35 +109,42 @@ func fetchSource(dockerClient DockerClient, dir string, build *api.Build, urlTim
// remote repository failed to authenticate.
// Since this is calling the 'git' binary, the proxy settings should be
// available for this command.
func checkRemoteGit(gitClient GitClient, url string, timeout time.Duration) error {
glog.V(4).Infof("git ls-remote --heads %s", url)
func checkRemoteGit(gitClient GitClient, url string, initialTimeout time.Duration) error {

var (
out string
errOut string
err error
)

out, errOut, err = gitClient.TimedListRemote(timeout, url, "--heads")
if _, ok := err.(*git.TimeoutError); err != nil && ok {
return fmt.Errorf("timeout while waiting for remote repository %q", url)
}

if len(out) != 0 {
glog.V(4).Infof(out)
}
if len(errOut) != 0 {
glog.V(4).Infof(errOut)
timeout := initialTimeout
for {
glog.V(4).Infof("git ls-remote --heads %s", url)
out, errOut, err = gitClient.TimedListRemote(timeout, url, "--heads")
if len(out) != 0 {
glog.V(4).Infof(out)
}
if len(errOut) != 0 {
glog.V(4).Infof(errOut)
}
if err != nil {
if _, ok := err.(*git.TimeoutError); ok {
timeout = timeout * timeoutIncrementFactor
glog.Infof("WARNING: timed out waiting for git server, will wait %s", timeout)
continue
}
}
break
}

combinedOut := out + errOut
switch {
case strings.Contains(combinedOut, "Authentication failed"):
return gitAuthError(url)
case strings.Contains(combinedOut, "not found"):
return gitNotFoundError(url)
if err != nil {
combinedOut := out + errOut
switch {
case strings.Contains(combinedOut, "Authentication failed"):
return gitAuthError(url)
case strings.Contains(combinedOut, "not found"):
return gitNotFoundError(url)
}
}

return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/build/builder/sti.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s *S2IBuilder) Build() error {
download := &downloader{
s: s,
in: os.Stdin,
timeout: urlCheckTimeout,
timeout: initialURLCheckTimeout,

dir: srcDir,
contextDir: contextDir,
Expand Down

0 comments on commit 2902d08

Please sign in to comment.