Skip to content

Commit c8d2ffd

Browse files
authored
[plugins] Cache github plugin (#1997)
## Summary Excessive github requests are slow and can also lead to API rate limiting. This caches content for 24 hours. Ideally we store resolved plugin data in the lock file and cache indefinitely (until user does update) but that's a bit more work so this is a stopgap. Users can use `devbox update` to clear cache. cc: @Lagoja ## How was it tested? Added print statements to track http requests and observed them going away after content is cached.
1 parent 2d567ca commit c8d2ffd

File tree

14 files changed

+109
-54
lines changed

14 files changed

+109
-54
lines changed

devbox.lock

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,56 @@
22
"lockfile_version": "1",
33
"packages": {
44
"go@latest": {
5-
"last_modified": "2024-03-06T17:57:40Z",
6-
"resolved": "github:NixOS/nixpkgs/58ae79ea707579c40102ddf62d84b902a987c58b#go_1_22",
5+
"last_modified": "2024-03-22T11:26:23Z",
6+
"resolved": "github:NixOS/nixpkgs/a3ed7406349a9335cb4c2a71369b697cecd9d351#go",
77
"source": "devbox-search",
88
"version": "1.22.1",
99
"systems": {
1010
"aarch64-darwin": {
11-
"store_path": "/nix/store/q955gd41jh08l5pgbmx7y5jlqwwk4rqk-go-1.22.1"
11+
"outputs": [
12+
{
13+
"name": "out",
14+
"path": "/nix/store/n1k6wf8q10q7k0863gb78b1rf0j07r7r-go-1.22.1",
15+
"default": true
16+
}
17+
],
18+
"store_path": "/nix/store/n1k6wf8q10q7k0863gb78b1rf0j07r7r-go-1.22.1"
1219
},
1320
"aarch64-linux": {
14-
"store_path": "/nix/store/ca92iff1jpd9a3l3i8222rzbkq949mlh-go-1.22.1"
21+
"outputs": [
22+
{
23+
"name": "out",
24+
"path": "/nix/store/fl6cjlp5bvykfz1kxrw687zxzld25pn7-go-1.22.1",
25+
"default": true
26+
}
27+
],
28+
"store_path": "/nix/store/fl6cjlp5bvykfz1kxrw687zxzld25pn7-go-1.22.1"
1529
},
1630
"x86_64-darwin": {
17-
"store_path": "/nix/store/f64hpgi6lnn8vrf22cff2y8307c7zns3-go-1.22.1"
31+
"outputs": [
32+
{
33+
"name": "out",
34+
"path": "/nix/store/xgdp7gnf6vzr2ick2ip1lhq4cww65p7w-go-1.22.1",
35+
"default": true
36+
}
37+
],
38+
"store_path": "/nix/store/xgdp7gnf6vzr2ick2ip1lhq4cww65p7w-go-1.22.1"
1839
},
1940
"x86_64-linux": {
20-
"store_path": "/nix/store/jw9qrkqlkm699ncbrbs1d4czfg4wg8da-go-1.22.1"
41+
"outputs": [
42+
{
43+
"name": "out",
44+
"path": "/nix/store/bp39dh48cdqp89hk5mpdi1lxdf0mjl7x-go-1.22.1",
45+
"default": true
46+
}
47+
],
48+
"store_path": "/nix/store/bp39dh48cdqp89hk5mpdi1lxdf0mjl7x-go-1.22.1"
2149
}
2250
}
2351
},
2452
"runx:golangci/golangci-lint@latest": {
25-
"resolved": "golangci/golangci-lint@v1.56.2",
26-
"version": "v1.56.2"
53+
"resolved": "golangci/golangci-lint@v1.57.2",
54+
"version": "v1.57.2"
2755
},
2856
"runx:mvdan/gofumpt@latest": {
2957
"resolved": "mvdan/[email protected]",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
some data

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/getsentry/sentry-go v0.27.0
2424
github.com/google/go-cmp v0.6.0
2525
github.com/google/uuid v1.6.0
26+
github.com/gosimple/slug v1.14.0
2627
github.com/hashicorp/go-envparse v0.1.0
2728
github.com/joho/godotenv v1.5.1
2829
github.com/mattn/go-isatty v0.0.20
@@ -93,7 +94,6 @@ require (
9394
github.com/google/go-github/v53 v53.2.0 // indirect
9495
github.com/google/go-querystring v1.1.0 // indirect
9596
github.com/google/renameio/v2 v2.0.0 // indirect
96-
github.com/gosimple/slug v1.14.0 // indirect
9797
github.com/gosimple/unidecode v1.0.1 // indirect
9898
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
9999
github.com/h2non/filetype v1.1.3 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ github.com/zealic/go2node v0.1.0 h1:ofxpve08cmLJBwFdI0lPCk9jfwGWOSD+s6216x0oAaA=
360360
github.com/zealic/go2node v0.1.0/go.mod h1:GrkFr+HctXwP7vzcU9RsgtAeJjTQ6Ud0IPCQAqpTfBg=
361361
go.jetpack.io/envsec v0.0.16-0.20240329013200-4174c0acdb00 h1:Kb+OlWOntAq+1nF+01ntqnQEqSJkFmLLS0RX5sl5zak=
362362
go.jetpack.io/envsec v0.0.16-0.20240329013200-4174c0acdb00/go.mod h1:dVG2n8fBAGpQczW8yk/6wuXb9uEhzaJF7wGXkGLRRCU=
363-
go.jetpack.io/pkg v0.0.0-20240411004921-791796648f19 h1:ZloUPW4zNknwS2mYSmMVAtMfl3H2zZUCz+MxU5s79fY=
364-
go.jetpack.io/pkg v0.0.0-20240411004921-791796648f19/go.mod h1:JfVScypw14E4GR2TA2Nv29JqalRtgv2C9QjeQcNS4UM=
363+
go.jetpack.io/pkg v0.0.0-20240415163115-0f42b5c54fe1 h1:qLatba17X9hZ0+3m+9FzDu48cQBhCZjDHWJDctPGYp4=
364+
go.jetpack.io/pkg v0.0.0-20240415163115-0f42b5c54fe1/go.mod h1:JfVScypw14E4GR2TA2Nv29JqalRtgv2C9QjeQcNS4UM=
365365
go.jetpack.io/pkg v0.0.0-20240415190428-d17de207b432 h1:5B9uUxqzwDLUi+m/SVawTMszOmCK6KnUWb4yiCZJumg=
366366
go.jetpack.io/pkg v0.0.0-20240415190428-d17de207b432/go.mod h1:JfVScypw14E4GR2TA2Nv29JqalRtgv2C9QjeQcNS4UM=
367367
go.jetpack.io/typeid v1.0.1-0.20240410183543-96a4fd53d1e2 h1:w9uWg8BAim374iWzxEuDhf6MJ/cMQVR/0xi/L3DgfT0=

internal/cachehash/hash.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ import (
1919
)
2020

2121
// Bytes returns a hex-encoded hash of b.
22-
// TODO: This doesn't need to return an error.
23-
func Bytes(b []byte) (string, error) {
22+
func Bytes(b []byte) string {
2423
h := newHash()
2524
h.Write(b)
26-
return hex.EncodeToString(h.Sum(nil)), nil
25+
return hex.EncodeToString(h.Sum(nil))
26+
}
27+
28+
// Bytes6 returns the first 6 characters of the hash of b.
29+
func Bytes6(b []byte) string {
30+
hash := Bytes(b)
31+
return hash[:min(len(hash), 6)]
2732
}
2833

2934
// File returns a hex-encoded hash of a file's contents.
@@ -50,7 +55,7 @@ func JSON(a any) (string, error) {
5055
if err != nil {
5156
return "", redact.Errorf("marshal to json for hashing: %v", err)
5257
}
53-
return Bytes(b)
58+
return Bytes(b), nil
5459
}
5560

5661
// JSONFile compacts the JSON in a file and returns its hex-encoded hash.
@@ -66,7 +71,7 @@ func JSONFile(path string) (string, error) {
6671
if err := json.Compact(buf, b); err != nil {
6772
return "", redact.Errorf("compact json for hashing: %v", err)
6873
}
69-
return Bytes(buf.Bytes())
74+
return Bytes(buf.Bytes()), nil
7075
}
7176

7277
func newHash() hash.Hash { return sha256.New() }

internal/devbox/devbox.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (d *Devbox) ConfigHash() (string, error) {
188188
}
189189
buf.WriteString(h)
190190
}
191-
return cachehash.Bytes(buf.Bytes())
191+
return cachehash.Bytes(buf.Bytes()), nil
192192
}
193193

194194
func (d *Devbox) NixPkgsCommitHash() string {
@@ -1224,8 +1224,7 @@ var ignoreDevEnvVar = map[string]bool{
12241224
}
12251225

12261226
func (d *Devbox) ProjectDirHash() string {
1227-
h, _ := cachehash.Bytes([]byte(d.projectDir))
1228-
return h
1227+
return cachehash.Bytes([]byte(d.projectDir))
12291228
}
12301229

12311230
func (d *Devbox) addHashToEnv(env map[string]string) error {

internal/devbox/update.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"go.jetpack.io/devbox/internal/lock"
1515
"go.jetpack.io/devbox/internal/nix"
1616
"go.jetpack.io/devbox/internal/nix/nixprofile"
17+
"go.jetpack.io/devbox/internal/plugin"
1718
"go.jetpack.io/devbox/internal/searcher"
1819
"go.jetpack.io/devbox/internal/shellgen"
1920
"go.jetpack.io/devbox/internal/ux"
@@ -74,7 +75,7 @@ func (d *Devbox) Update(ctx context.Context, opts devopt.UpdateOpts) error {
7475
// It will return an error if .devbox/gen/flake is missing
7576
// TODO: Remove this if it's not needed.
7677
_ = nix.FlakeUpdate(shellgen.FlakePath(d))
77-
return nil
78+
return plugin.Update()
7879
}
7980

8081
func (d *Devbox) inputsToUpdate(

internal/devconfig/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func (c *Config) Hash() (string, error) {
277277
return "", err
278278
}
279279
data = append(data, hash...)
280-
return cachehash.Bytes(data)
280+
return cachehash.Bytes(data), nil
281281
}
282282

283283
func (c *Config) IsEnvsecEnabled() bool {

internal/devconfig/configfile/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (c *ConfigFile) Hash() (string, error) {
8484
}
8585
ast := c.ast.root.Clone()
8686
ast.Minimize()
87-
return cachehash.Bytes(ast.Pack())
87+
return cachehash.Bytes(ast.Pack()), nil
8888
}
8989

9090
func (c *ConfigFile) Equals(other *ConfigFile) bool {

internal/devpkg/package.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func (p *Package) Hash() string {
442442
}
443443

444444
if sum == "" {
445-
sum, _ = cachehash.Bytes([]byte(p.installable.String()))
445+
sum = cachehash.Bytes([]byte(p.installable.String()))
446446
}
447447
return sum[:min(len(sum), 6)]
448448
}

0 commit comments

Comments
 (0)