Skip to content

Commit

Permalink
[BUGFIX] Fixes to expChan reachability during mass data deletion (#140)
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <kpango@vdaas.org>
  • Loading branch information
kpango authored Sep 17, 2024
1 parent e54a136 commit c851e65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 13 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,22 @@ func main() {
return true
})

runtime.GC()
gch := gache.New[int64]().EnableExpiredHook().
SetExpiredHook(func(ctx context.Context, key string) {
glg.Debugf("key=%v expired", key)
}).
StartExpired(context.Background(), time.Second*10)
for i := 0; i < 10000; i++ {
gch.SetWithExpire("sample-"+strconv.Itoa(i), int64(i), time.Second*5)
}
time.Sleep(time.Second * 20)
glg.Debugf("length: %d", gch.Len())

runtime.GC()
gcs := gache.New[string]()
maxCnt := 10000000
digitLen := len(strconv.Itoa(maxCnt))
digitLen := len(strconv.Itoa(maxCnt))
for i := 0; i < maxCnt; i++ {
if i%1000 == 0 {
// runtime.ReadMemStats(&m)
Expand Down
10 changes: 6 additions & 4 deletions gache.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,22 @@ func (g *gache[V]) SetExpiredHook(f func(context.Context, string)) Gache[V] {
// StartExpired starts delete expired value daemon
func (g *gache[V]) StartExpired(ctx context.Context, dur time.Duration) Gache[V] {
go func() {
tick := time.NewTicker(dur)
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
g.cancel.Store(&cancel)
tick := time.NewTicker(dur)
for {
select {
case <-ctx.Done():
tick.Stop()
return
case <-tick.C:
g.DeleteExpired(ctx)
runtime.Gosched()
case key := <-g.expChan:
go g.expFunc(ctx, key)
case <-tick.C:
go func() {
g.DeleteExpired(ctx)
runtime.Gosched()
}()
}
}
}()
Expand Down

0 comments on commit c851e65

Please sign in to comment.