Skip to content

Commit 3a336a4

Browse files
committed
read all response body & expire time vs duration.
1 parent 52c63a1 commit 3a336a4

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

hlsproxy/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ func (m *ManagerCtx) getFromCache(key string) (*utils.Cache, bool) {
2828
return entry, true
2929
}
3030

31-
func (m *ManagerCtx) saveToCache(key string, reader io.Reader, duration time.Duration) *utils.Cache {
31+
func (m *ManagerCtx) saveToCache(key string, reader io.Reader, expires time.Time) *utils.Cache {
3232
m.cacheMu.Lock()
33-
cache := utils.NewCache(time.Now().Add(duration))
33+
cache := utils.NewCache(expires)
3434
m.cache[key] = cache
3535
m.cacheMu.Unlock()
3636

hlsproxy/manager.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ func (m *ManagerCtx) ServePlaylist(w http.ResponseWriter, r *http.Request) {
6767
defer resp.Body.Close()
6868

6969
if resp.StatusCode < 200 && resp.StatusCode >= 300 {
70-
defer resp.Body.Close()
70+
// read all response body
71+
io.Copy(io.Discard, resp.Body)
7172

7273
m.logger.Err(err).Int("code", resp.StatusCode).Msg("invalid HTTP response")
7374
http.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway)
@@ -84,7 +85,7 @@ func (m *ManagerCtx) ServePlaylist(w http.ResponseWriter, r *http.Request) {
8485
var re = regexp.MustCompile(`(?m:^(https?\:\/\/[^\/]+)?\/)`)
8586
text := re.ReplaceAllString(string(buf), m.prefix)
8687

87-
cache = m.saveToCache(url, strings.NewReader(text), playlistExpiration)
88+
cache = m.saveToCache(url, strings.NewReader(data), time.Now().Add(playlistExpiration))
8889
}
8990

9091
w.Header().Set("Content-Type", "application/vnd.apple.mpegurl")
@@ -106,14 +107,16 @@ func (m *ManagerCtx) ServeMedia(w http.ResponseWriter, r *http.Request) {
106107
}
107108

108109
if resp.StatusCode < 200 && resp.StatusCode >= 300 {
109-
defer resp.Body.Close()
110+
// read all response body
111+
io.Copy(io.Discard, resp.Body)
112+
resp.Body.Close()
110113

111114
m.logger.Err(err).Int("code", resp.StatusCode).Msg("invalid HTTP response")
112115
http.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway)
113116
return
114117
}
115118

116-
cache = m.saveToCache(url, resp.Body, segmentExpiration)
119+
cache = m.saveToCache(url, resp.Body, time.Now().Add(segmentExpiration))
117120
}
118121

119122
w.Header().Set("Content-Type", "video/MP2T")

0 commit comments

Comments
 (0)