fix: fetch summary timeout

This commit is contained in:
langhuihui
2023-09-06 13:36:19 +08:00
parent f89b75ecb1
commit 934d3cf9d4
7 changed files with 144 additions and 168 deletions

View File

@@ -92,6 +92,10 @@ func (bt *Base[T, F]) SetStuff(stuff ...any) {
} }
} }
func (bt *Base[T, F]) Dispose() {
bt.Value.Broadcast()
}
type Track interface { type Track interface {
GetName() string GetName() string
GetBPS() int GetBPS() int

View File

@@ -2,7 +2,6 @@ package engine
import ( import (
"encoding/json" "encoding/json"
"sort"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -88,30 +87,6 @@ var StreamFSM = [len(StateNames)]map[StreamAction]StreamState{
// Streams 所有的流集合 // Streams 所有的流集合
var Streams util.Map[string, *Stream] var Streams util.Map[string, *Stream]
type StreamList []*Stream
func (l StreamList) Len() int {
return len(l)
}
func (l StreamList) Less(i, j int) bool {
return l[i].Path < l[j].Path
}
func (l StreamList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
func (l StreamList) Sort() {
sort.Sort(l)
}
func GetSortedStreamList() StreamList {
result := StreamList(Streams.ToList())
result.Sort()
return result
}
func FilterStreams[T IPublisher]() (ss []*Stream) { func FilterStreams[T IPublisher]() (ss []*Stream) {
Streams.Range(func(_ string, s *Stream) { Streams.Range(func(_ string, s *Stream) {
if _, ok := s.Publisher.(T); ok { if _, ok := s.Publisher.(T); ok {
@@ -350,14 +325,18 @@ func (r *Stream) action(action StreamAction) (ok bool) {
r.timeout.Reset(r.DelayCloseTimeout) r.timeout.Reset(r.DelayCloseTimeout)
} }
case STATE_CLOSED: case STATE_CLOSED:
Streams.Delete(r.Path)
r.timeout.Stop()
r.Subscribers.Dispose()
for !r.actionChan.Close() { for !r.actionChan.Close() {
// 等待channel发送完毕伪自旋锁 // 等待channel发送完毕伪自旋锁
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)
} }
stateEvent = SEclose{event} stateEvent = SEclose{event}
r.Subscribers.Broadcast(stateEvent) r.Subscribers.Broadcast(stateEvent)
Streams.Delete(r.Path) r.Tracks.Range(func(_ string, t Track) {
r.timeout.Stop() t.Dispose()
})
} }
EventBus <- stateEvent EventBus <- stateEvent
if r.Publisher != nil { if r.Publisher != nil {
@@ -494,8 +473,10 @@ func (s *Stream) run() {
s.action(ACTION_TIMEOUT) s.action(ACTION_TIMEOUT)
} }
case action, ok := <-s.actionChan.C: case action, ok := <-s.actionChan.C:
if !ok {
return
}
timeStart = time.Now() timeStart = time.Now()
if ok {
switch v := action.(type) { switch v := action.(type) {
case SubPulse: case SubPulse:
timeOutInfo = zap.String("action", "SubPulse") timeOutInfo = zap.String("action", "SubPulse")
@@ -622,13 +603,6 @@ func (s *Stream) run() {
timeOutInfo = zap.String("action", "unknown") timeOutInfo = zap.String("action", "unknown")
s.Error("unknown action", timeOutInfo) s.Error("unknown action", timeOutInfo)
} }
} else {
s.Subscribers.Dispose()
s.Tracks.Range(func(_ string, t Track) {
t.Dispose()
})
return
}
} }
} }
} }

View File

@@ -16,7 +16,7 @@ var (
summary SummaryUtil summary SummaryUtil
lastSummary Summary lastSummary Summary
children util.Map[string, *Summary] children util.Map[string, *Summary]
collectLock sync.Mutex collectLock sync.RWMutex
) )
// ServerSummary 系统摘要定义 // ServerSummary 系统摘要定义
type Summary struct { type Summary struct {
@@ -62,8 +62,13 @@ func (s *SummaryUtil) MarshalYAML() (any, error) {
} }
func (s *SummaryUtil) collect() *Summary { func (s *SummaryUtil) collect() *Summary {
collectLock.Lock() if collectLock.TryLock() {
defer collectLock.Unlock() defer collectLock.Unlock()
} else {
collectLock.RLock()
defer collectLock.RUnlock()
return &lastSummary
}
dur := time.Since(s.ts) dur := time.Since(s.ts)
if dur < time.Second { if dur < time.Second {
return &lastSummary return &lastSummary

View File

@@ -102,10 +102,6 @@ type Media struct {
流速控制 流速控制
} }
func (av *Media) Dispose() {
av.Value.Broadcast()
}
func (av *Media) GetFromPool(b util.IBytes) (item *util.ListItem[util.Buffer]) { func (av *Media) GetFromPool(b util.IBytes) (item *util.ListItem[util.Buffer]) {
if b.Reuse() { if b.Reuse() {
item = av.BytesPool.Get(b.Len()) item = av.BytesPool.Get(b.Len())

View File

@@ -60,10 +60,6 @@ func (d *Data[T]) Attach(s IStream) {
} }
} }
func (d *Data[T]) Dispose() {
d.Value.Broadcast()
}
func (d *Data[T]) LastWriteTime() time.Time { func (d *Data[T]) LastWriteTime() time.Time {
return d.LastValue.WriteTime return d.LastValue.WriteTime
} }

View File

@@ -82,7 +82,7 @@ type List[T any] struct {
} }
func (p *List[T]) PushValue(value T) { func (p *List[T]) PushValue(value T) {
p.Push(&ListItem[T]{Value: value}) p.Push(&ListItem[T]{Value: value, reset: true})
} }
func (p *List[T]) Push(item *ListItem[T]) { func (p *List[T]) Push(item *ListItem[T]) {

View File

@@ -309,6 +309,7 @@ func (p BytesPool) Get(size int) (item *ListItem[Buffer]) {
if item == nil { if item == nil {
item = &ListItem[Buffer]{ item = &ListItem[Buffer]{
Value: make(Buffer, size), Value: make(Buffer, size),
reset: true,
} }
} }
return return