-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
159 lines (144 loc) · 3.18 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package imports
import (
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
)
var projectTombstonesCommon = [...]string{
".git",
"go.mod",
"go.work", // go1.18
}
var projectTombstonesUncommon = [...]string{
"glide.yaml",
"Gopkg.toml",
".svn",
".hg",
}
func projectDir(dir string) string {
dir = filepath.Clean(dir)
orig := dir
// fast path using comming tombstones
for {
for _, s := range &projectTombstonesCommon {
if fileExists(dir + string(filepath.Separator) + s) {
return dir
}
}
d := filepath.Dir(dir)
if d == dir {
break
}
dir = d
}
// try with more uncommon project tombstones
dir = orig
for {
for _, s := range &projectTombstonesUncommon {
if fileExists(dir + string(filepath.Separator) + s) {
return dir
}
}
d := filepath.Dir(dir)
if d == dir {
break
}
dir = d
}
return orig
}
// TODO: include go.mod file in the cache key
type goEnvCacheKey struct {
WorkDir string
BuildFlags string
Env string
// TODO: consider hashing the contents of the mod file
// after formatting it
ModFile string
ModModTime int64
ModSize int64
}
func newGoEnvCacheKey(p *ProcessEnv) goEnvCacheKey {
env := p.env()
if len(env) > 1 {
sort.Strings(env)
}
var flags []string
if len(p.BuildFlags) != 0 {
flags = make([]string, len(p.BuildFlags))
copy(flags, p.BuildFlags)
if len(flags) > 1 {
sort.Strings(flags)
}
}
dir := projectDir(p.WorkingDir)
key := goEnvCacheKey{
WorkDir: dir,
BuildFlags: strings.Join(flags, ","),
Env: strings.Join(env, ","),
}
for _, name := range []string{"go.mod", "go.work"} {
name = dir + string(filepath.Separator) + name
if fi, err := os.Stat(name); err == nil && fi.Mode().IsRegular() {
key.ModFile = name
key.ModModTime = fi.ModTime().UnixNano()
key.ModSize = fi.Size()
}
}
return key
}
type goEnvCacheEntry struct {
once sync.Once
createdAt time.Time // time.Time the entry was created
env map[string]string
err error
}
func (e *goEnvCacheEntry) shouldInvalidate() bool {
const (
// max age of valid cache entries
maxAge = time.Minute * 5
// max age of invalid (error'd) cache entrie
errInterval = time.Second * 5
)
d := time.Since(e.createdAt)
return d >= maxAge || (e.err != nil && d >= errInterval)
}
func (e *goEnvCacheEntry) lazyInit(p *ProcessEnv) {
e.once.Do(func() {
var stdout *bytes.Buffer
stdout, e.err = p.invokeGo(context.TODO(), "env", append([]string{"-json"}, RequiredGoEnvVars...)...)
if e.err != nil {
return
}
env := make(map[string]string, len(RequiredGoEnvVars))
if e.err = json.Unmarshal(stdout.Bytes(), &env); e.err == nil {
e.env = env
}
})
}
var goEnvCache sync.Map
func (p *ProcessEnv) goCmdEnv(_ context.Context) (map[string]string, error) {
key := newGoEnvCacheKey(p)
var e *goEnvCacheEntry
v, ok := goEnvCache.Load(key)
if !ok {
e = &goEnvCacheEntry{createdAt: time.Now()}
if vv, loaded := goEnvCache.LoadOrStore(key, e); loaded {
e = vv.(*goEnvCacheEntry)
}
} else {
e = v.(*goEnvCacheEntry)
}
e.lazyInit(p)
if e.shouldInvalidate() {
e = &goEnvCacheEntry{createdAt: time.Now()}
goEnvCache.Store(key, e)
}
return e.env, e.err
}