Skip to content

Commit

Permalink
Use sync.Map to cache base image lookups (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh authored May 25, 2022
1 parent 85538a6 commit 691da5a
Showing 1 changed file with 5 additions and 4 deletions.
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

0 comments on commit 691da5a

Please sign in to comment.