all repos — captcha @ 9ee08184699a934586df81a2c781378657d6a281

Go package captcha implements generation and verification of image and audio CAPTCHAs.

store: simplify locking.
Dmitry Chestnykh dmitry@codingrobots.com
Sat, 13 Oct 2012 12:24:14 +0200
commit

9ee08184699a934586df81a2c781378657d6a281

parent

cab7b0ddc6fd9250d28c4704b919c4185297ef38

1 files changed, 10 insertions(+), 10 deletions(-)

jump to
M store.gostore.go

@@ -36,7 +36,7 @@ }

// memoryStore is an internal store for captcha ids and their values. type memoryStore struct { - mu sync.RWMutex + sync.RWMutex digitsById map[string][]byte idByTime *list.List // Number of items stored since last collection.

@@ -60,26 +60,26 @@ return s

} func (s *memoryStore) Set(id string, digits []byte) { - s.mu.Lock() + s.Lock() s.digitsById[id] = digits s.idByTime.PushBack(idByTimeValue{time.Now(), id}) s.numStored++ if s.numStored <= s.collectNum { - s.mu.Unlock() + s.Unlock() return } - s.mu.Unlock() + s.Unlock() go s.collect() } func (s *memoryStore) Get(id string, clear bool) (digits []byte) { if !clear { // When we don't need to clear captcha, acquire read lock. - s.mu.RLock() - defer s.mu.RUnlock() + s.RLock() + defer s.RUnlock() } else { - s.mu.Lock() - defer s.mu.Unlock() + s.Lock() + defer s.Unlock() } digits, ok := s.digitsById[id] if !ok {

@@ -97,8 +97,8 @@ }

func (s *memoryStore) collect() { now := time.Now() - s.mu.Lock() - defer s.mu.Unlock() + s.Lock() + defer s.Unlock() s.numStored = 0 for e := s.idByTime.Front(); e != nil; { ev, ok := e.Value.(idByTimeValue)