Skip to content

Commit

Permalink
fix: remove ctx from entry timer
Browse files Browse the repository at this point in the history
  • Loading branch information
shaj13 committed Jun 17, 2022
1 parent d00c710 commit 971790b
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions internal/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package internal

import (
"context"
"time"
)

Expand All @@ -23,20 +22,19 @@ type Entry struct {
Element interface{}
Exp time.Time
timer *time.Timer
cancel context.CancelFunc
cancel chan struct{}
}

// start/stop timer added for safety to prevent fire on expired callback,
// when entry re-stored at the expiry time.
func (e *Entry) startTimer(d time.Duration, f func(key, value interface{})) {
ctx, cancel := context.WithCancel(context.TODO())
e.cancel = cancel
e.stopTimer() // Stop old timer if there
e.timer = time.AfterFunc(d, func() {
if ctx.Err() != nil {
return
select {
case <-e.cancel:
default:
f(e.Key, e.Value)
}

f(e.Key, e.Value)
})
}

Expand All @@ -45,7 +43,7 @@ func (e *Entry) stopTimer() {
return
}
e.timer.Stop()
e.cancel()
close(e.cancel)
}

// Cache is an abstracted cache that provides a skeletal implementation,
Expand Down

0 comments on commit 971790b

Please sign in to comment.