Skip to content

Commit bf27e27

Browse files
committed
buildlet: ensure buildlet client creation timeout is correct
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Alexander Rakoczy <[email protected]>
1 parent ae04491 commit bf27e27

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

buildlet/buildlet.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ package buildlet
77
import (
88
"context"
99
"crypto/tls"
10-
"errors"
1110
"fmt"
1211
"io/ioutil"
12+
"log"
1313
"net/http"
1414
"time"
1515

@@ -80,25 +80,24 @@ type VMOpts struct {
8080
// buildletClient returns a buildlet client configured to speak to a VM via the buildlet
8181
// URL. The communication will use TLS if one is provided in the vmopts. This will wait until
8282
// it can connect with the endpoint before returning. The buildletURL is in the form of:
83-
// "https://<ip>". The ipPort field is in the form of "<ip>:<port>".
83+
// "https://<ip>". The ipPort field is in the form of "<ip>:<port>". The function
84+
// will attempt to connect to the buildlet for the lesser of: the default timeout period
85+
// (5 minutes) or the timeout set in the passed in context.
8486
func buildletClient(ctx context.Context, buildletURL, ipPort string, opts *VMOpts) (*Client, error) {
85-
const timeout = 5 * time.Minute
86-
deadline := time.Now().Add(timeout)
87+
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
88+
defer cancel()
8789
try := 0
8890
for !opts.SkipEndpointVerification {
8991
try++
90-
if deadline.Before(time.Now()) {
91-
return nil, fmt.Errorf("unable to probe buildet at %s in %v with %d attempts", buildletURL, timeout, try)
92+
if ctx.Err() != nil {
93+
return nil, fmt.Errorf("unable to probe buildet at %s after %d attempts", buildletURL, try)
9294
}
9395
err := probeBuildlet(ctx, buildletURL, opts)
94-
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
95-
return nil, fmt.Errorf("unable to probe buildlet at %s: %w", buildletURL, err)
96-
}
97-
if err != nil {
98-
time.Sleep(time.Second)
99-
continue
96+
if err == nil {
97+
break
10098
}
101-
break
99+
log.Printf("probing buildlet at %s with attempt %d failed: %s", buildletURL, try, err)
100+
time.Sleep(time.Second)
102101
}
103102
return NewClient(ipPort, opts.TLS), nil
104103
}

buildlet/buildlet_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ func TestBuildletClientError(t *testing.T) {
9191
if httpCalled {
9292
t.Error("http endpoint called")
9393
}
94-
if !OnBeginBuildletProbeCalled {
94+
if OnBeginBuildletProbeCalled {
9595
t.Error("OnBeginBuildletProbe() was not called")
9696
}
97-
if !OnEndBuildletProbeCalled {
97+
if OnEndBuildletProbeCalled {
9898
t.Error("OnEndBuildletProbe() was not called")
9999
}
100100
}

0 commit comments

Comments
 (0)