Skip to content

Commit 51fd2b3

Browse files
g-linvillenjhale
andauthored
fix: only check cred helper repo once per 24h (#572)
Signed-off-by: Grant Linville <[email protected]> Co-authored-by: Nick Hale <[email protected]>
1 parent c8b865a commit 51fd2b3

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

pkg/credentials/util.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55
)
66

77
type CredentialHelperDirs struct {
8-
RevisionFile, BinDir, RepoDir string
8+
RevisionFile, LastCheckedFile, BinDir, RepoDir string
99
}
1010

1111
func GetCredentialHelperDirs(cacheDir string) CredentialHelperDirs {
1212
return CredentialHelperDirs{
13-
RevisionFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "revision"),
14-
BinDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "bin"),
15-
RepoDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "repo"),
13+
RevisionFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "revision"),
14+
LastCheckedFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "last-checked"),
15+
BinDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "bin"),
16+
RepoDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "repo"),
1617
}
1718
}

pkg/repos/get.go

+20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"strings"
1212
"sync"
13+
"time"
1314

1415
"github.com/BurntSushi/locker"
1516
"github.com/gptscript-ai/gptscript/pkg/config"
@@ -112,11 +113,30 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
112113
locker.Lock("gptscript-credential-helpers")
113114
defer locker.Unlock("gptscript-credential-helpers")
114115

116+
// Load the last-checked file to make sure we haven't checked the repo in the last 24 hours.
117+
now := time.Now()
118+
lastChecked, err := os.ReadFile(m.credHelperDirs.LastCheckedFile)
119+
if err == nil {
120+
if t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(lastChecked))); err == nil && now.Sub(t) < 24*time.Hour {
121+
// Make sure the binary still exists, and if it does, return.
122+
if _, err := os.Stat(filepath.Join(m.credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil {
123+
log.Debugf("Credential helper %s up-to-date as of %v, checking for updates after %v", helperName, t, t.Add(24*time.Hour))
124+
return nil
125+
}
126+
}
127+
}
128+
129+
// Load the credential helpers repo information.
115130
_, repo, _, err := github.Load(ctx, nil, credentialHelpersRepo)
116131
if err != nil {
117132
return err
118133
}
119134

135+
// Update the last-checked file.
136+
if err := os.WriteFile(m.credHelperDirs.LastCheckedFile, []byte(now.Format(time.RFC3339)), 0644); err != nil {
137+
return err
138+
}
139+
120140
var needsBuild bool
121141

122142
// Check the last revision shasum and see if it is different from the current one.

0 commit comments

Comments
 (0)