Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions gcc/delivery_rate_estimator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// SPDX-FileCopyrightText: 2025 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package gcc

import (
"container/heap"
"time"
)

type deliveryRateHeapItem struct {
arrival time.Time
size int
}

type deliveryRateHeap []deliveryRateHeapItem

// Len implements heap.Interface.
func (d deliveryRateHeap) Len() int {
return len(d)
}

// Less implements heap.Interface.
func (d deliveryRateHeap) Less(i int, j int) bool {
return d[i].arrival.Before(d[j].arrival)
}

// Pop implements heap.Interface.
func (d *deliveryRateHeap) Pop() any {
old := *d
n := len(old)
x := old[n-1]
*d = old[0 : n-1]

return x
}

// Push implements heap.Interface.
func (d *deliveryRateHeap) Push(x any) {
// nolint
*d = append(*d, x.(deliveryRateHeapItem))
}

// Swap implements heap.Interface.
func (d deliveryRateHeap) Swap(i int, j int) {
d[i], d[j] = d[j], d[i]
}

type deliveryRateEstimator struct {
window time.Duration
latestArrival time.Time
history *deliveryRateHeap
}

func newDeliveryRateEstimator(window time.Duration) *deliveryRateEstimator {
return &deliveryRateEstimator{
window: window,
latestArrival: time.Time{},
history: &deliveryRateHeap{},
}
}

func (e *deliveryRateEstimator) onPacketAcked(arrival time.Time, size int) {
if arrival.After(e.latestArrival) {
e.latestArrival = arrival
}
heap.Push(e.history, deliveryRateHeapItem{
arrival: arrival,
size: size,
})
}

func (e *deliveryRateEstimator) getRate() int {
deadline := e.latestArrival.Add(-e.window)
Copy link
Member

@JoTurk JoTurk Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is silly but if window is 0, we won't evict. so maybe we should assert here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand. Wouldn't a window of 0 mean we evict everything before latestArrival?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry i didn't explain well, but with window=0, we don't evict history with arrival = latestArrival. because of (*e.history)[0].arrival.Before(deadline).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's ok though, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was just pointing it out in case it's not intended, sorry :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. No worries! Thanks for the review!

for len(*e.history) > 0 && (*e.history)[0].arrival.Before(deadline) {
heap.Pop(e.history)
}
earliest := e.latestArrival
sum := 0
for _, i := range *e.history {
if i.arrival.Before(earliest) {
earliest = i.arrival
}
sum += i.size
}
d := e.latestArrival.Sub(earliest)
if d == 0 {
return 0
}
rate := 8 * float64(sum) / d.Seconds()

return int(rate)
}
77 changes: 77 additions & 0 deletions gcc/delivery_rate_estimator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-FileCopyrightText: 2025 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package gcc

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestDeliveryRateEstimator(t *testing.T) {
type ack struct {
arrival time.Time
size int
}
cases := []struct {
window time.Duration
acks []ack
expectedRate int
}{
{
window: 0,
acks: []ack{},
expectedRate: 0,
},
{
window: time.Second,
acks: []ack{},
expectedRate: 0,
},
{
window: time.Second,
acks: []ack{
{time.Time{}, 1200},
},
expectedRate: 0,
},
{
window: time.Second,
acks: []ack{
{time.Time{}.Add(time.Millisecond), 1200},
},
expectedRate: 0,
},
{
window: time.Second,
acks: []ack{
{time.Time{}.Add(time.Second), 1200},
{time.Time{}.Add(1500 * time.Millisecond), 1200},
{time.Time{}.Add(2 * time.Second), 1200},
},
expectedRate: 28800,
},
{
window: time.Second,
acks: []ack{
{time.Time{}.Add(500 * time.Millisecond), 1200},
{time.Time{}.Add(time.Second), 1200},
{time.Time{}.Add(1500 * time.Millisecond), 1200},
{time.Time{}.Add(2 * time.Second), 1200},
},
expectedRate: 28800,
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
e := newDeliveryRateEstimator(tc.window)
for _, ack := range tc.acks {
e.onPacketAcked(ack.arrival, ack.size)
}
assert.Equal(t, tc.expectedRate, e.getRate())
})
}
}
Loading