Created
June 2, 2025 12:59
-
-
Save Dostonlv/6a7ca817996d3d51fae7454f0bac0e9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
@Dostonlv manimcha normalniy, shu faqat default size bilan saqlaganiz yaxshi poolga
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
buffer ni reset qilmasdan, default size bilan saqlab qo'ygan yaxshimasmi?