Rework peer connection writer to keep individual writes smaller

This fixes an issue with WebRTC when the write buffers get too big.
This commit is contained in:
Matt Joiner 2022-06-14 14:07:45 +10:00
parent caf35cd9b8
commit 54665f160a
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
1 changed files with 14 additions and 8 deletions

View File

@ -86,19 +86,25 @@ func (cn *peerConnMsgWriter) run(keepAliveTimeout time.Duration) {
// Flip the buffers. // Flip the buffers.
frontBuf, cn.writeBuffer = cn.writeBuffer, frontBuf frontBuf, cn.writeBuffer = cn.writeBuffer, frontBuf
cn.mu.Unlock() cn.mu.Unlock()
n, err := cn.w.Write(frontBuf.Bytes()) if frontBuf.Len() == 0 {
if n != 0 { panic("expected non-empty front buffer")
lastWrite = time.Now() }
keepAliveTimer.Reset(keepAliveTimeout) var err error
for frontBuf.Len() != 0 {
// Limit write size for WebRTC. See https://github.com/pion/datachannel/issues/59.
next := frontBuf.Next(1<<16 - 1)
var n int
n, err = cn.w.Write(next)
if err == nil && n != len(next) {
panic("expected full write")
}
} }
if err != nil { if err != nil {
cn.logger.WithDefaultLevel(log.Debug).Printf("error writing: %v", err) cn.logger.WithDefaultLevel(log.Debug).Printf("error writing: %v", err)
return return
} }
if n != frontBuf.Len() { lastWrite = time.Now()
panic("short write") keepAliveTimer.Reset(keepAliveTimeout)
}
frontBuf.Reset()
} }
} }