Heavy security and file splitting
CI / Go Tests (push) Successful in 15s
CI / Build (push) Successful in 9s
Format / gofmt (push) Successful in 7s
Release Artifacts / Validate release tag (push) Successful in 2s
Release Artifacts / Build and release executables (push) Successful in 15s
Release Artifacts / Build and release Docker image (push) Successful in 28s
CI / Go Tests (push) Successful in 15s
CI / Build (push) Successful in 9s
Format / gofmt (push) Successful in 7s
Release Artifacts / Validate release tag (push) Successful in 2s
Release Artifacts / Build and release executables (push) Successful in 15s
Release Artifacts / Build and release Docker image (push) Successful in 28s
This commit is contained in:
@@ -32,6 +32,19 @@ type rawContentLoad struct {
|
||||
err error
|
||||
}
|
||||
|
||||
// copyBytes returns a defensive copy so callers cannot mutate the cache's backing
|
||||
// slice (which would affect concurrent/future hits for the same ID). Cost is
|
||||
// acceptable: cache is small (default 32 entries) and holds decompressed raw
|
||||
// content for range requests only.
|
||||
func copyBytes(b []byte) []byte {
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
c := make([]byte, len(b))
|
||||
copy(c, b)
|
||||
return c
|
||||
}
|
||||
|
||||
func newRawContentCache(maxEntries int, maxBytes int64) *rawContentCache {
|
||||
if maxEntries <= 0 {
|
||||
maxEntries = defaultRawCacheMaxEntries
|
||||
@@ -53,14 +66,14 @@ func (c *rawContentCache) GetOrLoad(id string, loader func() ([]byte, error)) ([
|
||||
if elem, ok := c.items[id]; ok {
|
||||
c.lru.MoveToFront(elem)
|
||||
item := elem.Value.(*rawContentItem)
|
||||
data := item.data
|
||||
data := copyBytes(item.data)
|
||||
c.mu.Unlock()
|
||||
return data, nil, true
|
||||
}
|
||||
if load, ok := c.inFlight[id]; ok {
|
||||
c.mu.Unlock()
|
||||
<-load.wait
|
||||
return load.data, load.err, false
|
||||
return copyBytes(load.data), load.err, false
|
||||
}
|
||||
|
||||
load := &rawContentLoad{wait: make(chan struct{})}
|
||||
@@ -79,7 +92,7 @@ func (c *rawContentCache) GetOrLoad(id string, loader func() ([]byte, error)) ([
|
||||
c.mu.Unlock()
|
||||
|
||||
close(load.wait)
|
||||
return data, err, false
|
||||
return copyBytes(data), err, false
|
||||
}
|
||||
|
||||
func (c *rawContentCache) Delete(id string) {
|
||||
|
||||
Reference in New Issue
Block a user