Kaynağa Gözat

newStore: accept collectNum and expiration time.

Prevoiusly, Expiration and CollectNum were hard-coded into store.go.
This change moves these constants to captcha.go (renaming to Std*), and
makes newStore accept them as arguments.

This will enable setting these paraments by package users sometime in
the future, if needed, and also allows testing of the store garbage
collection.
Dmitry Chestnykh 14 yıl önce
ebeveyn
işleme
90e4f7499b
2 değiştirilmiş dosya ile 24 ekleme ve 17 silme
  1. 11 3
      captcha.go
  2. 13 14
      store.go

+ 11 - 3
captcha.go

@@ -8,12 +8,20 @@ import (
 	"os"
 	"os"
 )
 )
 
 
-// Standard number of digits in captcha.
-const StdLength = 6
+const (
+	// Standard number of digits in captcha.
+	StdLength = 6
+	// The number of captchas created that triggers garbage collection
+	StdCollectNum = 100
+	// Expiration time of captchas
+	StdExpiration = 2*60 // 2 minutes
+
+)
 
 
 var ErrNotFound = os.NewError("captcha with the given id not found")
 var ErrNotFound = os.NewError("captcha with the given id not found")
 
 
-var globalStore = newStore()
+// globalStore is a shared storage for captchas, generated by New function.
+var globalStore = newStore(StdCollectNum, StdExpiration)
 
 
 // RandomDigits returns a byte slice of the given length containing random
 // RandomDigits returns a byte slice of the given length containing random
 // digits in range 0-9.
 // digits in range 0-9.

+ 13 - 14
store.go

@@ -6,13 +6,6 @@ import (
 	"time"
 	"time"
 )
 )
 
 
-const (
-	// Expiration time for captchas
-	Expiration = 2 * 60 // 2 minutes
-	// The number of captchas created that triggers garbage collection
-	CollectNum = 100
-)
-
 // expValue stores timestamp and id of captchas. It is used in a list inside
 // expValue stores timestamp and id of captchas. It is used in a list inside
 // store for indexing generated captchas by timestamp to enable garbage
 // store for indexing generated captchas by timestamp to enable garbage
 // collection of expired captchas.
 // collection of expired captchas.
@@ -26,15 +19,21 @@ type store struct {
 	mu  sync.RWMutex
 	mu  sync.RWMutex
 	ids map[string][]byte
 	ids map[string][]byte
 	exp *list.List
 	exp *list.List
-	// Number of items stored after last collection
-	colNum int
+	// Number of items stored after last collection.
+	numStored int
+	// Number of saved items that triggers collection.
+	collectNum int
+	// Expiration time of captchas.
+	expiration int64
 }
 }
 
 
 // newStore initializes and returns a new store.
 // newStore initializes and returns a new store.
-func newStore() *store {
+func newStore(collectNum int, expiration int64) *store {
 	s := new(store)
 	s := new(store)
 	s.ids = make(map[string][]byte)
 	s.ids = make(map[string][]byte)
 	s.exp = list.New()
 	s.exp = list.New()
+	s.collectNum = collectNum
+	s.expiration = expiration
 	return s
 	return s
 }
 }
 
 
@@ -44,10 +43,10 @@ func (s *store) saveCaptcha(id string, digits []byte) {
 	defer s.mu.Unlock()
 	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.colNum++
-	if s.colNum > CollectNum {
+	s.numStored++
+	if s.numStored > s.collectNum {
 		go s.collect()
 		go s.collect()
-		s.colNum = 0
+		s.numStored = 0
 	}
 	}
 }
 }
 
 
@@ -85,7 +84,7 @@ func (s *store) collect() {
 		if !ok {
 		if !ok {
 			return
 			return
 		}
 		}
-		if ev.timestamp+Expiration < now {
+		if ev.timestamp+s.expiration < now {
 			s.ids[ev.id] = nil, false
 			s.ids[ev.id] = nil, false
 			s.exp.Remove(e)
 			s.exp.Remove(e)
 		} else {
 		} else {