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:
parent
caf35cd9b8
commit
54665f160a
|
@ -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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue