-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathentry.go
More file actions
126 lines (104 loc) · 3.3 KB
/
entry.go
File metadata and controls
126 lines (104 loc) · 3.3 KB
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
package kocache
import (
"context"
"io"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/stackrox/rox/pkg/concurrency"
"github.com/stackrox/rox/pkg/ioutils"
"github.com/stackrox/rox/pkg/sync"
"github.com/stackrox/rox/pkg/timestamp"
"github.com/stackrox/rox/pkg/utils"
)
type entry struct {
done concurrency.ErrorSignal
references sync.WaitGroup
data *ioutils.RWBuf
creationTime time.Time
lastAccess timestamp.MicroTS // atomically set
}
func newEntry() *entry {
return &entry{
done: concurrency.NewErrorSignal(),
creationTime: time.Now(),
lastAccess: timestamp.Now(),
}
}
func (e *entry) DoneSig() concurrency.ReadOnlyErrorSignal {
return &e.done
}
func (e *entry) Contents() (io.ReaderAt, int64, error) {
if err, ok := e.done.Error(); err != nil {
return nil, 0, err
} else if !ok {
return nil, 0, errors.New("content is not yet available")
}
return e.data.Contents()
}
func (e *entry) AcquireRef() {
e.references.Add(1)
e.lastAccess.StoreAtomic(timestamp.Now())
}
func (e *entry) ReleaseRef() {
e.lastAccess.StoreAtomic(timestamp.Now())
e.references.Done()
}
func (e *entry) Destroy() {
e.references.Wait()
concurrency.Wait(e.DoneSig()) // will happen after 60s max
_ = e.data.Close()
}
func (e *entry) CreationTime() time.Time {
return e.creationTime
}
func (e *entry) LastAccess() time.Time {
return e.lastAccess.LoadAtomic().GoTime()
}
func (e *entry) IsError() bool {
err, ok := e.done.Error()
return ok && err != nil
}
func (e *entry) Populate(ctx context.Context, client httpClient, upstreamURL string, opts *options) {
err := e.doPopulate(ctx, client, upstreamURL, opts)
defer e.done.SignalWithError(err)
e.lastAccess.StoreAtomic(timestamp.Now())
}
func (e *entry) doPopulate(ctx context.Context, client httpClient, upstreamURL string, opts *options) error {
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, upstreamURL, nil)
if err != nil {
return errors.Errorf("creating HTTP request: %v", err)
}
if opts.ModifyRequest != nil {
opts.ModifyRequest(req)
}
resp, err := client.Do(req)
if err != nil {
return errors.Errorf("making upstream request: %v", err)
}
defer utils.IgnoreError(resp.Body.Close)
// We really are not interested in anything other than 200. We can't assume the body being valid data on any 2xx
// or 3xx status code.
// Note that Golang's HTTP client by default follows redirects.
if resp.StatusCode != http.StatusOK {
// If the probe does not exist, we may receive 403 Forbidden due to security constraints
// on the storage. Convert this to errProbeNotFound for better visibility of this scenario.
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusForbidden {
return errProbeNotFound
}
return errors.Errorf("upstream HTTP request returned status %s", resp.Status)
}
contentLen := resp.ContentLength
e.data = ioutils.NewRWBuf(ioutils.RWBufOptions{
MemLimit: opts.ObjMemLimit,
HardLimit: opts.ObjHardLimit,
})
if n, err := io.Copy(e.data, resp.Body); err != nil {
return errors.Errorf("downloading data from upstream: %v", err)
} else if contentLen > 0 && n != contentLen {
return errors.Errorf("unexpected number of bytes read from upstream: expected %d bytes, got %d", contentLen, n)
}
return nil
}