Skip to content

Commit

Permalink
[plugins] Add env var to control github cache ttl (#2314)
Browse files Browse the repository at this point in the history
## 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
```
  • Loading branch information
mikeland73 authored Oct 1, 2024
1 parent 59c2d6d commit fc7a398
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions internal/plugin/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
},
)
}
Expand Down

0 comments on commit fc7a398

Please sign in to comment.