From 925d48c10dca9e1b2d126e6fdba762be2c60b9a2 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 30 Jul 2022 15:39:19 +0400 Subject: [PATCH] ICaptchaDB interface renamed to CaptchaDB. And implementation was renamed to InMemoryCaptchaDB. Composition of sync.Mutex was moved to the top of struct. Methods was rearranged to resemble order from CaptchaDB interface. --- pkg/captcha/db.go | 55 ++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/pkg/captcha/db.go b/pkg/captcha/db.go index 5095242..c510864 100644 --- a/pkg/captcha/db.go +++ b/pkg/captcha/db.go @@ -25,8 +25,8 @@ func NewID(additionalData string, answer Answer) ID { return ID(base64.RawURLEncoding.EncodeToString(idHash.Sum(nil))) } -type ICaptchaDB interface { - New(data string) (ICaptcha, ID) +type CaptchaDB interface { + New(data string) (Captcha, ID) SetExpiry(expiry time.Duration) GetExpiry() time.Duration Image(id ID) (*image.Image, error) @@ -34,15 +34,29 @@ type ICaptchaDB interface { IsSolved(id ID) bool } -type CaptchaDB struct { - DB map[ID]ICaptcha - ExpireIn time.Duration +type InMemoryCaptchaDB struct { sync.Mutex + + DB map[ID]Captcha + ExpireIn time.Duration +} + +// New accepts an Captcha instance, generates an ID and store it in a database. +// `data` string is an additional random data used to generate an ID, +// e.g. IP-address. +func (cdb *InMemoryCaptchaDB) New(data string, captcha Captcha) (Captcha, ID) { + id := NewID(data, captcha.GetAnswer()) + + cdb.Lock() + cdb.DB[id] = captcha + cdb.Unlock() + + return captcha, id } // SetExpiry stores expire value and starts a goroutine -// that checks for expired CAPTCHAs every minute. -func (cdb *CaptchaDB) SetExpiry(expire time.Duration) { +// that checks for expired CAPTCHAs. +func (cdb *InMemoryCaptchaDB) SetExpiry(expire time.Duration) { cdb.ExpireIn = expire if expire < expiredScanInterval { expiredScanInterval = expire @@ -64,32 +78,24 @@ func (cdb *CaptchaDB) SetExpiry(expire time.Duration) { }() } -// New accepts an ICaptcha instance, generates an ID and store it in a database. -// `data` string is an additional random data used to generate an ID, -// e.g. IP-address. -func (cdb *CaptchaDB) New(data string, captcha ICaptcha) (ICaptcha, ID) { - id := NewID(data, captcha.GetAnswer()) - - cdb.Lock() - cdb.DB[id] = captcha - cdb.Unlock() - - return captcha, id +// GetExpiry returns time for how long captcha will last. +func (cdb *InMemoryCaptchaDB) GetExpiry() time.Duration { + return cdb.ExpireIn } // Image returns image for a captcha. -func (cdb *CaptchaDB) Image(id ID, style string) (*image.Image, error) { +func (cdb *InMemoryCaptchaDB) Image(id ID, style string) (image.Image, error) { cdb.Lock() defer cdb.Unlock() if c, ok := cdb.DB[id]; ok { - return c.GetImage(style), nil + return c.Image(style), nil } return nil, errorNotFound } // Solve compares given answer with a stored one and if failed // deletes a captcha from database. -func (cdb *CaptchaDB) Solve(id ID, answer Answer) (bool, error) { +func (cdb *InMemoryCaptchaDB) Solve(id ID, answer Answer) (bool, error) { cdb.Lock() defer cdb.Unlock() if c, ok := cdb.DB[id]; ok { @@ -104,7 +110,7 @@ func (cdb *CaptchaDB) Solve(id ID, answer Answer) (bool, error) { // IsSolved checks if captcha was solved and removes it // from a database. -func (cdb *CaptchaDB) IsSolved(id ID) (bool, error) { +func (cdb *InMemoryCaptchaDB) IsSolved(id ID) (bool, error) { cdb.Lock() defer cdb.Unlock() if c, ok := cdb.DB[id]; ok { @@ -114,8 +120,3 @@ func (cdb *CaptchaDB) IsSolved(id ID) (bool, error) { } return false, errorNotFound } - -// GetExpiry returns time for how long captcha will last. -func (cdb *CaptchaDB) GetExpiry() time.Duration { - return cdb.ExpireIn -}