Browse Source

HLS Downloader

- refactor map to lru cache
Alexey Kim 10 months ago
parent
commit
84e1f35dd5
1 changed files with 4 additions and 3 deletions
  1. 4 3
      internal/download/hls.go

+ 4 - 3
internal/download/hls.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"errors"
 	"github.com/grafov/m3u8"
+	"github.com/hashicorp/golang-lru/v2/expirable"
 	"github.com/rs/zerolog/log"
 	"io"
 	"net/http"
@@ -75,7 +76,7 @@ func (d *HLS) Start() chan []byte {
 	var (
 		c         = make(chan []byte)
 		medialist *m3u8.MediaPlaylist
-		segments  = make(map[uint64]any)
+		segments  = expirable.NewLRU[uint64, *m3u8.MediaSegment](64, nil, time.Minute)
 		err       error
 	)
 
@@ -109,11 +110,11 @@ func (d *HLS) Start() chan []byte {
 						continue
 					}
 
-					if _, ok := segments[seq.SeqId]; ok {
+					if _, ok := segments.Get(seq.SeqId); ok {
 						continue
 					}
 
-					segments[seq.SeqId] = 0
+					segments.Add(seq.SeqId, seq)
 
 					var (
 						uri      = seq.URI