From fc7a398d97a7ce8e557604de08b3ca2062d09c28 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Tue, 1 Oct 2024 09:12:45 -0700 Subject: [PATCH] [plugins] Add env var to control github cache ttl (#2314) ## Summary This adds new experimental env var `DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL` that controls how long devbox caches github plugins. The default is 24h. Ideally we store plugin versions in lockfile, but this can help improve UX in the meantime. ## How was it tested? Added print statement to monitor network requests ```bash DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL=0 devbox run echo hello DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL=1s devbox run echo hello DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL=1h devbox run echo hello ``` --- internal/plugin/github.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/internal/plugin/github.go b/internal/plugin/github.go index 972b815f731..f47dd3b7a75 100644 --- a/internal/plugin/github.go +++ b/internal/plugin/github.go @@ -69,8 +69,28 @@ func (p *githubPlugin) FileContent(subpath string) ([]byte, error) { if err != nil { return nil, err } + + // Cache for 24 hours. Once we store the plugin in the lockfile, we + // should cache this indefinitely and only invalidate if the plugin + // is updated. + ttl := 24 * time.Hour + + // This is a stopgap until plugin is stored in lockfile. + // DEVBOX_X indicates this is an experimental env var. + // Use DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL to override the default TTL. + // e.g. DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL=1h will cache the plugin for 1 hour. + // Note: If you want to disable cache, we recommend using a low second value instead of zero to + // ensure only one network request is made. + ttlStr := os.Getenv("DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL") + if ttlStr != "" { + ttl, err = time.ParseDuration(ttlStr) + if err != nil { + return nil, err + } + } + return githubCache.GetOrSet( - contentURL, + contentURL+ttlStr, func() ([]byte, time.Duration, error) { req, err := p.request(contentURL) if err != nil { @@ -96,10 +116,8 @@ func (p *githubPlugin) FileContent(subpath string) ([]byte, error) { if err != nil { return nil, 0, err } - // Cache for 24 hours. Once we store the plugin in the lockfile, we - // should cache this indefinitely and only invalidate if the plugin - // is updated. - return body, 24 * time.Hour, nil + + return body, ttl, nil }, ) }