InMemoryCaptchaDB was renamed to InMemoryDB.

This commit is contained in:
Alexander Andreev 2023-03-05 00:30:24 +04:00
parent fe2c3c8e59
commit 5a16c4d678
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 16 additions and 16 deletions

View File

@ -8,8 +8,8 @@ import (
"git.arav.su/Arav/justcaptcha/pkg/captcha"
)
// InMemoryCaptchaDB implementation that lives in a memory (map).
type InMemoryCaptchaDB struct {
// InMemoryDB implementation that lives in a memory (map).
type InMemoryDB struct {
sync.Mutex
db map[captcha.ID]captcha.Captcha
@ -17,11 +17,11 @@ type InMemoryCaptchaDB struct {
expiryScanInterval time.Duration
}
// NewInMemoryCaptchaDB returns an initialised instance of an InMemoryCaptchaDB.
// NewInMemoryDB returns an initialised instance of an InMemoryDB.
// An expiry is a scan interval for expired CAPTCHAs (if passed a longer one,
// resets to a default (captcha.DefaultExpiredScanInterval)).
func NewInMemoryCaptchaDB(expiry time.Duration) *InMemoryCaptchaDB {
db := &InMemoryCaptchaDB{
func NewInMemoryDB(expiry time.Duration) *InMemoryDB {
db := &InMemoryDB{
db: make(map[captcha.ID]captcha.Captcha),
expiry: expiry}
@ -39,7 +39,7 @@ func NewInMemoryCaptchaDB(expiry time.Duration) *InMemoryCaptchaDB {
// New accepts a CAPTCHA instance, generates an ID and store it in a database.
// A data string is an additional random data used to generate an ID,
// e.g. an IP-address.
func (imcdb *InMemoryCaptchaDB) New(data string, cptcha captcha.Captcha) (captcha.Captcha, captcha.ID) {
func (imcdb *InMemoryDB) New(data string, cptcha captcha.Captcha) (captcha.Captcha, captcha.ID) {
id := captcha.NewID(data, cptcha.Answer())
imcdb.Lock()
@ -50,13 +50,13 @@ func (imcdb *InMemoryCaptchaDB) New(data string, cptcha captcha.Captcha) (captch
}
// GetExpiry returns an expiry for a CAPTCHA.
func (imcdb *InMemoryCaptchaDB) GetExpiry() time.Duration {
func (imcdb *InMemoryDB) GetExpiry() time.Duration {
return imcdb.expiry
}
// SetExpiry changes an expiry for a CAPTCHA and a scan interval. Scan interval
// cannot be longer than a default, so if it is, then resets to a default.
func (imcdb *InMemoryCaptchaDB) SetExpiry(expiry time.Duration) {
func (imcdb *InMemoryDB) SetExpiry(expiry time.Duration) {
imcdb.expiry = expiry
if expiry < captcha.DefaultExpiredScanInterval {
imcdb.expiryScanInterval = expiry
@ -67,7 +67,7 @@ func (imcdb *InMemoryCaptchaDB) SetExpiry(expiry time.Duration) {
// Image returns a freshly generated image for a CAPTCHA with style if
// applicable.
func (imcdb *InMemoryCaptchaDB) Image(id captcha.ID, style string) *image.Image {
func (imcdb *InMemoryDB) Image(id captcha.ID, style string) *image.Image {
imcdb.Lock()
defer imcdb.Unlock()
if c, ok := imcdb.db[id]; ok {
@ -78,7 +78,7 @@ func (imcdb *InMemoryCaptchaDB) Image(id captcha.ID, style string) *image.Image
// Solve compares given answer with a stored one and if failed
// deletes a CAPTCHA from database.
func (imcdb *InMemoryCaptchaDB) Solve(id captcha.ID, answer captcha.Answer) bool {
func (imcdb *InMemoryDB) Solve(id captcha.ID, answer captcha.Answer) bool {
imcdb.Lock()
defer imcdb.Unlock()
if c, ok := imcdb.db[id]; ok {
@ -93,7 +93,7 @@ func (imcdb *InMemoryCaptchaDB) Solve(id captcha.ID, answer captcha.Answer) bool
// IsSolved checks if CAPTCHA was solved and removes it
// from a database.
func (imcdb *InMemoryCaptchaDB) IsSolved(id captcha.ID) bool {
func (imcdb *InMemoryDB) IsSolved(id captcha.ID) bool {
imcdb.Lock()
defer imcdb.Unlock()
if c, ok := imcdb.db[id]; ok {
@ -104,14 +104,14 @@ func (imcdb *InMemoryCaptchaDB) IsSolved(id captcha.ID) bool {
}
// Remove a CAPTCHA from a database.
func (imcdb *InMemoryCaptchaDB) Remove(id captcha.ID) {
func (imcdb *InMemoryDB) Remove(id captcha.ID) {
imcdb.Lock()
defer imcdb.Unlock()
delete(imcdb.db, id)
}
// cleanExpired removes expired CAPTCHAs in a loop.
func (imcdb *InMemoryCaptchaDB) cleanExpired() {
func (imcdb *InMemoryDB) cleanExpired() {
for {
sleepFor := imcdb.expiryScanInterval - (time.Duration(time.Now().Second()) % imcdb.expiryScanInterval)
time.Sleep(sleepFor)
@ -126,9 +126,9 @@ func (imcdb *InMemoryCaptchaDB) cleanExpired() {
}
}
// An instance of InMemoryCaptchaDB
// An instance of InMemoryDB
var imcdb = NewInMemoryCaptchaDB(captcha.DefaultExpiredScanInterval)
var imcdb = NewInMemoryDB(captcha.DefaultExpiredScanInterval)
func GetExpiry() time.Duration {
return imcdb.GetExpiry()

View File

@ -40,7 +40,7 @@ func TestInMemDBDefaultInstance(t *testing.T) {
}
func TestInMemDBNewInstance(t *testing.T) {
db := inmemdb.NewInMemoryCaptchaDB(expiry)
db := inmemdb.NewInMemoryDB(expiry)
if db.GetExpiry() != expiry {
t.Errorf("expected expiry %v, but got %v", expiry, db.GetExpiry())