This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
digestcache.go
199 lines (167 loc) · 4.53 KB
/
digestcache.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package server
import (
"sync"
"time"
"github.com/hashicorp/golang-lru"
"github.com/docker/distribution/digest"
"k8s.io/kubernetes/pkg/util/clock"
)
// digestToRepositoryCache maps image digests to recently seen remote repositories that
// may contain that digest. Each digest is bucketed and remembering new repositories will
// push old repositories out.
type digestToRepositoryCache struct {
*lru.Cache
clock clock.Clock
}
// newDigestToRepositoryCache creates a new LRU cache of image digests to possible remote
// repository strings with the given size. It returns an error if the cache
// cannot be created.
func newDigestToRepositoryCache(size int) (digestToRepositoryCache, error) {
c, err := lru.New(size)
if err != nil {
return digestToRepositoryCache{}, err
}
return digestToRepositoryCache{
Cache: c,
clock: &clock.RealClock{},
}, nil
}
const bucketSize = 16
// RememberDigest associates a digest with a repository.
func (c digestToRepositoryCache) RememberDigest(dgst digest.Digest, ttl time.Duration, repo string) {
key := dgst.String()
value, ok := c.Get(key)
if !ok {
value = &repositoryBucket{clock: c.clock}
if ok, _ := c.ContainsOrAdd(key, value); ok {
// the value exists now, get it
value, ok = c.Get(key)
if !ok {
// should not happen
return
}
}
}
repos := value.(*repositoryBucket)
repos.Add(ttl, repo)
}
// ForgetDigest removes an association between given digest and repository from the cache.
func (c digestToRepositoryCache) ForgetDigest(dgst digest.Digest, repo string) {
key := dgst.String()
value, ok := c.Peek(key)
if !ok {
return
}
repos := value.(*repositoryBucket)
repos.Remove(repo)
}
// RepositoriesForDigest returns a list of repositories that may contain this digest.
func (c digestToRepositoryCache) RepositoriesForDigest(dgst digest.Digest) []string {
value, ok := c.Get(dgst.String())
if !ok {
return nil
}
repos := value.(*repositoryBucket)
return repos.Copy()
}
func (c digestToRepositoryCache) RepositoryHasBlob(repo string, dgst digest.Digest) bool {
value, ok := c.Get(dgst.String())
if !ok {
return false
}
repos := value.(*repositoryBucket)
return repos.Has(repo)
}
// repositoryBucket contains a list of repositories with eviction timeouts.
type repositoryBucket struct {
mu sync.Mutex
clock clock.Clock
list []bucketEntry
}
// Has returns true if the bucket contains this repository.
func (b *repositoryBucket) Has(repo string) bool {
b.mu.Lock()
defer b.mu.Unlock()
b.evictStale()
return b.getIndexOf(repo) >= 0
}
// Add one or more repositories to this bucket.
func (b *repositoryBucket) Add(ttl time.Duration, repos ...string) {
b.mu.Lock()
defer b.mu.Unlock()
b.evictStale()
evictOn := b.clock.Now().Add(ttl)
for _, repo := range repos {
index := b.getIndexOf(repo)
arr := b.list
if index >= 0 {
// repository already exists, move it to the end with highest eviction time
entry := arr[index]
copy(arr[index:], arr[index+1:])
if entry.evictOn.Before(evictOn) {
entry.evictOn = evictOn
}
arr[len(arr)-1] = entry
} else {
// repo is a new entry
if len(arr) >= bucketSize {
copy(arr, arr[1:])
arr = arr[:bucketSize-1]
}
arr = append(arr, bucketEntry{
repository: repo,
evictOn: evictOn,
})
}
b.list = arr
}
}
// Remove removes all the given repos from repository bucket.
func (b *repositoryBucket) Remove(repos ...string) {
b.mu.Lock()
defer b.mu.Unlock()
for _, repo := range repos {
index := b.getIndexOf(repo)
if index >= 0 {
copy(b.list[index:], b.list[index+1:])
b.list = b.list[:len(b.list)-1]
}
}
}
// getIndexOf returns an index of given repository in bucket's array. If not found, -1 will be returned.
func (b *repositoryBucket) getIndexOf(repo string) int {
for i, entry := range b.list {
if entry.repository == repo {
return i
}
}
return -1
}
// evictStale removes stale entries from the list and shifts all the survivalists to the front.
func (b *repositoryBucket) evictStale() {
now := b.clock.Now()
arr := b.list[:0]
for _, entry := range b.list {
if entry.evictOn.Before(now) {
continue
}
arr = append(arr, entry)
}
b.list = arr
}
// Copy returns a copy of the contents of this bucket in a thread-safe fashion.
func (b *repositoryBucket) Copy() []string {
b.mu.Lock()
defer b.mu.Unlock()
b.evictStale()
out := make([]string, len(b.list))
for i, e := range b.list {
out[i] = e.repository
}
return out
}
// bucketEntry holds a repository name with eviction timeout.
type bucketEntry struct {
repository string
evictOn time.Time
}