Created
February 23, 2019 18:42
-
-
Save arriqaaq/47984a5e6ec1d833f06bd64fd1de3525 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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