fix #67, hang on EXPIRE

This commit is contained in:
Josh Baker 2016-10-16 08:50:02 -07:00
parent bd2c25987a
commit 4630a78613
1 changed files with 30 additions and 27 deletions

View File

@ -56,38 +56,41 @@ func (c *Controller) getExpires(key, id string) (at time.Time, ok bool) {
// per second. // per second.
func (c *Controller) backgroundExpiring() { func (c *Controller) backgroundExpiring() {
for { for {
c.mu.Lock() ok := func() bool {
if c.stopBackgroundExpiring { c.mu.Lock()
c.mu.Unlock() defer c.mu.Unlock()
return if c.stopBackgroundExpiring {
} return false
// Only excute for leaders. Followers should ignore. }
if c.config.FollowHost == "" { // Only excute for leaders. Followers should ignore.
now := time.Now() if c.config.FollowHost == "" {
for key, m := range c.expires { now := time.Now()
for id, at := range m { for key, m := range c.expires {
if now.After(at) { for id, at := range m {
// issue a DEL command if now.After(at) {
c.mu.Lock() // issue a DEL command
c.statsExpired++ c.statsExpired++
c.mu.Unlock() msg := &server.Message{}
msg := &server.Message{} msg.Values = resp.MultiBulkValue("del", key, id).Array()
msg.Values = resp.MultiBulkValue("del", key, id).Array() msg.Command = "del"
msg.Command = "del" _, d, err := c.cmdDel(msg)
_, d, err := c.cmdDel(msg) if err != nil {
if err != nil { log.Fatal(err)
log.Fatal(err) continue
continue }
} if err := c.writeAOF(resp.ArrayValue(msg.Values), &d); err != nil {
if err := c.writeAOF(resp.ArrayValue(msg.Values), &d); err != nil { log.Fatal(err)
log.Fatal(err) continue
continue }
} }
} }
} }
} }
return true
}()
if !ok {
return
} }
c.mu.Unlock()
time.Sleep(time.Second / 5) time.Sleep(time.Second / 5)
} }
} }