-
Notifications
You must be signed in to change notification settings - Fork 6
/
cache.go
120 lines (101 loc) · 2.38 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
/*
file created by Junlin Chen in 2022
*/
package common
import (
"bytes"
"context"
"fmt"
"github.com/containerd/containerd/log"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/pkg/errors"
"io"
"sync"
"time"
)
type CacheInterface interface {
Digest() name.Digest
Size() int64
}
type LayerCache struct {
Buffer *io.SectionReader
Mutex sync.Mutex
Ready bool
UseCounter int
LastUsed time.Time
subscribers []*chan error
digest name.Digest
size int64
}
func (lc *LayerCache) String() string {
lc.Mutex.Lock()
defer lc.Mutex.Unlock()
return fmt.Sprintf("%s:%v:%02d:%s",
lc.digest, lc.Ready, lc.UseCounter, lc.LastUsed.Format(time.RFC3339Nano))
}
func (lc *LayerCache) SetReady(err error) {
lc.Mutex.Lock()
defer lc.Mutex.Unlock()
lc.Ready = true
lc.notify(err)
}
func (lc *LayerCache) notify(err error) {
for _, s := range lc.subscribers {
if err != nil {
*s <- err
}
close(*s)
}
}
func (lc *LayerCache) Subscribe(errChan *chan error) {
lc.Mutex.Lock()
defer lc.Mutex.Unlock()
if lc.Ready {
close(*errChan)
return
}
lc.subscribers = append(lc.subscribers, errChan)
}
func (lc *LayerCache) Load(ctx context.Context) (err error) {
defer lc.SetReady(err)
var l v1.Layer
l, err = remote.Layer(lc.digest, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return err
}
var rc io.ReadCloser
rc, err = l.Compressed()
if err != nil {
log.G(ctx).WithField("layer", lc.String()).Error(errors.Wrapf(err, "failed to load layer"))
return err
}
buf := new(bytes.Buffer)
var n int64
n, err = io.Copy(buf, rc)
if err != nil {
log.G(ctx).WithField("layer", lc.String()).Error(errors.Wrapf(err, "failed to load layer"))
return err
}
if n != lc.size {
err = fmt.Errorf("size unmatch expected %d, but got %d", lc.size, n)
log.G(ctx).WithField("layer", lc.String()).Error(errors.Wrapf(err, "failed to load layer"))
return err
}
rc.Close()
lc.Buffer = io.NewSectionReader(bytes.NewReader(buf.Bytes()), 0, n)
return nil
}
func NewLayerCache(layer CacheInterface) *LayerCache {
return &LayerCache{
Buffer: nil,
Mutex: sync.Mutex{},
Ready: false,
UseCounter: 0,
LastUsed: time.Now(),
digest: layer.Digest(),
size: layer.Size(),
}
}