From 821f6578081ede06de3c12177ccd258336a365da Mon Sep 17 00:00:00 2001 From: Grant Linville Date: Thu, 27 Jun 2024 09:39:51 -0400 Subject: [PATCH 1/3] fix: only check cred helper repo once per 24h Signed-off-by: Grant Linville --- pkg/credentials/util.go | 9 +++++---- pkg/repos/get.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/credentials/util.go b/pkg/credentials/util.go index cf3291d7..f6165308 100644 --- a/pkg/credentials/util.go +++ b/pkg/credentials/util.go @@ -5,13 +5,14 @@ import ( ) type CredentialHelperDirs struct { - RevisionFile, BinDir, RepoDir string + RevisionFile, LastCheckedFile, BinDir, RepoDir string } func GetCredentialHelperDirs(cacheDir string) CredentialHelperDirs { return CredentialHelperDirs{ - RevisionFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "revision"), - BinDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "bin"), - RepoDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "repo"), + RevisionFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "revision"), + LastCheckedFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "last-checked"), + BinDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "bin"), + RepoDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "repo"), } } diff --git a/pkg/repos/get.go b/pkg/repos/get.go index 68b2e427..09fc8f31 100644 --- a/pkg/repos/get.go +++ b/pkg/repos/get.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" "sync" + "time" "github.com/BurntSushi/locker" "github.com/gptscript-ai/gptscript/pkg/config" @@ -112,11 +113,29 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co locker.Lock("gptscript-credential-helpers") defer locker.Unlock("gptscript-credential-helpers") + // Load the last-checked file to make sure we haven't checked the repo in the last 24 hours. + lastChecked, err := os.ReadFile(m.credHelperDirs.LastCheckedFile) + if err == nil { + if t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(lastChecked))); err == nil && time.Since(t) < 24*time.Hour { + // Make sure the binary still exists, and if it does, return. + if _, err := os.Stat(filepath.Join(m.credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil { + log.Debugf("Not checking for new version of credential helper %s, last checked %v", helperName, t) + return nil + } + } + } + + // Load the credential helpers repo information. _, repo, _, err := github.Load(ctx, nil, credentialHelpersRepo) if err != nil { return err } + // Update the last-checked file. + if err := os.WriteFile(m.credHelperDirs.LastCheckedFile, []byte(time.Now().Format(time.RFC3339)), 0644); err != nil { + return err + } + var needsBuild bool // Check the last revision shasum and see if it is different from the current one. From 28a4174c0107b1f076ffb028e626365f8f18ec71 Mon Sep 17 00:00:00 2001 From: Grant Linville Date: Thu, 27 Jun 2024 10:40:31 -0400 Subject: [PATCH 2/3] Update pkg/repos/get.go Co-authored-by: Nick Hale <4175918+njhale@users.noreply.github.com> Signed-off-by: Grant Linville --- pkg/repos/get.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/repos/get.go b/pkg/repos/get.go index 09fc8f31..85762bae 100644 --- a/pkg/repos/get.go +++ b/pkg/repos/get.go @@ -119,7 +119,7 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co if t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(lastChecked))); err == nil && time.Since(t) < 24*time.Hour { // Make sure the binary still exists, and if it does, return. if _, err := os.Stat(filepath.Join(m.credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil { - log.Debugf("Not checking for new version of credential helper %s, last checked %v", helperName, t) + log.Debugf("Credential helper %s up-to-date as of %v, checking for updates after %v", helperName, t, lastChecked.Add(24*time.Hour)) return nil } } From c09da159e18a39334e8844258ba486d0eb2cd438 Mon Sep 17 00:00:00 2001 From: Grant Linville Date: Thu, 27 Jun 2024 10:42:47 -0400 Subject: [PATCH 3/3] PR feedback Signed-off-by: Grant Linville --- pkg/repos/get.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/repos/get.go b/pkg/repos/get.go index 85762bae..b3d5f609 100644 --- a/pkg/repos/get.go +++ b/pkg/repos/get.go @@ -114,12 +114,13 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co defer locker.Unlock("gptscript-credential-helpers") // Load the last-checked file to make sure we haven't checked the repo in the last 24 hours. + now := time.Now() lastChecked, err := os.ReadFile(m.credHelperDirs.LastCheckedFile) if err == nil { - if t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(lastChecked))); err == nil && time.Since(t) < 24*time.Hour { + if t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(lastChecked))); err == nil && now.Sub(t) < 24*time.Hour { // Make sure the binary still exists, and if it does, return. if _, err := os.Stat(filepath.Join(m.credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil { - log.Debugf("Credential helper %s up-to-date as of %v, checking for updates after %v", helperName, t, lastChecked.Add(24*time.Hour)) + log.Debugf("Credential helper %s up-to-date as of %v, checking for updates after %v", helperName, t, t.Add(24*time.Hour)) return nil } } @@ -132,7 +133,7 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co } // Update the last-checked file. - if err := os.WriteFile(m.credHelperDirs.LastCheckedFile, []byte(time.Now().Format(time.RFC3339)), 0644); err != nil { + if err := os.WriteFile(m.credHelperDirs.LastCheckedFile, []byte(now.Format(time.RFC3339)), 0644); err != nil { return err }