Added more comments.

This commit is contained in:
Alexander Andreev 2023-01-12 04:41:09 +04:00
parent f3e68f4c41
commit e26bdd64f5
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
1 changed files with 9 additions and 5 deletions

View File

@ -8,6 +8,7 @@ import (
"git.arav.top/Arav/justcaptcha/pkg/captcha"
)
// InMemoryCaptchaDB implementation that lives in a memory (map).
type InMemoryCaptchaDB struct {
sync.Mutex
@ -17,7 +18,8 @@ type InMemoryCaptchaDB struct {
}
// NewInMemoryCaptchaDB returns an initialised instance of an InMemoryCaptchaDB.
// An expiry duration is a time for how long CAPTCHA will be valid.
// An expiry is a scan interval for expired CAPTCHAs (if passed a longer one,
// resets to a default (captcha.DefaultExpiredScanInterval)).
func NewInMemoryCaptchaDB(expiry time.Duration) *InMemoryCaptchaDB {
db := &InMemoryCaptchaDB{
db: make(map[captcha.ID]captcha.Captcha),
@ -34,7 +36,7 @@ func NewInMemoryCaptchaDB(expiry time.Duration) *InMemoryCaptchaDB {
return db
}
// New accepts a CAPTHA instance, generates an ID and store it in a database.
// New accepts a CAPTCHA instance, generates an ID and store it in a database.
// A data string is an additional random data used to generate an ID,
// e.g. an IP-address.
func (imcdb *InMemoryCaptchaDB) New(data string, cptcha captcha.Captcha) (captcha.Captcha, captcha.ID) {
@ -47,12 +49,13 @@ func (imcdb *InMemoryCaptchaDB) New(data string, cptcha captcha.Captcha) (captch
return cptcha, id
}
// GetExpiry returns time for how long CAPTCHA will last.
// GetExpiry returns an expiry for a CAPTCHA.
func (imcdb *InMemoryCaptchaDB) GetExpiry() time.Duration {
return imcdb.expiry
}
// SetExpiry changes an expiry duration to a new one.
// SetExpiry changes an expiry for a CAPTCHA and a scan interval. Scan interval
// cannot be longer than a default, so if it is, then resets to a default.
func (imcdb *InMemoryCaptchaDB) SetExpiry(expiry time.Duration) {
imcdb.expiry = expiry
if expiry < captcha.DefaultExpiredScanInterval {
@ -62,7 +65,8 @@ func (imcdb *InMemoryCaptchaDB) SetExpiry(expiry time.Duration) {
}
}
// Image returns a freshly generated image for a CAPTCHA.
// Image returns a freshly generated image for a CAPTCHA with style if
// applicable.
func (imcdb *InMemoryCaptchaDB) Image(id captcha.ID, style string) *image.Image {
imcdb.Lock()
defer imcdb.Unlock()