Refactor of db.go to change expiration and expire to expiry. ExpireInterval field renamed to ExpireIn.

This commit is contained in:
Alexander Andreev 2022-06-26 20:49:43 +04:00
parent 2e3d41a628
commit 1dd070abd4
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -13,23 +13,23 @@ var expiredScanInterval = 60 * time.Second
type ICaptchaDB interface { type ICaptchaDB interface {
New(data string) (ICaptcha, ID) New(data string) (ICaptcha, ID)
SetExpiration(expire time.Duration) SetExpiry(expiry time.Duration)
Image(id ID) (*image.Image, error) Image(id ID) (*image.Image, error)
Solve(id ID, answer Answer) (bool, error) Solve(id ID, answer Answer) (bool, error)
IsSolved(id ID) bool IsSolved(id ID) bool
GetExpireInterval() time.Duration GetExpiry() time.Duration
} }
type CaptchaDB struct { type CaptchaDB struct {
DB map[ID]ICaptcha DB map[ID]ICaptcha
ExpireInterval time.Duration ExpireIn time.Duration
mut sync.Mutex mut sync.Mutex
} }
// SetExpiration stores expire value and starts a goroutine // SetExpiry stores expire value and starts a goroutine
// that checks for expired CAPTCHAs every minute. // that checks for expired CAPTCHAs every minute.
func (cdb *CaptchaDB) SetExpiration(expire time.Duration) { func (cdb *CaptchaDB) SetExpiry(expire time.Duration) {
cdb.ExpireInterval = expire cdb.ExpireIn = expire
if expire < expiredScanInterval { if expire < expiredScanInterval {
expiredScanInterval = expire expiredScanInterval = expire
} }
@ -40,7 +40,7 @@ func (cdb *CaptchaDB) SetExpiration(expire time.Duration) {
time.Sleep(sleepFor) time.Sleep(sleepFor)
for id, captcha := range cdb.DB { for id, captcha := range cdb.DB {
if time.Now().Sub(captcha.Expire()) <= 0 { if time.Now().Sub(captcha.Expiry()) <= 0 {
delete(cdb.DB, id) delete(cdb.DB, id)
} }
} }
@ -104,5 +104,5 @@ func (cdb *CaptchaDB) IsSolved(id ID) (bool, error) {
} }
func (cdb *CaptchaDB) GetExpireInterval() time.Duration { func (cdb *CaptchaDB) GetExpireInterval() time.Duration {
return cdb.ExpireInterval return cdb.ExpireIn
} }