Skip to content

Commit

Permalink
Merge pull request #942 from AtakanColak/patch-1
Browse files Browse the repository at this point in the history
Cache common/common_linux.Virtualization()
  • Loading branch information
shirou authored Oct 1, 2020
2 parents 8ece829 + ba243b1 commit 5084874
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/common/common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ func Virtualization() (string, string, error) {
return VirtualizationWithContext(context.Background())
}

var virtualizationCache map[string]string

func VirtualizationWithContext(ctx context.Context) (string, string, error) {
// if cached already, return from cache
if virtualizationCache != nil {
return virtualizationCache["system"], virtualizationCache["role"], nil
}

var system string
var role string

Expand Down Expand Up @@ -231,6 +238,13 @@ func VirtualizationWithContext(ctx context.Context) (string, string, error) {
role = "host"
}
}

// before returning for the first time, cache the system and role
virtualizationCache = map[string]string{
"system": system,
"role": role,
}

return system, role, nil
}

Expand Down

0 comments on commit 5084874

Please sign in to comment.