Skip to content

Instantly share code, notes, and snippets.

@arriqaaq
Created February 23, 2019 18:42
Show Gist options
  • Save arriqaaq/47984a5e6ec1d833f06bd64fd1de3525 to your computer and use it in GitHub Desktop.
Save arriqaaq/47984a5e6ec1d833f06bd64fd1de3525 to your computer and use it in GitHub Desktop.
type evictor interface {
Run(*shard)
Stop()
}
func newSweeper(interval time.Duration) evictor {
return &sweeper{
interval: interval,
stopC: make(chan bool),
}
}
type sweeper struct {
interval time.Duration
stopC chan bool
}
func (s *sweeper) Run(c *shard) {
<-time.After(startupDelay())
ticker := time.NewTicker(s.interval)
for {
select {
case <-ticker.C:
c.Evict()
case <-s.stopC:
ticker.Stop()
return
}
}
}
func (s *sweeper) Stop() {
s.stopC <- true
}
func newShardWithSweeper(sweepTime time.Duration) *shard {
nc := &shard{
data: make(map[string]item),
}
if sweepTime > 0 {
runSweeper(nc, sweepTime)
runtime.SetFinalizer(nc, stopSweeper)
}
return nc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment