Zorch review, use session.ID to count running stream

This commit is contained in:
Zorglube 2021-03-20 22:32:13 +01:00
parent b2eea597bd
commit efbb284eb8
2 changed files with 10 additions and 11 deletions

View File

@ -425,9 +425,9 @@ func handleLive(w http.ResponseWriter, r *http.Request) {
cursor := ch.que.Latest() cursor := ch.que.Latest()
session, _ := sstore.Get(r, "moviesession") session, _ := sstore.Get(r, "moviesession")
stats.addViewer(session) stats.addViewer(session.ID)
avutil.CopyFile(muxer, cursor) avutil.CopyFile(muxer, cursor)
stats.removeViewer(session) stats.removeViewer(session.ID)
} else { } else {
// Maybe HTTP_204 is better than HTTP_404 // Maybe HTTP_204 is better than HTTP_404
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)

View File

@ -4,7 +4,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/gorilla/sessions"
"github.com/zorchenhimer/MovieNight/common" "github.com/zorchenhimer/MovieNight/common"
) )
@ -16,22 +15,22 @@ type streamStats struct {
mutex sync.Mutex mutex sync.Mutex
streamStart time.Time streamStart time.Time
streamLive bool // True if live streamLive bool // True if live
viewers map[*sessions.Session]int viewers map[string]int
maxViewers int maxViewers int
} }
func (s *streamStats) addViewer(session *sessions.Session) { func (s *streamStats) addViewer(id string) {
s.mutex.Lock() s.mutex.Lock()
s.viewers[session] = len(s.viewers) s.viewers[id] = len(s.viewers)
size := len(s.viewers) size := len(s.viewers)
s.updateMaxViewers(size) s.updateMaxViewers(size)
s.mutex.Unlock() s.mutex.Unlock()
common.LogDebugf("[stats] %d viewer(s) connected\n", size) common.LogDebugf("[stats] %d viewer(s) connected\n", size)
} }
func (s *streamStats) removeViewer(session *sessions.Session) { func (s *streamStats) removeViewer(id string) {
s.mutex.Lock() s.mutex.Lock()
delete(s.viewers, session) delete(s.viewers, id)
s.mutex.Unlock() s.mutex.Unlock()
common.LogDebugf("[stats] One viewer left the stream\n") common.LogDebugf("[stats] One viewer left the stream\n")
@ -47,8 +46,8 @@ func (s *streamStats) resetViewers() {
s.viewers = sessionsMapNew() s.viewers = sessionsMapNew()
} }
func sessionsMapNew() map[*sessions.Session]int { func sessionsMapNew() map[string]int {
return make(map[*sessions.Session]int) return make(map[string]int)
} }
func newStreamStats() streamStats { func newStreamStats() streamStats {
@ -132,7 +131,7 @@ func (s *streamStats) getMaxViewerCount() int {
return s.maxViewers return s.maxViewers
} }
func (s *streamStats) getViewers() map[*sessions.Session]int { func (s *streamStats) getViewers() map[string]int {
s.mutex.Lock() s.mutex.Lock()
defer s.mutex.Unlock() defer s.mutex.Unlock()