-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ccff82
commit 7d3f0d9
Showing
21 changed files
with
1,593 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/pion/interceptor | ||
|
||
go 1.20 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/pion/logging v0.2.3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package bwe | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type ECN uint8 | ||
|
||
const ( | ||
//nolint:misspell | ||
// ECNNonECT signals Non ECN-Capable Transport, Non-ECT | ||
ECNNonECT ECN = iota // 00 | ||
|
||
//nolint:misspell | ||
// ECNECT1 signals ECN Capable Transport, ECT(0) | ||
ECNECT1 // 01 | ||
|
||
//nolint:misspell | ||
// ECNECT0 signals ECN Capable Transport, ECT(1) | ||
ECNECT0 // 10 | ||
|
||
// ECNCE signals ECN Congestion Encountered, CE | ||
ECNCE // 11 | ||
) | ||
|
||
type Acknowledgment struct { | ||
SeqNr int64 | ||
Size uint16 | ||
Departure time.Time | ||
Arrived bool | ||
Arrival time.Time | ||
ECN ECN | ||
} | ||
|
||
func (a Acknowledgment) String() string { | ||
return fmt.Sprintf("seq=%v, departure=%v, arrival=%v", a.SeqNr, a.Departure, a.Arrival) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package bwe | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type arrivalGroup []Acknowledgment | ||
|
||
type arrivalGroupAccumulator struct { | ||
next arrivalGroup | ||
burstInterval time.Duration | ||
maxBurstDuration time.Duration | ||
} | ||
|
||
func newArrivalGroupAccumulator() *arrivalGroupAccumulator { | ||
return &arrivalGroupAccumulator{ | ||
next: make([]Acknowledgment, 0), | ||
burstInterval: 5 * time.Millisecond, | ||
maxBurstDuration: 100 * time.Millisecond, | ||
} | ||
} | ||
|
||
func (a *arrivalGroupAccumulator) onPacketAcked(ack Acknowledgment) arrivalGroup { | ||
if len(a.next) == 0 { | ||
a.next = append(a.next, ack) | ||
return nil | ||
} | ||
|
||
if ack.Departure.Sub(a.next[0].Departure) < a.burstInterval { | ||
a.next = append(a.next, ack) | ||
return nil | ||
} | ||
|
||
sendTimeDelta := ack.Departure.Sub(a.next[0].Departure) | ||
arrivalTimeDeltaLast := ack.Arrival.Sub(a.next[len(a.next)-1].Arrival) | ||
arrivalTimeDeltaFirst := ack.Arrival.Sub(a.next[0].Arrival) | ||
propagationDelta := arrivalTimeDeltaFirst - sendTimeDelta | ||
|
||
if propagationDelta < 0 && arrivalTimeDeltaLast <= a.burstInterval && arrivalTimeDeltaFirst < a.maxBurstDuration { | ||
a.next = append(a.next, ack) | ||
return nil | ||
} | ||
|
||
group := make(arrivalGroup, len(a.next)) | ||
copy(group, a.next) | ||
a.next = arrivalGroup{ack} | ||
return group | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
package bwe | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestArrivalGroupAccumulator(t *testing.T) { | ||
triggerNewGroupElement := Acknowledgment{ | ||
Departure: time.Time{}.Add(time.Second), | ||
Arrival: time.Time{}.Add(time.Second), | ||
} | ||
cases := []struct { | ||
name string | ||
log []Acknowledgment | ||
exp []arrivalGroup | ||
}{ | ||
{ | ||
name: "emptyCreatesNoGroups", | ||
log: []Acknowledgment{}, | ||
exp: []arrivalGroup{}, | ||
}, | ||
{ | ||
name: "createsSingleElementGroup", | ||
log: []Acknowledgment{ | ||
{ | ||
Departure: time.Time{}, | ||
Arrival: time.Time{}.Add(time.Millisecond), | ||
}, | ||
triggerNewGroupElement, | ||
}, | ||
exp: []arrivalGroup{{ | ||
{ | ||
Departure: time.Time{}, | ||
Arrival: time.Time{}.Add(time.Millisecond), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "createsTwoElementGroup", | ||
log: []Acknowledgment{ | ||
{ | ||
Arrival: time.Time{}.Add(15 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(3 * time.Millisecond), | ||
Arrival: time.Time{}.Add(20 * time.Millisecond), | ||
}, | ||
triggerNewGroupElement, | ||
}, | ||
exp: []arrivalGroup{{ | ||
{ | ||
Departure: time.Time{}, | ||
Arrival: time.Time{}.Add(15 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(3 * time.Millisecond), | ||
Arrival: time.Time{}.Add(20 * time.Millisecond), | ||
}, | ||
}}, | ||
}, | ||
{ | ||
name: "createsTwoArrivalGroups1", | ||
log: []Acknowledgment{ | ||
{ | ||
Departure: time.Time{}, | ||
Arrival: time.Time{}.Add(15 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(3 * time.Millisecond), | ||
Arrival: time.Time{}.Add(20 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(9 * time.Millisecond), | ||
Arrival: time.Time{}.Add(24 * time.Millisecond), | ||
}, | ||
triggerNewGroupElement, | ||
}, | ||
exp: []arrivalGroup{ | ||
{ | ||
{ | ||
Arrival: time.Time{}.Add(15 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(3 * time.Millisecond), | ||
Arrival: time.Time{}.Add(20 * time.Millisecond), | ||
}, | ||
}, | ||
{ | ||
{ | ||
Departure: time.Time{}.Add(9 * time.Millisecond), | ||
Arrival: time.Time{}.Add(24 * time.Millisecond), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "createsTwoArrivalGroups2", | ||
log: []Acknowledgment{ | ||
{ | ||
Departure: time.Time{}, | ||
Arrival: time.Time{}.Add(15 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(3 * time.Millisecond), | ||
Arrival: time.Time{}.Add(20 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(9 * time.Millisecond), | ||
Arrival: time.Time{}.Add(30 * time.Millisecond), | ||
}, | ||
triggerNewGroupElement, | ||
}, | ||
exp: []arrivalGroup{ | ||
{ | ||
{ | ||
Arrival: time.Time{}.Add(15 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(3 * time.Millisecond), | ||
Arrival: time.Time{}.Add(20 * time.Millisecond), | ||
}, | ||
}, | ||
{ | ||
{ | ||
Departure: time.Time{}.Add(9 * time.Millisecond), | ||
Arrival: time.Time{}.Add(30 * time.Millisecond), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "ignoresOutOfOrderPackets", | ||
log: []Acknowledgment{ | ||
{ | ||
Departure: time.Time{}, | ||
Arrival: time.Time{}.Add(15 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(6 * time.Millisecond), | ||
Arrival: time.Time{}.Add(34 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(8 * time.Millisecond), | ||
Arrival: time.Time{}.Add(30 * time.Millisecond), | ||
}, | ||
triggerNewGroupElement, | ||
}, | ||
exp: []arrivalGroup{ | ||
{ | ||
{ | ||
Departure: time.Time{}, | ||
Arrival: time.Time{}.Add(15 * time.Millisecond), | ||
}, | ||
}, | ||
{ | ||
{ | ||
Departure: time.Time{}.Add(6 * time.Millisecond), | ||
Arrival: time.Time{}.Add(34 * time.Millisecond), | ||
}, | ||
{ | ||
Departure: time.Time{}.Add(8 * time.Millisecond), | ||
Arrival: time.Time{}.Add(30 * time.Millisecond), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "newGroupBecauseOfInterDepartureTime", | ||
log: []Acknowledgment{ | ||
{ | ||
SeqNr: 0, | ||
Departure: time.Time{}, | ||
Arrival: time.Time{}.Add(4 * time.Millisecond), | ||
}, | ||
{ | ||
SeqNr: 1, | ||
Departure: time.Time{}.Add(3 * time.Millisecond), | ||
Arrival: time.Time{}.Add(4 * time.Millisecond), | ||
}, | ||
{ | ||
SeqNr: 2, | ||
Departure: time.Time{}.Add(6 * time.Millisecond), | ||
Arrival: time.Time{}.Add(10 * time.Millisecond), | ||
}, | ||
{ | ||
SeqNr: 3, | ||
Departure: time.Time{}.Add(9 * time.Millisecond), | ||
Arrival: time.Time{}.Add(10 * time.Millisecond), | ||
}, | ||
triggerNewGroupElement, | ||
}, | ||
exp: []arrivalGroup{ | ||
{ | ||
{ | ||
SeqNr: 0, | ||
Departure: time.Time{}, | ||
Arrival: time.Time{}.Add(4 * time.Millisecond), | ||
}, | ||
{ | ||
SeqNr: 1, | ||
Departure: time.Time{}.Add(3 * time.Millisecond), | ||
Arrival: time.Time{}.Add(4 * time.Millisecond), | ||
}, | ||
}, | ||
{ | ||
{ | ||
SeqNr: 2, | ||
Departure: time.Time{}.Add(6 * time.Millisecond), | ||
Arrival: time.Time{}.Add(10 * time.Millisecond), | ||
}, | ||
{ | ||
SeqNr: 3, | ||
Departure: time.Time{}.Add(9 * time.Millisecond), | ||
Arrival: time.Time{}.Add(10 * time.Millisecond), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
aga := newArrivalGroupAccumulator() | ||
received := []arrivalGroup{} | ||
for _, ack := range tc.log { | ||
next := aga.onPacketAcked(ack) | ||
if next != nil { | ||
received = append(received, next) | ||
} | ||
} | ||
assert.Equal(t, tc.exp, received) | ||
}) | ||
} | ||
} |
Oops, something went wrong.