From 4630a7861342220363823d113c35535c4a2806cf Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Sun, 16 Oct 2016 08:50:02 -0700 Subject: [PATCH] fix #67, hang on EXPIRE --- controller/expire.go | 57 +++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/controller/expire.go b/controller/expire.go index bfa6bad4..8608a269 100644 --- a/controller/expire.go +++ b/controller/expire.go @@ -56,38 +56,41 @@ func (c *Controller) getExpires(key, id string) (at time.Time, ok bool) { // per second. func (c *Controller) backgroundExpiring() { for { - c.mu.Lock() - if c.stopBackgroundExpiring { - c.mu.Unlock() - return - } - // Only excute for leaders. Followers should ignore. - if c.config.FollowHost == "" { - now := time.Now() - for key, m := range c.expires { - for id, at := range m { - if now.After(at) { - // issue a DEL command - c.mu.Lock() - c.statsExpired++ - c.mu.Unlock() - msg := &server.Message{} - msg.Values = resp.MultiBulkValue("del", key, id).Array() - msg.Command = "del" - _, d, err := c.cmdDel(msg) - if err != nil { - log.Fatal(err) - continue - } - if err := c.writeAOF(resp.ArrayValue(msg.Values), &d); err != nil { - log.Fatal(err) - continue + ok := func() bool { + c.mu.Lock() + defer c.mu.Unlock() + if c.stopBackgroundExpiring { + return false + } + // Only excute for leaders. Followers should ignore. + if c.config.FollowHost == "" { + now := time.Now() + for key, m := range c.expires { + for id, at := range m { + if now.After(at) { + // issue a DEL command + c.statsExpired++ + msg := &server.Message{} + msg.Values = resp.MultiBulkValue("del", key, id).Array() + msg.Command = "del" + _, d, err := c.cmdDel(msg) + if err != nil { + log.Fatal(err) + continue + } + if err := c.writeAOF(resp.ArrayValue(msg.Values), &d); err != nil { + log.Fatal(err) + continue + } } } } } + return true + }() + if !ok { + return } - c.mu.Unlock() time.Sleep(time.Second / 5) } }