Skip to content

Commit

Permalink
api: always encode timestamps wth RFC3339 (#1093)
Browse files Browse the repository at this point in the history
this fixes "lastRequest" in /v1/hlsmuxers/list that was encoded in a
wrong format
  • Loading branch information
aler9 committed Aug 23, 2022
1 parent 02d3dd9 commit 960cfb9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion internal/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ func TestAPIProtocolSpecificList(t *testing.T) {
case "hls":
var out struct {
Items map[string]struct {
Created string `json:"created"`
LastRequest string `json:"lastRequest"`
} `json:"items"`
}
Expand All @@ -480,7 +481,9 @@ func TestAPIProtocolSpecificList(t *testing.T) {
firstID = k
}

require.NotEqual(t, "", out.Items[firstID].LastRequest)
s := fmt.Sprintf("^%d-", time.Now().Year())
require.Regexp(t, s, out.Items[firstID].Created)
require.Regexp(t, s, out.Items[firstID].LastRequest)
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions internal/core/hls_muxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func newHLSMuxer(
ctxCancel: ctxCancel,
created: time.Now(),
lastRequestTime: func() *int64 {
v := time.Now().Unix()
v := time.Now().UnixNano()
return &v
}(),
chRequest: make(chan *hlsMuxerRequest),
Expand Down Expand Up @@ -244,7 +244,7 @@ func (m *hlsMuxer) run() {
case req := <-m.chAPIHLSMuxersList:
req.data.Items[m.name] = hlsServerAPIMuxersListItem{
Created: m.created,
LastRequest: time.Unix(atomic.LoadInt64(m.lastRequestTime), 0).String(),
LastRequest: time.Unix(0, atomic.LoadInt64(m.lastRequestTime)),
}
close(req.res)

Expand Down Expand Up @@ -408,7 +408,7 @@ func (m *hlsMuxer) runInner(innerCtx context.Context, innerReady chan struct{})
for {
select {
case <-closeCheckTicker.C:
t := time.Unix(atomic.LoadInt64(m.lastRequestTime), 0)
t := time.Unix(0, atomic.LoadInt64(m.lastRequestTime))
if m.remoteAddr != "" && time.Since(t) >= closeAfterInactivity {
m.ringBuffer.Close()
<-writerDone
Expand All @@ -427,7 +427,7 @@ func (m *hlsMuxer) runInner(innerCtx context.Context, innerReady chan struct{})
}

func (m *hlsMuxer) handleRequest(req *hlsMuxerRequest) func() *hls.MuxerFileResponse {
atomic.StoreInt64(m.lastRequestTime, time.Now().Unix())
atomic.StoreInt64(m.lastRequestTime, time.Now().UnixNano())

err := m.authenticate(req.ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/core/hls_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (nilWriter) Write(p []byte) (int, error) {

type hlsServerAPIMuxersListItem struct {
Created time.Time `json:"created"`
LastRequest string `json:"lastRequest"`
LastRequest time.Time `json:"lastRequest"`
}

type hlsServerAPIMuxersListData struct {
Expand Down

0 comments on commit 960cfb9

Please sign in to comment.