From b0d5f9d9d01f1ad2f635ccdefc23d144bc9931d6 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 27 Jun 2022 01:03:28 +0400 Subject: [PATCH] Embed sync.Mutex in CaptchaDB struct in order to directly call its methods. --- pkg/captcha/db.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/captcha/db.go b/pkg/captcha/db.go index b9fa5e7..c58da36 100644 --- a/pkg/captcha/db.go +++ b/pkg/captcha/db.go @@ -37,7 +37,7 @@ type ICaptchaDB interface { type CaptchaDB struct { DB map[ID]ICaptcha ExpireIn time.Duration - mut sync.Mutex + sync.Mutex } // SetExpiry stores expire value and starts a goroutine @@ -68,8 +68,8 @@ func (cdb *CaptchaDB) SetExpiry(expire time.Duration) { func (cdb *CaptchaDB) New(data string, captcha ICaptcha) (ICaptcha, ID) { id := NewID(data, captcha.GetAnswer()) - cdb.mut.Lock() - defer cdb.mut.Unlock() + cdb.Lock() + defer cdb.Unlock() cdb.DB[id] = captcha return captcha, id @@ -86,8 +86,8 @@ func (cdb *CaptchaDB) Image(id ID) (*image.Image, error) { // 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) { - cdb.mut.Lock() - defer cdb.mut.Unlock() + cdb.Lock() + defer cdb.Unlock() if c, ok := cdb.DB[id]; ok { ok = c.Solve(answer) if !ok { @@ -101,8 +101,8 @@ 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) { - cdb.mut.Lock() - defer cdb.mut.Unlock() + cdb.Lock() + defer cdb.Unlock() if c, ok := cdb.DB[id]; ok { ok = c.IsSolved() delete(cdb.DB, id)