Merge pull request #2923 from lucas-clemente/receive-buffer-warning

improve the warning about the UDP receive buffer size
This commit is contained in:
Marten Seemann 2020-12-04 10:37:51 +07:00 committed by GitHub
commit d1bc0c20b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 14 deletions

View file

@ -4,6 +4,7 @@ import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"hash"
"log"
@ -54,40 +55,38 @@ type packetHandlerMap struct {
var _ packetHandlerManager = &packetHandlerMap{}
func setReceiveBuffer(c net.PacketConn, logger utils.Logger) {
func setReceiveBuffer(c net.PacketConn, logger utils.Logger) error {
conn, ok := c.(interface{ SetReadBuffer(int) error })
if !ok {
logger.Debugf("Connection doesn't allow setting of receive buffer size")
return
return errors.New("connection doesn't allow setting of receive buffer size")
}
size, err := inspectReadBuffer(c)
if err != nil {
log.Printf("Failed to determine receive buffer size: %s", err)
return
return fmt.Errorf("failed to determine receive buffer size: %w", err)
}
if size >= protocol.DesiredReceiveBufferSize {
logger.Debugf("Conn has receive buffer of %d kiB (wanted: at least %d kiB)", size/1024, protocol.DesiredReceiveBufferSize/1024)
}
if err := conn.SetReadBuffer(protocol.DesiredReceiveBufferSize); err != nil {
log.Printf("Failed to increase receive buffer size: %s\n", err)
return
return fmt.Errorf("failed to increase receive buffer size: %w", err)
}
newSize, err := inspectReadBuffer(c)
if err != nil {
log.Printf("Failed to determine receive buffer size: %s", err)
return
return fmt.Errorf("failed to determine receive buffer size: %w", err)
}
if newSize == size {
log.Printf("Failed to determine receive buffer size: %s", err)
return
return fmt.Errorf("failed to determine receive buffer size: %w", err)
}
if newSize < protocol.DesiredReceiveBufferSize {
log.Printf("Failed to sufficiently increase receive buffer size. Was: %d kiB, wanted: %d kiB, got: %d kiB.", size/1024, protocol.DesiredReceiveBufferSize/1024, newSize/1024)
return
return fmt.Errorf("failed to sufficiently increase receive buffer size (was: %d kiB, wanted: %d kiB, got: %d kiB)", size/1024, protocol.DesiredReceiveBufferSize/1024, newSize/1024)
}
logger.Debugf("Increased receive buffer size to %d kiB", newSize/1024)
return nil
}
// only print warnings about the UPD receive buffer size once
var receiveBufferWarningOnce sync.Once
func newPacketHandlerMap(
c net.PacketConn,
connIDLen int,
@ -95,7 +94,11 @@ func newPacketHandlerMap(
tracer logging.Tracer,
logger utils.Logger,
) (packetHandlerManager, error) {
setReceiveBuffer(c, logger)
if err := setReceiveBuffer(c, logger); err != nil {
receiveBufferWarningOnce.Do(func() {
log.Printf("%s. See https://github.com/lucas-clemente/quic-go/wiki/UDP-Receive-Buffer-Size for details.", err)
})
}
conn, err := wrapConn(c)
if err != nil {
return nil, err

View file

@ -1,6 +1,8 @@
package quic
import (
"io/ioutil"
"log"
"sync"
"testing"
@ -23,6 +25,10 @@ var _ = BeforeEach(func() {
connMuxerOnce = *new(sync.Once)
})
var _ = BeforeSuite(func() {
log.SetOutput(ioutil.Discard)
})
var _ = AfterEach(func() {
mockCtrl.Finish()
})