optimize streaming

This commit is contained in:
KingKDot
2026-04-08 14:14:09 -05:00
parent 67d81d1b75
commit b711efe6bd
10 changed files with 113 additions and 9 deletions
@@ -0,0 +1,49 @@
package capture
import (
"sync/atomic"
"time"
)
const maxInFlightFrames int64 = 2
var (
inFlightFrames atomic.Int64
frameAckSeen atomic.Bool
lastAckNano atomic.Int64
)
func AcquireFrameSlot() bool {
if !frameAckSeen.Load() {
return true
}
for {
cur := inFlightFrames.Load()
if cur >= maxInFlightFrames {
lastAck := lastAckNano.Load()
if lastAck > 0 && time.Since(time.Unix(0, lastAck)) > time.Second {
inFlightFrames.Store(0)
continue
}
return false
}
if inFlightFrames.CompareAndSwap(cur, cur+1) {
return true
}
}
}
func ReleaseFrameSlot() {
frameAckSeen.Store(true)
lastAckNano.Store(time.Now().UnixNano())
if inFlightFrames.Add(-1) < 0 {
inFlightFrames.Store(0)
}
}
func ResetFrameSlots() {
inFlightFrames.Store(0)
frameAckSeen.Store(false)
lastAckNano.Store(0)
}