Skip to content

Commit 592ad68

Browse files
committed
add context to InstallGoVersions and http get
1 parent f56dec6 commit 592ad68

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

cmd/publishing-bot/publisher.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"bytes"
21+
"context"
2122
"errors"
2223
"fmt"
2324
"os"
@@ -228,7 +229,7 @@ func (p *PublisherMunger) runSmokeTests(smokeTest, oldHead, newHead string, bran
228229
func (p *PublisherMunger) construct() error {
229230
sourceRemote := filepath.Join(p.baseRepoPath, p.config.SourceRepo, ".git")
230231

231-
if err := golang.InstallGoVersions(&p.reposRules); err != nil {
232+
if err := golang.InstallGoVersions(context.Background(), &p.reposRules); err != nil {
232233
return err
233234
}
234235

pkg/golang/install.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package golang
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"io"
2223
"net/http"
@@ -32,7 +33,7 @@ import (
3233

3334
// InstallGoVersions download and unpacks the specified Golang versions to $GOPATH/
3435
// If the DefaultGoVersion is not specfied in rules, it defaults to the current Go release.
35-
func InstallGoVersions(rules *config.RepositoryRules) error {
36+
func InstallGoVersions(ctx context.Context, rules *config.RepositoryRules) error {
3637
if rules == nil {
3738
return nil
3839
}
@@ -41,15 +42,15 @@ func InstallGoVersions(rules *config.RepositoryRules) error {
4142
//
4243
// Any version > 1.21 that supports GOTOOLCHAIN can automatically
4344
// fetch the correct go version for a given module if not otherwise overridden.
44-
defaultGoVersion := ""
45+
var defaultGoVersion string
4546
if rules.DefaultGoVersion != nil {
4647
defaultGoVersion = *rules.DefaultGoVersion
4748
} else {
4849
// NOTE: we only do this in the else block, so if the rules explicitly
4950
// specify a default, they do not depend on this endpoint
5051
// That means if we ever have issues with getCurrentGoRelease, a quick
5152
// fix is just setting the default again.
52-
v, err := getCurrentGoRelease()
53+
v, err := getCurrentGoRelease(ctx)
5354
if err != nil {
5455
return err
5556
}
@@ -118,18 +119,26 @@ func installGoVersion(v, pth string) error {
118119
return os.Rename(tmpPath, pth)
119120
}
120121

121-
func getCurrentGoRelease() (string, error) {
122+
func getCurrentGoRelease(ctx context.Context) (string, error) {
122123
var resp *http.Response
123124
var err error
124125
for i := 0; i < 3; i++ {
125-
resp, err = http.Get("https://go.dev/VERSION?m=text")
126+
req, reqErr := http.NewRequestWithContext(ctx, "GET", "https://go.dev/VERSION?m=text", nil)
127+
if reqErr != nil {
128+
return "", reqErr
129+
}
130+
resp, err = http.DefaultClient.Do(req)
126131
if err == nil && resp.StatusCode == http.StatusOK {
127132
break
128133
}
129134
if resp != nil {
130135
resp.Body.Close()
131136
}
132-
time.Sleep(time.Second)
137+
select {
138+
case <-ctx.Done():
139+
return "", ctx.Err()
140+
case <-time.After(time.Second):
141+
}
133142
}
134143
if err != nil {
135144
return "", err

0 commit comments

Comments
 (0)