diff --git a/pkg/captcha/db.go b/pkg/captcha/db.go index 992cf07..917afef 100644 --- a/pkg/captcha/db.go +++ b/pkg/captcha/db.go @@ -53,13 +53,13 @@ func (cdb *CaptchaDB) SetExpiry(expire time.Duration) { sleepFor := expiredScanInterval - (time.Duration(time.Now().Second()) % expiredScanInterval) time.Sleep(sleepFor) + cdb.Lock() for id, captcha := range cdb.DB { - if time.Now().Sub(captcha.Expiry()) <= 0 { - cdb.Lock() - defer cdb.Unlock() + if time.Since(captcha.Expiry()) >= cdb.ExpireIn { delete(cdb.DB, id) } } + cdb.Unlock() } }() } @@ -71,14 +71,16 @@ func (cdb *CaptchaDB) New(data string, captcha ICaptcha) (ICaptcha, ID) { id := NewID(data, captcha.GetAnswer()) cdb.Lock() - defer cdb.Unlock() cdb.DB[id] = captcha + cdb.Unlock() return captcha, id } // Image returns image for a captcha. func (cdb *CaptchaDB) 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 }