From 13a2e0e69062468df90dda5ac07d545ee5d21b4e Mon Sep 17 00:00:00 2001 From: silentsai Date: Wed, 18 Jun 2014 15:47:26 +0800 Subject: [PATCH] fix bug - expire bk routine keep running after ledis close --- ledis/ledis.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ledis/ledis.go b/ledis/ledis.go index baf152b..f011d65 100644 --- a/ledis/ledis.go +++ b/ledis/ledis.go @@ -46,6 +46,7 @@ type Ledis struct { binlog *BinLog quit chan struct{} + jobs *sync.WaitGroup } func Open(configJson json.RawMessage) (*Ledis, error) { @@ -75,6 +76,7 @@ func OpenWithConfig(cfg *Config) (*Ledis, error) { l := new(Ledis) l.quit = make(chan struct{}) + l.jobs = new(sync.WaitGroup) l.ldb = ldb @@ -118,6 +120,7 @@ func newDB(l *Ledis, index uint8) *DB { func (l *Ledis) Close() { close(l.quit) + l.jobs.Wait() l.ldb.Close() @@ -156,19 +159,23 @@ func (l *Ledis) activeExpireCycle() { executors[i] = db.newEliminator() } + l.jobs.Add(1) go func() { tick := time.NewTicker(1 * time.Second) - for { + end := false + for !end { select { case <-tick.C: for _, eli := range executors { eli.active() } case <-l.quit: + end = true break } } tick.Stop() + l.jobs.Done() }() }