Sfoglia il codice sorgente

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 anni fa
parent
commit
c5afad979e
3 ha cambiato i file con 10 aggiunte e 33 eliminazioni
  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
 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
 
 
 	func New(length int) (id string)
 	func New(length int) (id string)
@@ -248,12 +238,6 @@ type Store interface {
     // Get returns stored digits for the captcha id. Clear indicates
     // Get returns stored digits for the captcha id. Clear indicates
     // whether the captcha must be deleted from the store.
     // whether the captcha must be deleted from the store.
     Get(id string, clear bool) (digits []byte)
     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
 function to handle storage and retrieval of captcha ids and solutions for
 them, replacing the default memory store.
 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
 
 
 	func NewMemoryStore(collectNum int, expiration int64) Store
 	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)
 	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
 // An object implementing Store interface can be registered with SetCustomStore
 // function to handle storage and retrieval of captcha ids and solutions for
 // function to handle storage and retrieval of captcha ids and solutions for
 // them, replacing the default memory store.
 // 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 {
 type Store interface {
 	// Set sets the digits for the captcha id.
 	// Set sets the digits for the captcha id.
 	Set(id string, digits []byte)
 	Set(id string, digits []byte)
@@ -16,12 +20,6 @@ type Store interface {
 	// Get returns stored digits for the captcha id. Clear indicates
 	// Get returns stored digits for the captcha id. Clear indicates
 	// whether the captcha must be deleted from the store.
 	// whether the captcha must be deleted from the store.
 	Get(id string, clear bool) (digits []byte)
 	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
 // 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
 		return
 	}
 	}
 	s.mu.Unlock()
 	s.mu.Unlock()
-	go s.Collect()
+	go s.collect()
 }
 }
 
 
 func (s *memoryStore) Get(id string, clear bool) (digits []byte) {
 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
 	return
 }
 }
 
 
-func (s *memoryStore) Collect() {
+func (s *memoryStore) collect() {
 	now := time.Seconds()
 	now := time.Seconds()
 	s.mu.Lock()
 	s.mu.Lock()
 	defer s.mu.Unlock()
 	defer s.mu.Unlock()