Skip to content

Commit 0a06aeb

Browse files
patrislavclaude
andcommitted
refactor(encryption): use x/sync singleflight for DEK loads
Replaces the hand-rolled inflight map with singleflight.Group. DoChan keeps the context cancellation, and Result.Shared is the coalesced annotation, so fetchDEK loses the wait-then-re-read-then-load-anyway path. copyBytes was slices.Clone. x/sync was already in the module graph; this only promotes it to direct. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 968420f commit 0a06aeb

4 files changed

Lines changed: 34 additions & 245 deletions

File tree

‎encryption/cache.go‎

Lines changed: 12 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package encryption
22

33
import (
44
"container/list"
5-
"context"
5+
"slices"
66
"sync"
77
"time"
8+
9+
"golang.org/x/sync/singleflight"
810
)
911

1012
// CacheConfig configures the optional DEK (data encryption key) cache.
@@ -33,26 +35,16 @@ type dekCache struct {
3335
maxSize int
3436
ttl time.Duration
3537

36-
inflightMu sync.Mutex
37-
inflight map[string]*inflightEntry
38-
}
39-
40-
// inflightEntry coordinates singleflight deduplication for concurrent
41-
// cache misses on the same keyRef. It deliberately carries no key material:
42-
// waiters read the result from the cache, so there is no second copy of a DEK
43-
// to keep track of and zero.
44-
type inflightEntry struct {
45-
done chan struct{}
46-
err error
38+
// group collapses concurrent misses on the same keyRef into one load.
39+
group singleflight.Group
4740
}
4841

4942
func newDEKCache(maxSize int, ttl time.Duration) *dekCache {
5043
return &dekCache{
51-
entries: make(map[string]*dekCacheEntry),
52-
order: list.New(),
53-
maxSize: maxSize,
54-
ttl: ttl,
55-
inflight: make(map[string]*inflightEntry),
44+
entries: make(map[string]*dekCacheEntry),
45+
order: list.New(),
46+
maxSize: maxSize,
47+
ttl: ttl,
5648
}
5749
}
5850

@@ -72,7 +64,7 @@ func (c *dekCache) get(keyRef string) ([]byte, bool) {
7264
}
7365

7466
c.order.MoveToFront(entry.element)
75-
return copyBytes(entry.dek), true
67+
return slices.Clone(entry.dek), true
7668
}
7769

