Skip to content

Commit

Permalink
Pass context to httpGet (#2)
Browse files Browse the repository at this point in the history
This should fix a bug where the context is getting canceled immediately
because of the defered `cancel()` within the helper function.
  • Loading branch information
hryx authored Jun 25, 2024
1 parent d06eafd commit 42798b6
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions internal/wrench/http_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ func (b *Bot) httpPkgEnsureZigDownloadCached(version, versionKind, fname string)
}
fmt.Fprintf(logWriter, "fetch: %s > %s\n", url, filePath)

resp, err := httpGet(url, 60*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
resp, err := httpGet(ctx, url)
if err != nil {
return errors.Wrap(err, "Get")
}
Expand Down Expand Up @@ -634,7 +636,9 @@ func (b *Bot) httpPkgZigIndexCached() ([]byte, error) {
}

// Fetch the latest upstream Zig index.json
resp, err := httpGet("https://ziglang.org/download/index.json", 60*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
resp, err := httpGet(ctx, "https://ziglang.org/download/index.json")
if err != nil {
return nil, errors.Wrap(err, "fetching upstream https://ziglang.org/download/index.json")
}
Expand All @@ -646,7 +650,9 @@ func (b *Bot) httpPkgZigIndexCached() ([]byte, error) {

// Fetch the Mach index.json which contains Mach nominated versions, but is otherwise not as
// up-to-date as ziglang.org's version.
resp, err = httpGet("https://machengine.org/zig/index.json", 60*time.Second)
ctx, cancel = context.WithTimeout(context.Background(), time.Minute)
defer cancel()
resp, err = httpGet(ctx, "https://machengine.org/zig/index.json")
if err != nil {
return nil, errors.Wrap(err, "fetching mach https://machengine.org/zig/index.json")
}
Expand Down Expand Up @@ -714,9 +720,7 @@ func (b *Bot) httpPkgZigIndexCached() ([]byte, error) {
}

// Like http.Get, but actually respects a timeout instead of leaking a goroutine to forever run.
func httpGet(url string, timeout time.Duration) (*http.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
func httpGet(ctx context.Context, url string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
Expand Down

0 comments on commit 42798b6

Please sign in to comment.