Skip to content

Commit 97118ac

Browse files
committed
go use time for last request.
1 parent fb61933 commit 97118ac

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

hls/manager.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ const playlistTimeout = 20 * time.Second
2727
const hlsMinimumSegments = 2
2828

2929
// how long must be active stream idle to be considered as dead
30-
const activeIdleTimeout = 12
30+
const activeIdleTimeout = 12 * time.Second
3131

3232
// how long must be iactive stream idle to be considered as dead
33-
const inactiveIdleTimeout = 24
33+
const inactiveIdleTimeout = 24 * time.Second
3434

3535
type ManagerCtx struct {
3636
logger zerolog.Logger
@@ -40,7 +40,7 @@ type ManagerCtx struct {
4040

4141
cmd *exec.Cmd
4242
tempdir string
43-
lastRequest int64
43+
lastRequest time.Time
4444

4545
sequence int
4646
playlist string
@@ -87,7 +87,7 @@ func (m *ManagerCtx) Start() error {
8787
}
8888

8989
m.active = false
90-
m.lastRequest = time.Now().Unix()
90+
m.lastRequest = time.Now()
9191

9292
m.sequence = 0
9393
m.playlist = ""
@@ -166,12 +166,14 @@ func (m *ManagerCtx) Stop() {
166166
}
167167

168168
func (m *ManagerCtx) Cleanup() {
169-
diff := time.Now().Unix() - m.lastRequest
169+
m.mu.Lock()
170+
diff := time.Since(m.lastRequest)
170171
stop := m.active && diff > activeIdleTimeout || !m.active && diff > inactiveIdleTimeout
172+
m.mu.Unlock()
171173

172174
m.logger.Debug().
173-
Int64("last_request", m.lastRequest).
174-
Int64("diff", diff).
175+
Time("last_request", m.lastRequest).
176+
Dur("diff", diff).
175177
Bool("active", m.active).
176178
Bool("stop", stop).
177179
Msg("performing cleanup")
@@ -182,7 +184,10 @@ func (m *ManagerCtx) Cleanup() {
182184
}
183185

184186
func (m *ManagerCtx) ServePlaylist(w http.ResponseWriter, r *http.Request) {
185-
m.lastRequest = time.Now().Unix()
187+
m.mu.Lock()
188+
m.lastRequest = time.Now()
189+
m.mu.Unlock()
190+
186191
playlist := m.playlist
187192

188193
if m.cmd == nil {
@@ -227,7 +232,10 @@ func (m *ManagerCtx) ServeMedia(w http.ResponseWriter, r *http.Request) {
227232
return
228233
}
229234

230-
m.lastRequest = time.Now().Unix()
235+
m.mu.Lock()
236+
m.lastRequest = time.Now()
237+
m.mu.Unlock()
238+
231239
w.Header().Set("Content-Type", "application/vnd.apple.mpegurl")
232240
w.Header().Set("Cache-Control", "no-cache")
233241
http.ServeFile(w, r, path)

0 commit comments

Comments
 (0)