Skip to content

Commit

Permalink
Merge pull request #2923 from lucas-clemente/receive-buffer-warning
Browse files Browse the repository at this point in the history
improve the warning about the UDP receive buffer size
  • Loading branch information
marten-seemann authored Dec 4, 2020
2 parents afe287e + b0974c1 commit d1bc0c2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
31 changes: 17 additions & 14 deletions packet_handler_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"hash"
"log"
Expand Down Expand Up @@ -54,48 +55,50 @@ 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,
statelessResetKey []byte,
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
Expand Down
6 changes: 6 additions & 0 deletions quic_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package quic

import (
"io/ioutil"
"log"
"sync"
"testing"

Expand All @@ -23,6 +25,10 @@ var _ = BeforeEach(func() {
connMuxerOnce = *new(sync.Once)
})

var _ = BeforeSuite(func() {
log.SetOutput(ioutil.Discard)
})

var _ = AfterEach(func() {
mockCtrl.Finish()
})

0 comments on commit d1bc0c2

Please sign in to comment.