@@ -7,9 +7,9 @@ package buildlet
7
7
import (
8
8
"context"
9
9
"crypto/tls"
10
- "errors"
11
10
"fmt"
12
11
"io/ioutil"
12
+ "log"
13
13
"net/http"
14
14
"time"
15
15
@@ -80,25 +80,24 @@ type VMOpts struct {
80
80
// buildletClient returns a buildlet client configured to speak to a VM via the buildlet
81
81
// URL. The communication will use TLS if one is provided in the vmopts. This will wait until
82
82
// 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.
84
86
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 ( )
87
89
try := 0
88
90
for ! opts .SkipEndpointVerification {
89
91
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 )
92
94
}
93
95
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
100
98
}
101
- break
99
+ log .Printf ("probing buildlet at %s with attempt %d failed: %s" , buildletURL , try , err )
100
+ time .Sleep (time .Second )
102
101
}
103
102
return NewClient (ipPort , opts .TLS ), nil
104
103
}
0 commit comments