1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-12-02 20:22:52 +00:00

reuse buffers

This commit is contained in:
Laura
2025-08-31 23:46:22 +02:00
parent 2d65c2f484
commit db138324fa
7 changed files with 50 additions and 34 deletions

View File

@@ -27,6 +27,14 @@ var pool = sync.Pool{
},
}
func GetFreeBuffer() *bytes.Buffer {
buf := pool.Get().(*bytes.Buffer)
buf.Reset()
return buf
}
func NewStream(w http.ResponseWriter, ctx context.Context) (*Stream, error) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
@@ -104,10 +112,7 @@ func WriteChunk(w http.ResponseWriter, ctx context.Context, chunk any) error {
return err
}
buf := pool.Get().(*bytes.Buffer)
buf.Reset()
buf := GetFreeBuffer()
defer pool.Put(buf)
if err := json.NewEncoder(buf).Encode(chunk); err != nil {