7870
// put stores a copy of dek in the cache, evicting the LRU entry if full.
@@ -83,14 +75,14 @@ func (c *dekCache) put(keyRef string, dek []byte) {
8375
if entry, ok := c.entries[keyRef]; ok {
8476
// Update existing entry.
8577
clear(entry.dek)
86-
entry.dek = copyBytes(dek)
78+
entry.dek = slices.Clone(dek)
8779
entry.expiresAt = time.Now().Add(c.ttl)
8880
c.order.MoveToFront(entry.element)
8981
return
9082
}
9183

9284
entry := &dekCacheEntry{
93-
dek: copyBytes(dek),
85+
dek: slices.Clone(dek),
9486
keyRef: keyRef,
9587
expiresAt: time.Now().Add(c.ttl),
9688
}
@@ -121,52 +113,3 @@ func (c *dekCache) evictLocked(entry *dekCacheEntry) {
121113
c.order.Remove(entry.element)
122114
delete(c.entries, entry.keyRef)
123115
}
124-
125-
// waitOrStart implements singleflight deduplication. If another goroutine is
126-
// already fetching the DEK for keyRef, started=false and wait blocks until that
127-
// fetch finishes, reporting its error; the caller then reads the DEK from the
128-
// cache. Otherwise started=true and the caller must call finish.
129-
func (c *dekCache) waitOrStart(keyRef string) (started bool, wait func(ctx context.Context) error) {
130-
c.inflightMu.Lock()
131-
132-
if entry, ok := c.inflight[keyRef]; ok {
133-
c.inflightMu.Unlock()
134-
return false, func(ctx context.Context) error {
135-
select {
136-
case <-entry.done:
137-
return entry.err
138-
case <-ctx.Done():
139-
return ctx.Err()
140-
}
141-
}
142-
}
143-
144-
entry := &inflightEntry{done: make(chan struct{})}
145-
c.inflight[keyRef] = entry
146-
c.inflightMu.Unlock()
147-
148-
return true, nil
149-
}
150-
151-
// finish signals all waiters for keyRef with the fetch outcome.
152-
func (c *dekCache) finish(keyRef string, err error) {
153-
c.inflightMu.Lock()
154-
defer c.inflightMu.Unlock()
155-
156-
entry, ok := c.inflight[keyRef]
157-
if !ok {
158-
return
159-
}
160-
entry.err = err
161-
close(entry.done)
162-
delete(c.inflight, keyRef)
163-
}
164-
165-
func copyBytes(b []byte) []byte {
166-
if b == nil {
167-
return nil
168-
}
169-
cp := make([]byte, len(b))
170-
copy(cp, b)
171-
return cp
172-
}

‎encryption/cache_test.go‎

Lines changed: 0 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package encryption
22

33
import (
4-
"context"
5-
"errors"
64
"sync"
75
"testing"
86
"time"
@@ -192,147 +190,6 @@ func TestDEKCache_DeleteMissing(t *testing.T) {
192190
c.delete("nonexistent")
193191
}
194192

195-
func TestDEKCache_Singleflight(t *testing.T) {
196-
c := newDEKCache(10, time.Minute)
197-
198-
dek := []byte("0123456789abcdef0123456789abcdef")
199-
200-
// First caller starts the fetch.
201-
started, _ := c.waitOrStart("ref1")
202-
if !started {
203-
t.Fatal("first caller should start")
204-
}
205-
206-
// Second and third callers should wait.
207-
var wg sync.WaitGroup
208-
results := make([][]byte, 2)
209-
errs := make([]error, 2)
210-
211-
for i := 0; i < 2; i++ {
212-
wg.Add(1)
213-
go func(idx int) {
214-
defer wg.Done()
215-
s, wait := c.waitOrStart("ref1")
216-
if s {
217-
t.Error("subsequent caller should not start")
218-
return
219-
}
220-
if errs[idx] = wait(context.Background()); errs[idx] != nil {
221-
return
222-
}
223-
results[idx], _ = c.get("ref1")
224-
}(i)
225-
}
226-
227-
// Simulate the winner completing: populate the cache, then release waiters.
228-
time.Sleep(10 * time.Millisecond) // let goroutines reach wait()
229-
c.put("ref1", dek)
230-
c.finish("ref1", nil)
231-
232-
wg.Wait()
233-
234-
for i := 0; i < 2; i++ {
235-
if errs[i] != nil {
236-
t.Fatalf("waiter %d got error: %v", i, errs[i])
237-
}
238-
if string(results[i]) != string(dek) {
239-
t.Fatalf("waiter %d got wrong dek", i)
240-
}
241-
}
242-
243-
// Each waiter should have received an independent copy.
244-
results[0][0] = 0xFF
245-
if results[1][0] == 0xFF {
246-
t.Fatal("waiters should receive independent copies")
247-
}
248-
}
249-
250-
func TestDEKCache_SingleflightContextCancel(t *testing.T) {
251-
c := newDEKCache(10, time.Minute)
252-
253-
started, _ := c.waitOrStart("ref1")
254-
if !started {
255-
t.Fatal("first caller should start")
256-
}
257-
258-
ctx, cancel := context.WithCancel(context.Background())
259-
done := make(chan error, 1)
260-
go func() {
261-
_, wait := c.waitOrStart("ref1")
262-
done <- wait(ctx)
263-
}()
264-
265-
time.Sleep(10 * time.Millisecond)
266-
cancel()
267-
268-
select {
269-
case err := <-done:
270-
if !errors.Is(err, context.Canceled) {
271-
t.Fatalf("got %v, want context.Canceled", err)
272-
}
273-
case <-time.After(time.Second):
274-
t.Fatal("waiter ignored context cancellation")
275-
}
276-
277-
// The starter can still finish without affecting the cancelled waiter.
278-
c.finish("ref1", nil)
279-
}
280-
281-
func TestDEKCache_SingleflightError(t *testing.T) {
282-
c := newDEKCache(10, time.Minute)
283-
fetchErr := &testError{msg: "kms failed"}
284-
285-
started, _ := c.waitOrStart("ref1")
286-
if !started {
287-
t.Fatal("first caller should start")
288-
}
289-
290-
var wg sync.WaitGroup
291-
waiterErrs := make([]error, 2)
292-
293-
for i := 0; i < 2; i++ {
294-
wg.Add(1)
295-
go func(idx int) {
296-
defer wg.Done()
297-
_, wait := c.waitOrStart("ref1")
298-
waiterErrs[idx] = wait(context.Background())
299-
}(i)
300-
}
301-
302-
time.Sleep(10 * time.Millisecond)
303-
c.finish("ref1", fetchErr)
304-
305-
wg.Wait()
306-
307-
for i := 0; i < 2; i++ {
308-
if waiterErrs[i] == nil {
309-
t.Fatalf("waiter %d should have received error", i)
310-
}
311-
if waiterErrs[i].Error() != "kms failed" {
312-
t.Fatalf("waiter %d got error %q, want %q", i, waiterErrs[i].Error(), "kms failed")
313-
}
314-
}
315-
}
316-
317-
func TestDEKCache_SingleflightIndependentKeys(t *testing.T) {
318-
c := newDEKCache(10, time.Minute)
319-
320-
// Start fetch for ref1.
321-
started1, _ := c.waitOrStart("ref1")
322-
if !started1 {
323-
t.Fatal("first caller for ref1 should start")
324-
}
325-
326-
// Start fetch for ref2 — should NOT be blocked by ref1.
327-
started2, _ := c.waitOrStart("ref2")
328-
if !started2 {
329-
t.Fatal("first caller for ref2 should start independently")
330-
}
331-
332-
c.finish("ref1", nil)
333-
c.finish("ref2", nil)
334-
}
335-
336193
func TestDEKCache_ConcurrentAccess(t *testing.T) {
337194
c := newDEKCache(10, time.Minute)
338195
dek := []byte("0123456789abcdef0123456789abcdef")
@@ -357,9 +214,3 @@ func TestDEKCache_ConcurrentAccess(t *testing.T) {
357214
}
358215
wg.Wait()
359216
}
360-
361-
type testError struct {
362-
msg string
363-
}
364-
365-
func (e *testError) Error() string { return e.msg }

‎encryption/pool.go‎

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"log/slog"
9+
"slices"
910
"strconv"
1011
"time"
1112

@@ -226,36 +227,30 @@ func (p *Pool) fetchDEK(ctx context.Context, att *enclave.Attestation, keyRef st
226227
return dek, nil
227228
}
228229

229-
started, wait := p.cache.waitOrStart(keyRef)
230-
if !started {
231-
span.SetAnnotation(annotationDEKCache, "coalesced")
232-
if err := wait(ctx); err != nil {
230+
loaded := p.cache.group.DoChan(keyRef, func() (any, error) {
231+
dek, err := p.loadDEK(ctx, att, keyRef)
232+
if err != nil {
233233
return nil, err
234234
}
235-
if dek, ok := p.cache.get(keyRef); ok {
236-
return dek, nil
237-
}
238-
// Evicted between the winner's put and this read; load it ourselves
239-
// rather than waiting again.
240-
span.SetAnnotation(annotationDEKCache, "miss")
241-
return p.loadDEK(ctx, att, keyRef)
242-
}
243-
244-
span.SetAnnotation(annotationDEKCache, "miss")
245-
246-
var (
247-
dek []byte
248-
err error
249-
)
250-
// Deferred so a panic in loadDEK still releases the waiters, and ordered
251-
// after the put so the cache is populated by the time they re-read it.
252-
defer func() { p.cache.finish(keyRef, err) }()
253-
254-
dek, err = p.loadDEK(ctx, att, keyRef)
255-
if err == nil {
256235
p.cache.put(keyRef, dek)
236+
return dek, nil
237+
})
238+
239+
select {
240+
case res := <-loaded:
241+
if res.Err != nil {
242+
return nil, res.Err
243+
}
244+
if res.Shared {
245+
span.SetAnnotation(annotationDEKCache, "coalesced")
246+
} else {
247+
span.SetAnnotation(annotationDEKCache, "miss")
248+
}
249+
// Shared between everyone who coalesced onto this load.
250+
return slices.Clone(res.Val.([]byte)), nil
251+
case <-ctx.Done():
252+
return nil, ctx.Err()
257253
}
258-
return dek, err
259254
}
260255

261256
// loadDEK recovers a DEK through the full path: DynamoDB lookup, attestation

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/fxamacker/cbor/v2 v2.9.0
1414
github.com/go-chi/chi/v5 v5.2.5
1515
github.com/stretchr/testify v1.11.1
16+
golang.org/x/sync v0.20.0
1617
golang.org/x/tools v0.43.0
1718
)
1819

@@ -30,6 +31,5 @@ require (
3031
github.com/stretchr/objx v0.5.2 // indirect
3132
github.com/x448/float16 v0.8.4 // indirect
3233
golang.org/x/mod v0.34.0 // indirect
33-
golang.org/x/sync v0.20.0 // indirect
3434
gopkg.in/yaml.v3 v3.0.1 // indirect
3535
)

0 commit comments

Comments
 (0)