diff --git a/internal/handlers/mix.go b/internal/handlers/mix.go index 1e568fcd..876cf417 100644 --- a/internal/handlers/mix.go +++ b/internal/handlers/mix.go @@ -101,7 +101,7 @@ func Mix(app core.App) echo.HandlerFunc { } // Get current filetype encoder - entry.Encoder, err = fileType.NewEncoder(&entry.Buffer, entry.Format) + entry.Encoder, err = fileType.NewEncoder(entry.Buffer, entry.Format) if err != nil { panic(err) } @@ -168,7 +168,7 @@ func Mix(app core.App) echo.HandlerFunc { // Write buffered stream data to client c.Response().WriteHeader(http.StatusPartialContent) - if _, err := io.Copy(c.Response(), &entry.Buffer); err != nil { + if _, err := io.Copy(c.Response(), entry.Buffer); err != nil { if !errors.Is(err, syscall.EPIPE) && !errors.Is(err, syscall.ECONNRESET) { panic(err) } diff --git a/internal/stream/stream_cache/entry.go b/internal/stream/stream_cache/entry.go index c7f3b51d..398f2c8b 100644 --- a/internal/stream/stream_cache/entry.go +++ b/internal/stream/stream_cache/entry.go @@ -33,7 +33,7 @@ type Entry struct { Mix beep.Streamer Format beep.Format - Buffer bytes.Buffer + Buffer *bytes.Buffer Encoder encoder.Encoder Mu sync.Mutex @@ -47,6 +47,7 @@ func NewEntry(remoteAddr, preset string) *Entry { entry := &Entry{ RemoteAddr: remoteAddr, Preset: preset, + Buffer: bytes.NewBuffer(make([]byte, 0, 3*1024*1024)), Created: time.Now(), } entry.Accessed = entry.Created @@ -63,6 +64,10 @@ func (e *Entry) Close() error { activeStreamMetrics.Dec() + defer func() { + e.Buffer = nil + }() + return errors.Join( e.Streams.Close(), e.Encoder.Close(),