Skip to content

Commit

Permalink
buildlet: ensure buildlet client creation timeout is correct
Browse files Browse the repository at this point in the history
The buildlet client creation will either timeout at the current
default timeout period or the timeout set in the context. The
existing effective timeout is the five seconds set in the
probeBuildlet function.

Fixes golang/go#38956
Updates golang/go#36841

Change-Id: I09e96e2c5abcc45ccd535596104c52998ddb0d7a
Reviewed-on: https://go-review.googlesource.com/c/build/+/232997
Run-TryBot: Carlos Amedee <carlos@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
  • Loading branch information
cagedmantis committed May 8, 2020
1 parent ae04491 commit bf27e27
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
25 changes: 12 additions & 13 deletions buildlet/buildlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package buildlet
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"

Expand Down Expand Up @@ -80,25 +80,24 @@ type VMOpts struct {
// buildletClient returns a buildlet client configured to speak to a VM via the buildlet
// URL. The communication will use TLS if one is provided in the vmopts. This will wait until
// it can connect with the endpoint before returning. The buildletURL is in the form of:
// "https://<ip>". The ipPort field is in the form of "<ip>:<port>".
// "https://<ip>". The ipPort field is in the form of "<ip>:<port>". The function
// will attempt to connect to the buildlet for the lesser of: the default timeout period
// (5 minutes) or the timeout set in the passed in context.
func buildletClient(ctx context.Context, buildletURL, ipPort string, opts *VMOpts) (*Client, error) {
const timeout = 5 * time.Minute
deadline := time.Now().Add(timeout)
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
try := 0
for !opts.SkipEndpointVerification {
try++
if deadline.Before(time.Now()) {
return nil, fmt.Errorf("unable to probe buildet at %s in %v with %d attempts", buildletURL, timeout, try)
if ctx.Err() != nil {
return nil, fmt.Errorf("unable to probe buildet at %s after %d attempts", buildletURL, try)
}
err := probeBuildlet(ctx, buildletURL, opts)
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
return nil, fmt.Errorf("unable to probe buildlet at %s: %w", buildletURL, err)
}
if err != nil {
time.Sleep(time.Second)
continue
if err == nil {
break
}
break
log.Printf("probing buildlet at %s with attempt %d failed: %s", buildletURL, try, err)
time.Sleep(time.Second)
}
return NewClient(ipPort, opts.TLS), nil
}
Expand Down
4 changes: 2 additions & 2 deletions buildlet/buildlet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ func TestBuildletClientError(t *testing.T) {
if httpCalled {
t.Error("http endpoint called")
}
if !OnBeginBuildletProbeCalled {
if OnBeginBuildletProbeCalled {
t.Error("OnBeginBuildletProbe() was not called")
}
if !OnEndBuildletProbeCalled {
if OnEndBuildletProbeCalled {
t.Error("OnEndBuildletProbe() was not called")
}
}
Expand Down

0 comments on commit bf27e27

Please sign in to comment.