From 603c2cb77d2d01a52b087b1c60df483a2e889670 Mon Sep 17 00:00:00 2001 From: Steve Ramage Date: Mon, 2 Oct 2023 12:45:26 -0700 Subject: [PATCH] Resolves #380 - Warn if alias loading or saving is slow --- cmd/create.go | 7 ++++--- external/aliases/aliases.go | 33 +++++++++++++++++++++++++++++++++ external/json/print_json.go | 2 ++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index e048550..a9c0d58 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -68,9 +68,10 @@ func NewCreateCommand(parentCmd *cobra.Command) func() { resourceName := resource.SingularName var createResourceCmd = &cobra.Command{ - Use: GetCreateUsageString(resource), - Short: GetCreateShort(resource), - Long: GetCreateLong(resource), + Use: GetCreateUsageString(resource), + Short: GetCreateShort(resource), + Long: GetCreateLong(resource), + Example: GetCreateExample(resource), Args: GetArgFunctionForCreate(resource), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/external/aliases/aliases.go b/external/aliases/aliases.go index 3011392..67d43db 100644 --- a/external/aliases/aliases.go +++ b/external/aliases/aliases.go @@ -15,6 +15,7 @@ import ( "strconv" "strings" "sync" + "time" ) var mutex = &sync.RWMutex{} @@ -72,6 +73,20 @@ func getAliasesForSingleJsonApiType(jsonApiType string) map[string]*id.IdableAtt mutex.RUnlock() mutex.Lock() defer mutex.Unlock() + + done := make(chan bool, 1) + + defer close(done) + + go func() { + select { + case <-done: + break + case <-time.After(2 * time.Second): + log.Warnf("Loading of aliases for %s has taken more than 2 seconds, if you don't need aliases consider the --skip-alias-processing argument. You can also clear aliases by using `epcc aliases clear `", jsonApiType) + } + }() + aliasFile := getAliasFileForJsonApiType(getAliasDataDirectory(), jsonApiType) aliasMap = map[string]*id.IdableAttributes{} @@ -105,6 +120,8 @@ func getAliasesForSingleJsonApiType(jsonApiType string) map[string]*id.IdableAtt log.Tracef("Aliases for type [%s] loaded, with %d aliases", jsonApiType, len(aliasMap)) } + done <- true + } else { mutex.RUnlock() } @@ -533,6 +550,18 @@ func FlushAliases() int { } func SyncAliases() int { + done := make(chan bool, 1) + + defer close(done) + + go func() { + select { + case <-done: + break + case <-time.After(1 * time.Second): + log.Warnf("Saving of aliases took longer than 1 seconds, if you don't need aliases consider the --skip-alias-processing argument. You can also clear aliases by using `epcc aliases clear ") + } + }() syncedFiles := 0 mutex.RLock() @@ -553,6 +582,9 @@ func SyncAliases() int { // https://github.com/golang/go/issues/20599 tmpFileName := aliasFile + "." + uuid.New().String() + if len(aliasesForType) > 10000 { + log.Warnf("There are more than 10,000 aliases for type %s, you may notice a slow down when using epcc. If you don't need aliases consider the --skip-alias-processing argument. You can also clear aliases by using `epcc aliases clear ", jsonApiType) + } marshal, err := yaml.Marshal(aliasesForType) if err != nil { @@ -579,6 +611,7 @@ func SyncAliases() int { log.Debugf("Syncing aliases to disk, %d files changed", syncedFiles) + done <- true return syncedFiles } diff --git a/external/json/print_json.go b/external/json/print_json.go index 91e39c8..0b5a8f8 100644 --- a/external/json/print_json.go +++ b/external/json/print_json.go @@ -51,6 +51,8 @@ func printJsonToWriter(json string, w io.Writer) error { done := make(chan bool, 1) + defer close(done) + if !MonochromeOutput { go func() { select {