Use the same wrap logic as bucket as it is more robust (#460)

Also rename n -> s for golang convention
This commit is contained in:
Raja Subramanian
2022-02-24 17:51:02 +05:30
committed by GitHub
parent eed241cf7f
commit 5a9459d0d5
+58 -49
View File
@@ -112,82 +112,79 @@ func newSequencer(maxTrack int, logger logger.Logger) *sequencer {
} }
} }
func (n *sequencer) setRTT(rtt uint32) { func (s *sequencer) setRTT(rtt uint32) {
n.Lock() s.Lock()
defer n.Unlock() defer s.Unlock()
if rtt == 0 { if rtt == 0 {
n.rtt = defaultRtt s.rtt = defaultRtt
} else { } else {
n.rtt = rtt s.rtt = rtt
} }
} }
func (n *sequencer) push(sn, offSn uint16, timeStamp uint32, layer int8) *packetMeta { func (s *sequencer) push(sn, offSn uint16, timeStamp uint32, layer int8) *packetMeta {
n.Lock() s.Lock()
defer n.Unlock() defer s.Unlock()
inc := offSn - n.headSN if !s.init {
step := 0 s.headSN = offSn - 1
switch { s.init = true
case !n.init:
n.headSN = offSn
n.init = true
case inc == 0:
// duplicate
return nil
case inc < (1 << 15): // in-order packet
n.step += int(inc)
if n.step >= n.max {
n.step -= n.max
}
step = n.step
n.headSN = offSn
default: // out-of-order packet
back := int(n.headSN - offSn)
if back >= n.max {
n.logger.Debugw("old packet, can not be sequenced", "head", sn, "received", offSn)
return nil
}
step = n.step - back
if step < 0 {
step += n.max
}
} }
n.seq[step] = packetMeta{ diff := offSn - s.headSN
if diff == 0 {
// duplicate
return nil
}
slot := 0
if diff > (1 << 15) {
// out-of-order
back := int(s.headSN - offSn)
if back >= s.max {
s.logger.Debugw("old packet, can not be sequenced", "head", sn, "received", offSn)
return nil
}
slot = s.step - back - 1
} else {
slot = s.step + int(diff) - 1
s.headSN = offSn
// for next packet
s.step = s.wrap(s.step + int(diff))
}
slot = s.wrap(slot)
s.seq[slot] = packetMeta{
sourceSeqNo: sn, sourceSeqNo: sn,
targetSeqNo: offSn, targetSeqNo: offSn,
timestamp: timeStamp, timestamp: timeStamp,
layer: layer, layer: layer,
} }
return &n.seq[step] return &s.seq[slot]
} }
func (n *sequencer) getPacketsMeta(seqNo []uint16) []packetMeta { func (s *sequencer) getPacketsMeta(seqNo []uint16) []packetMeta {
n.Lock() s.Lock()
defer n.Unlock() defer s.Unlock()
meta := make([]packetMeta, 0, len(seqNo)) meta := make([]packetMeta, 0, len(seqNo))
refTime := uint32(time.Now().UnixNano()/1e6 - n.startTime) refTime := uint32(time.Now().UnixNano()/1e6 - s.startTime)
for _, sn := range seqNo { for _, sn := range seqNo {
diff := n.headSN - sn diff := s.headSN - sn
if diff > (1<<15) || int(diff) >= n.max { if diff > (1<<15) || int(diff) >= s.max {
// out-of-order from head (should not happen) or too old // out-of-order from head (should not happen) or too old
continue continue
} }
step := n.step - int(diff) slot := s.wrap(s.step - int(diff) - 1)
if step < 0 { seq := &s.seq[slot]
step += n.max
}
seq := &n.seq[step]
if seq.targetSeqNo != sn { if seq.targetSeqNo != sn {
continue continue
} }
if seq.lastNack == 0 || refTime-seq.lastNack > uint32(math.Min(float64(ignoreRetransmission), float64(2*n.rtt))) { if seq.lastNack == 0 || refTime-seq.lastNack > uint32(math.Min(float64(ignoreRetransmission), float64(2*s.rtt))) {
seq.nacked++ seq.nacked++
seq.lastNack = refTime seq.lastNack = refTime
meta = append(meta, *seq) meta = append(meta, *seq)
@@ -196,3 +193,15 @@ func (n *sequencer) getPacketsMeta(seqNo []uint16) []packetMeta {
return meta return meta
} }
func (s *sequencer) wrap(slot int) int {
for slot < 0 {
slot += s.max
}
for slot >= s.max {
slot -= s.max
}
return slot
}