Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sync.Map to cache base image lookups #712

Merged
merged 1 commit into from
May 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"time"

ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login"
Expand Down Expand Up @@ -55,7 +56,7 @@ var (

// getBaseImage returns a function that determines the base image for a given import path.
func getBaseImage(bo *options.BuildOptions) build.GetBase {
cache := map[string]build.Result{}
var cache sync.Map
fetch := func(ctx context.Context, ref name.Reference) (build.Result, error) {
// For ko.local, look in the daemon.
if ref.Context().RegistryStr() == publish.LocalDomain {
Expand Down Expand Up @@ -102,8 +103,8 @@ func getBaseImage(bo *options.BuildOptions) build.GetBase {
return nil, nil, fmt.Errorf("parsing base image (%q): %w", baseImage, err)
}

if cached, ok := cache[ref.String()]; ok {
return ref, cached, nil
if v, ok := cache.Load(ref.String()); ok {
return ref, v.(build.Result), nil
}

result, err := fetch(ctx, ref)
Expand All @@ -121,7 +122,7 @@ func getBaseImage(bo *options.BuildOptions) build.GetBase {
log.Printf("Using base %s@%s for %s", ref, dig, s)
}

cache[ref.String()] = result
cache.Store(ref.String(), result)
return ref, result, nil
}
}
Expand Down