Skip to content

Commit 42798b6

Browse files
authored
Pass context to httpGet (#2)
This should fix a bug where the context is getting canceled immediately because of the defered `cancel()` within the helper function.
1 parent d06eafd commit 42798b6

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

internal/wrench/http_pkg.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ func (b *Bot) httpPkgEnsureZigDownloadCached(version, versionKind, fname string)
416416
}
417417
fmt.Fprintf(logWriter, "fetch: %s > %s\n", url, filePath)
418418

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

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

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

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

0 commit comments

Comments
 (0)