Skip to content

Commit

Permalink
Add evictAfterSize
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-cordenier committed Sep 25, 2024
1 parent 94232d1 commit e299333
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
35 changes: 22 additions & 13 deletions core/capabilities/compute/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,22 @@ type moduleCache struct {
wg sync.WaitGroup
stopChan services.StopChan

tickInterval time.Duration
timeout time.Duration
tickInterval time.Duration
timeout time.Duration
evictAfterSize int

clock clockwork.Clock
onReaper chan struct{}
}

func newModuleCache(clock clockwork.Clock, tick, timeout time.Duration) *moduleCache {
func newModuleCache(clock clockwork.Clock, tick, timeout time.Duration, evictAfterSize int) *moduleCache {
return &moduleCache{
m: map[string]*module{},
tickInterval: tick,
timeout: timeout,
clock: clock,
stopChan: make(chan struct{}),
m: map[string]*module{},
tickInterval: tick,
timeout: timeout,
evictAfterSize: evictAfterSize,
clock: clock,
stopChan: make(chan struct{}),
}
}

Expand Down Expand Up @@ -106,11 +108,18 @@ func (mc *moduleCache) evictOlderThan(duration time.Duration) {
defer mc.mu.Unlock()

evicted := 0
for id, m := range mc.m {
if mc.clock.Now().Sub(m.lastFetchedAt) > duration {
delete(mc.m, id)
m.module.Close()
evicted++

if len(mc.m) > mc.evictAfterSize {
for id, m := range mc.m {
if mc.clock.Now().Sub(m.lastFetchedAt) > duration {
delete(mc.m, id)
m.module.Close()
evicted++
}

if len(mc.m) <= mc.evictAfterSize {
break
}
}
}

Expand Down
37 changes: 36 additions & 1 deletion core/capabilities/compute/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestCache(t *testing.T) {
tick := 1 * time.Second
timeout := 1 * time.Second

cache := newModuleCache(clock, tick, timeout)
cache := newModuleCache(clock, tick, timeout, 0)
cache.onReaper = make(chan struct{}, 1)
cache.start()
defer cache.close()
Expand Down Expand Up @@ -53,3 +53,38 @@ func TestCache(t *testing.T) {
_, ok = cache.get(id)
assert.False(t, ok)
}

func TestCache_EvictAfterSize(t *testing.T) {
clock := clockwork.NewFakeClock()
tick := 1 * time.Second
timeout := 1 * time.Second

cache := newModuleCache(clock, tick, timeout, 1)
cache.onReaper = make(chan struct{}, 1)
cache.start()
defer cache.close()

var binary []byte

Check failure on line 67 in core/capabilities/compute/cache_test.go

View workflow job for this annotation

GitHub Actions / lint

S1021: should merge variable declaration with assignment on next line (gosimple)
binary = wasmtest.CreateTestBinary(binaryCmd, binaryLocation, false, t)
hmod, err := host.NewModule(&host.ModuleConfig{
Logger: logger.TestLogger(t),
IsUncompressed: true,
}, binary)
require.NoError(t, err)

id := uuid.New().String()
mod := &module{
module: hmod,
}
cache.add(id, mod)

got, ok := cache.get(id)
assert.True(t, ok)

assert.Equal(t, got, mod)

clock.Advance(15 * time.Second)
<-cache.onReaper
_, ok = cache.get(id)
assert.True(t, ok)
}
2 changes: 1 addition & 1 deletion core/capabilities/compute/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func NewAction(log logger.Logger, registry coretypes.CapabilitiesRegistry) *Comp
compute := &Compute{
log: log,
registry: registry,
modules: newModuleCache(clockwork.NewRealClock(), 1*time.Minute, 10*time.Minute),
modules: newModuleCache(clockwork.NewRealClock(), 1*time.Minute, 10*time.Minute, 3),
}
return compute
}

0 comments on commit e299333

Please sign in to comment.