Skip to content

Commit

Permalink
Stop the ticker of cleaner on finalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed May 4, 2022
1 parent 90c1e58 commit d920d6a
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,25 @@ func New[K comparable, V any](replaceFn replaceFunc[K, V], freshFor, ttl time.Du
}

type cleaner[K comparable, V any] struct {
ticker *time.Ticker
closer chan struct{}
c *cache[K, V]
}

func newCleaner[K comparable, V any](c *cache[K, V], interval time.Duration, closer chan struct{}) *cleaner[K, V] {
cl := &cleaner[K, V]{
ticker: time.NewTicker(interval),
closer: closer,
c: c,
}
go cl.run()
go cl.run(interval)
return cl
}

func (c *cleaner[K, V]) run() {
func (c *cleaner[K, V]) run(interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-c.ticker.C:
case <-ticker.C:
c.c.cleanup()
case <-c.closer:
return
Expand Down

0 comments on commit d920d6a

Please sign in to comment.