Fixed mutexes. Now program doesn't fall under high load.

Seems like an old expiry comparing didn't work as left thousands of expired captchas in a map, so it was replaced by a forgotten by me time.Since() function.
This commit is contained in:
Alexander Andreev 2022-06-27 02:48:55 +04:00
parent a3faf8b9c9
commit 149675cd86
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -53,13 +53,13 @@ func (cdb *CaptchaDB) SetExpiry(expire time.Duration) {
sleepFor := expiredScanInterval - (time.Duration(time.Now().Second()) % expiredScanInterval)
time.Sleep(sleepFor)
for id, captcha := range cdb.DB {
if time.Now().Sub(captcha.Expiry()) <= 0 {
cdb.Lock()
defer cdb.Unlock()
for id, captcha := range cdb.DB {
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
}