From 149675cd8603cd1872f8ae4db018028d986f7b97 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 27 Jun 2022 02:48:55 +0400 Subject: [PATCH] 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. --- pkg/captcha/db.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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 }