瀏覽代碼

Fix collection (locking and getting next value).

Dmitry Chestnykh 14 年之前
父節點
當前提交
7b585d44e1
共有 1 個文件被更改,包括 5 次插入3 次删除
  1. 5 3
      store.go

+ 5 - 3
store.go

@@ -40,13 +40,12 @@ func newStore(collectNum int, expiration int64) *store {
 // saveCaptcha saves the captcha id and the corresponding digits.
 // saveCaptcha saves the captcha id and the corresponding digits.
 func (s *store) saveCaptcha(id string, digits []byte) {
 func (s *store) saveCaptcha(id string, digits []byte) {
 	s.mu.Lock()
 	s.mu.Lock()
-	defer s.mu.Unlock()
 	s.ids[id] = digits
 	s.ids[id] = digits
 	s.exp.PushBack(expValue{time.Seconds(), id})
 	s.exp.PushBack(expValue{time.Seconds(), id})
 	s.numStored++
 	s.numStored++
+	s.mu.Unlock()
 	if s.numStored > s.collectNum {
 	if s.numStored > s.collectNum {
 		go s.collect()
 		go s.collect()
-		s.numStored = 0
 	}
 	}
 }
 }
 
 
@@ -79,14 +78,17 @@ func (s *store) collect() {
 	now := time.Seconds()
 	now := time.Seconds()
 	s.mu.Lock()
 	s.mu.Lock()
 	defer s.mu.Unlock()
 	defer s.mu.Unlock()
-	for e := s.exp.Front(); e != nil; e = e.Next() {
+	s.numStored = 0
+	for e := s.exp.Front(); e != nil; {
 		ev, ok := e.Value.(expValue)
 		ev, ok := e.Value.(expValue)
 		if !ok {
 		if !ok {
 			return
 			return
 		}
 		}
 		if ev.timestamp+s.expiration < now {
 		if ev.timestamp+s.expiration < now {
 			s.ids[ev.id] = nil, false
 			s.ids[ev.id] = nil, false
+			next := e.Next()
 			s.exp.Remove(e)
 			s.exp.Remove(e)
+			e = next
 		} else {
 		} else {
 			return
 			return
 		}
 		}