Add bytes cap on migration data cache size (#4782)

This commit is contained in:
cnderrauber
2026-08-21 16:49:09 +08:00
committed by GitHub
parent 6a64df20df
commit 803864dc7b
3 changed files with 40 additions and 9 deletions
+23 -4
View File
@@ -14,25 +14,44 @@ func TestMigrationDataCache_Add(t *testing.T) {
cache := NewMigrationDataCache(10, expiredAt)
pkt1 := &livekit.DataPacket{Sequence: 9}
state := cache.Add(pkt1)
state := cache.Add(pkt1, 0)
require.Equal(t, MigrationDataCacheStateWaiting, state)
require.Empty(t, cache.Get())
pkt2 := &livekit.DataPacket{Sequence: 11}
state = cache.Add(pkt2)
state = cache.Add(pkt2, 0)
require.Equal(t, MigrationDataCacheStateDone, state)
require.Empty(t, cache.Get())
pkt3 := &livekit.DataPacket{Sequence: 12}
state = cache.Add(pkt3)
state = cache.Add(pkt3, 0)
require.Equal(t, MigrationDataCacheStateDone, state)
require.Empty(t, cache.Get())
cache2 := NewMigrationDataCache(20, time.Now().Add(10*time.Millisecond))
pkt4 := &livekit.DataPacket{Sequence: 22}
time.Sleep(20 * time.Millisecond)
state = cache2.Add(pkt4)
state = cache2.Add(pkt4, 0)
require.Equal(t, MigrationDataCacheStateTimeout, state)
require.Len(t, cache2.Get(), 1)
require.Equal(t, uint32(22), cache2.Get()[0].Sequence)
}
func TestMigrationDataCache_MaxSize(t *testing.T) {
// the cache should not grow past the size budget even if the expiry is far in the future
cache := NewMigrationDataCache(10, time.Now().Add(time.Minute))
pktSize := 1000
seq := uint32(12)
state := MigrationDataCacheStateWaiting
for ; state == MigrationDataCacheStateWaiting; seq++ {
state = cache.Add(&livekit.DataPacket{Sequence: seq}, pktSize)
}
require.Equal(t, MigrationDataCacheStateTimeout, state)
require.LessOrEqual(t, cache.Size(), migrationDataCacheMaxSize+pktSize)
require.Len(t, cache.Get(), migrationDataCacheMaxSize/pktSize+1)
// once full, further packets are dropped, including the continuous one
require.Equal(t, MigrationDataCacheStateTimeout, cache.Add(&livekit.DataPacket{Sequence: 11}, pktSize))
}