Skip to content

Commit

Permalink
Fixed memory leak.
Browse files Browse the repository at this point in the history
  • Loading branch information
DECE2183 committed Dec 14, 2023
1 parent af7464f commit e6c15c6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
24 changes: 19 additions & 5 deletions api/readseeker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func newReadSeaker(rc io.ReadCloser, totalSize int64) *HttpReadSeeker {
}

func (h *HttpReadSeeker) bufferNextFrame(size int64) {
if h.totalSize == int64(len(h.readBuffer)) {
return
}

if size < minBufferSize {
size = minBufferSize
}
Expand Down Expand Up @@ -72,13 +76,22 @@ func (h *HttpReadSeeker) bufferNextFrame(size int64) {
}

func (h *HttpReadSeeker) Close() error {
var err error

h.mux.Lock()
defer h.mux.Unlock()

h.bufferTimer.Stop()
close(h.readHappened)
h.readBuffer = nil
if h.totalSize > int64(len(h.readBuffer)) {
h.bufferTimer.Stop()
close(h.readHappened)
}

if h.source != nil {
err = h.source.Close()
}

return h.source.Close()
return err
}

func (h *HttpReadSeeker) Length() int64 {
Expand All @@ -91,7 +104,7 @@ func (h *HttpReadSeeker) Read(dest []byte) (n int, err error) {
readBufLen := int64(len(h.readBuffer))
destLen := int64(len(dest))

if readBufLen < h.totalSize {
if readBufLen < h.totalSize && h.source != nil {
// indicate buffering goroutine that Read was called
h.bufferTimer.Stop()
close(h.readHappened)
Expand Down Expand Up @@ -137,11 +150,12 @@ func (h *HttpReadSeeker) Read(dest []byte) (n int, err error) {
if err != nil {
if err == io.EOF {
h.source.Close()
h.source = nil
h.done = true
} else if err == http.ErrBodyReadAfterClose {
err = io.EOF
}
} else {
} else if h.totalSize > int64(len(h.readBuffer)) {
h.readHappened = make(chan struct{})
h.bufferTimer.Reset(readTimeout)
go h.bufferNextFrame(destLen * bufferFrameScale)
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZ
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/dece2183/go-stream-mp3 v0.0.0-20231213111702-da8128371595 h1:dktFlZ30DLQZPSJjWd/xRUnifFBm4MUMhvSbRh7FNhw=
github.com/dece2183/go-stream-mp3 v0.0.0-20231213111702-da8128371595/go.mod h1:hDd2a9oSj7kjEpBtln+NBgxfJEMPUBguc8RH5L/trPk=
github.com/dece2183/go-stream-mp3 v0.0.0-20231213115228-1a27407da02b h1:0VI6fBxSpYXiqKkJdQWUE+9vEDufv9pWZmb2NBPBEl0=
github.com/dece2183/go-stream-mp3 v0.0.0-20231213115228-1a27407da02b/go.mod h1:hDd2a9oSj7kjEpBtln+NBgxfJEMPUBguc8RH5L/trPk=
github.com/dece2183/go-stream-mp3 v0.0.0-20231213125202-78ebaccf0cc2 h1:fOItjuwhHY+AYybze6NlpJ1sEAHTDw2DbfjHV5mDUEc=
github.com/dece2183/go-stream-mp3 v0.0.0-20231213125202-78ebaccf0cc2/go.mod h1:hDd2a9oSj7kjEpBtln+NBgxfJEMPUBguc8RH5L/trPk=
github.com/dece2183/go-stream-mp3 v1.0.0 h1:0CQqd57+Aq1ojfhoCL/+o05rq/nOQ99/CWiEo2Geg60=
github.com/dece2183/go-stream-mp3 v1.0.0/go.mod h1:hDd2a9oSj7kjEpBtln+NBgxfJEMPUBguc8RH5L/trPk=
github.com/ebitengine/oto/v3 v3.0.0 h1:5F30UDJicFcJgVOJiBy7tRj/l7a+srL1IUYY5tWT3HU=
Expand Down
55 changes: 30 additions & 25 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,9 @@ func (m model) Update(message tea.Msg) (tea.Model, tea.Cmd) {
}
m.nextTrack()
case _PLAYER_PAUSE:
if m.player != nil {
m.player.Pause()
}
m.pauseTrack()
case _PLAYER_STOP:
if m.player != nil {
m.player.Pause()
m.player.Close()
m.player = nil
}
m.stopTrack()
}

// track progress update
Expand Down Expand Up @@ -507,13 +501,11 @@ func (m *model) playCurrentQueue(trackIndex int) {
m.player.Play()
return
}
} else {
m.player.Close()
m.player = nil
}
}

if len(m.playQueue) == 0 {
m.stopTrack()
return
}

Expand All @@ -532,11 +524,6 @@ func (m *model) playCurrentQueue(trackIndex int) {
}

func (m *model) prevTrack() {
if m.player != nil {
m.player.Close()
m.player = nil
}

if m.currentTrackIdx == 0 {
go programm.Send(_PLAYER_STOP)
return
Expand All @@ -552,11 +539,6 @@ func (m *model) prevTrack() {
}

func (m *model) nextTrack() {
if m.player != nil {
m.player.Close()
m.player = nil
}

if m.infinitePlaylist && m.currentTrackIdx+2 >= len(m.playQueue) {
tracks, err := m.client.StationTracks(api.MyWaveId, &m.playQueue[m.currentTrackIdx])
if err != nil {
Expand All @@ -582,6 +564,8 @@ func (m *model) nextTrack() {
}

func (m *model) playTrack(track *api.Track) {
m.stopTrack()

dowInfo, err := m.client.TrackDownloadInfo(track.Id)
if err != nil {
return
Expand Down Expand Up @@ -616,10 +600,6 @@ func (m *model) playTrack(track *api.Track) {
)
}

if m.trackWrapper.trackReader != nil {
m.trackWrapper.trackReader.Close()
}

m.trackWrapper.trackReader = trackReader
m.trackWrapper.decoder = decoder
m.trackWrapper.trackDurationMs = track.DurationMs
Expand All @@ -632,6 +612,30 @@ func (m *model) playTrack(track *api.Track) {
go m.client.PlayTrack(track, false)
}

func (m model) pauseTrack() {
if m.player == nil {
return
}
m.player.Pause()
}

func (m *model) stopTrack() {
if m.player == nil {
return
}

if m.player.IsPlaying() {
m.player.Pause()
}

m.player.Close()
m.player = nil

if m.trackWrapper.trackReader != nil {
m.trackWrapper.trackReader.Close()
}
}

func (w *trackReaderWrapper) Read(dest []byte) (n int, err error) {
if w.trackReader == nil {
err = io.EOF
Expand All @@ -647,6 +651,7 @@ func (w *trackReaderWrapper) Read(dest []byte) (n int, err error) {
}

if w.trackReader.IsDone() {
w.trackReader.Close()
w.trackReader = nil
go programm.Send(_PLAYER_NEXT)
} else if time.Since(w.lastUpdateTime) > time.Millisecond*33 {
Expand Down

0 comments on commit e6c15c6

Please sign in to comment.