From 59bd479ebee1c296d11908e7b3a619ee7bcdf5aa Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Wed, 17 Aug 2022 21:47:27 +0400 Subject: [PATCH] Image now passed by a pointer instead of value. Hope it will help with memory consumption a little. --- internal/captcha/dwelling_captcha.go | 5 +++-- internal/captcha/instance.go | 2 +- internal/handlers/handlers.go | 2 +- pkg/captcha/captcha.go | 2 +- pkg/captcha/db.go | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/internal/captcha/dwelling_captcha.go b/internal/captcha/dwelling_captcha.go index 9880208..7ec1b90 100644 --- a/internal/captcha/dwelling_captcha.go +++ b/internal/captcha/dwelling_captcha.go @@ -24,7 +24,7 @@ func NewDwellingCaptcha(expiry time.Duration) *DwellingCaptcha { ExpireIn: captcha.ExpiryDate(expiry)}} } -func (c *DwellingCaptcha) Image(style string) image.Image { +func (c *DwellingCaptcha) Image(style string) *image.Image { isDark := style == "dark" ctx := gg.NewContext(dwImageWidth, dwImageHeight) @@ -67,5 +67,6 @@ func (c *DwellingCaptcha) Image(style string) image.Image { ctx.Stroke() } - return ctx.Image() + i := ctx.Image() + return &i } diff --git a/internal/captcha/instance.go b/internal/captcha/instance.go index fea0322..1440647 100644 --- a/internal/captcha/instance.go +++ b/internal/captcha/instance.go @@ -17,7 +17,7 @@ func New(data string, captcha captcha.Captcha) (captcha.Captcha, captcha.ID) { 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) } diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 8ca27b9..567dd9a 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -39,7 +39,7 @@ func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) { 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) { diff --git a/pkg/captcha/captcha.go b/pkg/captcha/captcha.go index 2cd47cd..774df00 100644 --- a/pkg/captcha/captcha.go +++ b/pkg/captcha/captcha.go @@ -21,7 +21,7 @@ func NewIntAnswer() Answer { // Captcha interface that should be implemented by captcha. type Captcha interface { // Image generates and returns an image of captcha. - Image(style string) image.Image + Image(style string) *image.Image // GetAnswer returns a pregenerated answer. GetAnswer() Answer // Solve compares a stored answer with a passed one. diff --git a/pkg/captcha/db.go b/pkg/captcha/db.go index cfb6af8..1bca24b 100644 --- a/pkg/captcha/db.go +++ b/pkg/captcha/db.go @@ -87,7 +87,7 @@ func (cdb *InMemoryCaptchaDB) GetExpiry() time.Duration { } // 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() defer cdb.Unlock() if c, ok := cdb.DB[id]; ok {