Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pending queue in mux for dropped packets #2813

Closed
wants to merge 1 commit into from
Closed
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
62 changes: 55 additions & 7 deletions internal/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"io"
"net"
"sync"
"time"

"github.com/pion/ice/v3"
"github.com/pion/logging"
Expand All @@ -34,20 +35,25 @@
bufferSize int
closedCh chan struct{}

pendingPackets map[*pendingPacket]struct{}
pendingPacketsLock sync.Mutex

log logging.LeveledLogger
}

// NewMux creates a new Mux
func NewMux(config Config) *Mux {
m := &Mux{
nextConn: config.Conn,
endpoints: make(map[*Endpoint]MatchFunc),
bufferSize: config.BufferSize,
closedCh: make(chan struct{}),
log: config.LoggerFactory.NewLogger("mux"),
nextConn: config.Conn,
endpoints: make(map[*Endpoint]MatchFunc),
bufferSize: config.BufferSize,
closedCh: make(chan struct{}),
log: config.LoggerFactory.NewLogger("mux"),
pendingPackets: make(map[*pendingPacket]struct{}),
}

go m.readLoop()
go m.pendingPacketsHandler()

return m
}
Expand Down Expand Up @@ -140,10 +146,17 @@

if endpoint == nil {
if len(buf) > 0 {
m.log.Warnf("Warning: mux: no endpoint for packet starting with %d", buf[0])
m.log.Warnf("Warning: mux: no endpoint for packet starting with %d, queueing packet as pending", buf[0])
} else {
m.log.Warnf("Warning: mux: no endpoint for zero length packet")
m.log.Warnf("Warning: mux: no endpoint for zero length packet, queueing packet as pending")

Check warning on line 151 in internal/mux/mux.go

View check run for this annotation

Codecov / codecov/patch

internal/mux/mux.go#L151

Added line #L151 was not covered by tests
}

m.pendingPacketsLock.Lock()
m.pendingPackets[&pendingPacket{
t: time.Now(),
data: buf,
}] = struct{}{}
m.pendingPacketsLock.Unlock()
return nil
}

Expand All @@ -157,3 +170,38 @@

return err
}

func (m *Mux) pendingPacketsHandler() {
ticker := time.NewTicker(time.Millisecond)
defer ticker.Stop()

for {
select {
case <-ticker.C:
m.lock.RLock()
m.pendingPacketsLock.Lock()
for p := range m.pendingPackets {
if time.Since(p.t) > time.Second*5 {
m.log.Warnf("Warning: mux: dropping packet after 5 seconds in pending queue")
delete(m.pendingPackets, p)

Check warning on line 186 in internal/mux/mux.go

View check run for this annotation

Codecov / codecov/patch

internal/mux/mux.go#L185-L186

Added lines #L185 - L186 were not covered by tests
}
for endpoint, f := range m.endpoints {
if f(p.data) {
_, _ = endpoint.buffer.Write(p.data)
delete(m.pendingPackets, p)
m.log.Warnf("Warning: mux: found endpoint for packet after %s in pending queue", time.Since(p.t))

Check warning on line 192 in internal/mux/mux.go

View check run for this annotation

Codecov / codecov/patch

internal/mux/mux.go#L190-L192

Added lines #L190 - L192 were not covered by tests
}
}
}
m.pendingPacketsLock.Unlock()
m.lock.RUnlock()
case <-m.closedCh:
return
}
}
}

type pendingPacket struct {
t time.Time
data []byte
}
Loading