Browse Source

captcha: remove Collect method.

Garbage collection is an internal detail of a Store, and requiring to
export this method seem to be not very useful.

The default memory store, of course, still has garbage collection, but
it's not the unexported method, called in Set as usual.
Dmitry Chestnykh 14 years ago
parent
commit
c5afad979e
3 changed files with 10 additions and 33 deletions
  1. 4 16
      README.md
  2. 0 9
      captcha.go
  3. 6 8
      store.go

+ 4 - 16
README.md

@@ -91,16 +91,6 @@ var ErrNotFound = os.NewError("captcha with the given id not found")
 Functions
 ---------
 
-### func Collect
-
-	func Collect()
-	
-Collect deletes expired or used captchas from the internal storage. It is
-called automatically by New function every CollectNum generated captchas,
-but still exported to enable freeing memory manually if needed.
-
-Collection is launched in a new goroutine.
-
 ### func New
 
 	func New(length int) (id string)
@@ -248,12 +238,6 @@ type Store interface {
     // Get returns stored digits for the captcha id. Clear indicates
     // whether the captcha must be deleted from the store.
     Get(id string, clear bool) (digits []byte)
-
-    // Collect deletes expired captchas from the store.  For custom stores
-    // this method is not called automatically, it is only wired to the
-    // package's Collect function.  Custom stores must implement their own
-    // procedure for calling Collect, for example, in Set method.
-    Collect()
 }
 ```
 
@@ -261,6 +245,10 @@ An object implementing Store interface can be registered with SetCustomStore
 function to handle storage and retrieval of captcha ids and solutions for
 them, replacing the default memory store.
 
+It is the responsibility of an object to delete expired and used captchas
+when necessary (for example, the default memory store collects them in Set
+method after the certain amount of captchas has been stored.)
+
 ### func NewMemoryStore
 
 	func NewMemoryStore(collectNum int, expiration int64) Store

+ 0 - 9
captcha.go

@@ -166,12 +166,3 @@ func VerifyString(id string, digits string) bool {
 	}
 	return Verify(id, ns)
 }
-
-// Collect deletes expired or used captchas from the internal storage. It is
-// called automatically by New function every CollectNum generated captchas,
-// but still exported to enable freeing memory manually if needed.
-//
-// Collection is launched in a new goroutine.
-func Collect() {
-	go globalStore.Collect()
-}

+ 6 - 8
store.go

@@ -9,6 +9,10 @@ import (
 // An object implementing Store interface can be registered with SetCustomStore
 // function to handle storage and retrieval of captcha ids and solutions for
 // them, replacing the default memory store.
+//
+// It is the responsibility of an object to delete expired and used captchas
+// when necessary (for example, the default memory store collects them in Set
+// method after the certain amount of captchas has been stored.)
 type Store interface {
 	// Set sets the digits for the captcha id.
 	Set(id string, digits []byte)
@@ -16,12 +20,6 @@ type Store interface {
 	// Get returns stored digits for the captcha id. Clear indicates
 	// whether the captcha must be deleted from the store.
 	Get(id string, clear bool) (digits []byte)
-
-	// Collect deletes expired captchas from the store.  For custom stores
-	// this method is not called automatically, it is only wired to the
-	// package's Collect function.  Custom stores must implement their own
-	// procedure for calling Collect, for example, in Set method.
-	Collect()
 }
 
 // expValue stores timestamp and id of captchas. It is used in the list inside
@@ -67,7 +65,7 @@ func (s *memoryStore) Set(id string, digits []byte) {
 		return
 	}
 	s.mu.Unlock()
-	go s.Collect()
+	go s.collect()
 }
 
 func (s *memoryStore) Get(id string, clear bool) (digits []byte) {
@@ -93,7 +91,7 @@ func (s *memoryStore) Get(id string, clear bool) (digits []byte) {
 	return
 }
 
-func (s *memoryStore) Collect() {
+func (s *memoryStore) collect() {
 	now := time.Seconds()
 	s.mu.Lock()
 	defer s.mu.Unlock()