@@ -17,6 +17,7 @@ limitations under the License.
1717package golang
1818
1919import (
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