-
Notifications
You must be signed in to change notification settings - Fork 1
/
memrecorder.go
62 lines (53 loc) · 1.29 KB
/
memrecorder.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
package main
import (
"sync"
"time"
)
// MemRecorder stores all seen image tags in memory.
type MemRecorder struct {
imageTags map[string]time.Time
mutex sync.Mutex
}
// NewMemRecorder initializes a new MemRecorder for use.
func NewMemRecorder() *MemRecorder {
var r MemRecorder
r.imageTags = make(map[string]time.Time)
return &r
}
// SawImageTagAt records when a tag was last seen.
//
// Duplicate or older times will be ignored.
func (r *MemRecorder) SawImageTagAt(tag string, when time.Time) {
r.mutex.Lock()
defer r.mutex.Unlock()
if old, ok := r.imageTags[tag]; ok {
if when.Before(old) {
return // We don't need to adjust the value.
}
}
r.imageTags[tag] = when
}
// SawImageTag records a tag being seen now.
func (r *MemRecorder) SawImageTag(tag string) {
r.SawImageTagAt(tag, time.Now())
}
// Forget that you saw a tag.
func (r *MemRecorder) Forget(tag string) {
r.mutex.Lock()
delete(r.imageTags, tag)
r.mutex.Unlock()
}
// Sweep runs a function on all tag and timestamp pairs.
func (r *MemRecorder) Sweep(sweeper SweepHandler) {
copiedImageTags := make(map[string]time.Time)
r.mutex.Lock()
for tag, when := range r.imageTags {
copiedImageTags[tag] = when
}
r.mutex.Unlock()
for tag, when := range copiedImageTags {
if sweeper(tag, when) {
r.Forget(tag)
}
}
}