Skip to content

Instantly share code, notes, and snippets.

@Dostonlv
Created June 2, 2025 12:59
Show Gist options
  • Save Dostonlv/6a7ca817996d3d51fae7454f0bac0e9b to your computer and use it in GitHub Desktop.
Save Dostonlv/6a7ca817996d3d51fae7454f0bac0e9b to your computer and use it in GitHub Desktop.
package test
import (
jsoniter "github.com/json-iterator/go"
"github.com/gofiber/fiber/v2"
)
var bufferPool = sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
}
var json = jsoniter.ConfigFastest
func (r *contentRoutes) getContentByID(ctx *fiber.Ctx) error {
id := ctx.Params("id")
content, err := r.t.GetContentByID(ctx.Context(), id)
if err != nil {
r.l.Error(err, "http - v1 - getContentByID")
return errorResponse(ctx, http.StatusInternalServerError, "failed to get content by ID")
}
buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufferPool.Put(buf)
if err := json.NewEncoder(buf).Encode(content); err != nil {
r.l.Error(err, "marshal error")
return errorResponse(ctx, http.StatusInternalServerError, "failed to marshal content")
}
ctx.Set("Content-Type", "application/json")
return ctx.Send(buf.Bytes())
}
package test
import "github.com/gofiber/fiber/v2"
func (r *contentRoutes) getContentByID(ctx *fiber.Ctx) error {
id := ctx.Params("id")
content, err := r.t.GetContentByID(ctx.Context(), id)
if err != nil {
r.l.Error(err, "http - v1 - getContentByID")
return errorResponse(ctx, http.StatusInternalServerError, "failed to get content by ID")
}
return ctx.JSON(content)
}
@abdivasiyev
Copy link

buffer ni reset qilmasdan, default size bilan saqlab qo'ygan yaxshimasmi?

@abdivasiyev
Copy link

@Dostonlv manimcha normalniy, shu faqat default size bilan saqlaganiz yaxshi poolga

@abdivasiyev
Copy link

abdivasiyev commented Jun 2, 2025

adashdim, xullas, saqlashdan oldin Reset qilish kerak

 buf := bufferPool.Get().(*bytes.Buffer)
 defer bufferPool.Put(buf)
 defer buf.Reset()

mana shunaqa qilsez bo'ladi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment