Image now passed by a pointer instead of value. Hope it will help with memory consumption a little.

This commit is contained in:
Alexander Andreev 2022-08-17 21:47:27 +04:00
parent 18431e0001
commit 59bd479ebe
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
5 changed files with 7 additions and 6 deletions

View File

@ -24,7 +24,7 @@ func NewDwellingCaptcha(expiry time.Duration) *DwellingCaptcha {
ExpireIn: captcha.ExpiryDate(expiry)}} ExpireIn: captcha.ExpiryDate(expiry)}}
} }
func (c *DwellingCaptcha) Image(style string) image.Image { func (c *DwellingCaptcha) Image(style string) *image.Image {
isDark := style == "dark" isDark := style == "dark"
ctx := gg.NewContext(dwImageWidth, dwImageHeight) ctx := gg.NewContext(dwImageWidth, dwImageHeight)
@ -67,5 +67,6 @@ func (c *DwellingCaptcha) Image(style string) image.Image {
ctx.Stroke() ctx.Stroke()
} }
return ctx.Image() i := ctx.Image()
return &i
} }

View File

@ -17,7 +17,7 @@ func New(data string, captcha captcha.Captcha) (captcha.Captcha, captcha.ID) {
return captchaDb.New(data, captcha) return captchaDb.New(data, captcha)
} }
func Image(id captcha.ID, style string) (image.Image, error) { func Image(id captcha.ID, style string) (*image.Image, error) {
return captchaDb.Image(id, style) return captchaDb.Image(id, style)
} }

View File

@ -39,7 +39,7 @@ func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Disposition", "inline; filename=\""+string(captchaID)+"\"") w.Header().Add("Content-Disposition", "inline; filename=\""+string(captchaID)+"\"")
png.Encode(w, captchaImage) png.Encode(w, *captchaImage)
} }
func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) { func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {

View File

@ -21,7 +21,7 @@ func NewIntAnswer() Answer {
// Captcha interface that should be implemented by captcha. // Captcha interface that should be implemented by captcha.
type Captcha interface { type Captcha interface {
// Image generates and returns an image of captcha. // Image generates and returns an image of captcha.
Image(style string) image.Image Image(style string) *image.Image
// GetAnswer returns a pregenerated answer. // GetAnswer returns a pregenerated answer.
GetAnswer() Answer GetAnswer() Answer
// Solve compares a stored answer with a passed one. // Solve compares a stored answer with a passed one.

View File

@ -87,7 +87,7 @@ func (cdb *InMemoryCaptchaDB) GetExpiry() time.Duration {
} }
// Image returns image for a captcha. // Image returns image for a captcha.
func (cdb *InMemoryCaptchaDB) Image(id ID, style string) (image.Image, error) { func (cdb *InMemoryCaptchaDB) Image(id ID, style string) (*image.Image, error) {
cdb.Lock() cdb.Lock()
defer cdb.Unlock() defer cdb.Unlock()
if c, ok := cdb.DB[id]; ok { if c, ok := cdb.DB[id]; ok {