only print the warning about the UDP receive buffer size once

This commit is contained in:
Marten Seemann 2020-12-02 15:42:29 +07:00
parent a76879c305
commit 0eb3f14a60
2 changed files with 23 additions and 14 deletions

View file

@ -4,6 +4,7 @@ import (
"crypto/hmac" "crypto/hmac"
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"errors"
"fmt" "fmt"
"hash" "hash"
"log" "log"
@ -54,40 +55,38 @@ type packetHandlerMap struct {
var _ packetHandlerManager = &packetHandlerMap{} 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 }) conn, ok := c.(interface{ SetReadBuffer(int) error })
if !ok { if !ok {
logger.Debugf("Connection doesn't allow setting of receive buffer size") return errors.New("connection doesn't allow setting of receive buffer size")
return
} }
size, err := inspectReadBuffer(c) size, err := inspectReadBuffer(c)
if err != nil { if err != nil {
log.Printf("Failed to determine receive buffer size: %s", err) return fmt.Errorf("failed to determine receive buffer size: %w", err)
return
} }
if size >= protocol.DesiredReceiveBufferSize { if size >= protocol.DesiredReceiveBufferSize {
logger.Debugf("Conn has receive buffer of %d kiB (wanted: at least %d kiB)", size/1024, protocol.DesiredReceiveBufferSize/1024) 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 { if err := conn.SetReadBuffer(protocol.DesiredReceiveBufferSize); err != nil {
log.Printf("Failed to increase receive buffer size: %s\n", err) return fmt.Errorf("failed to increase receive buffer size: %w", err)
return
} }
newSize, err := inspectReadBuffer(c) newSize, err := inspectReadBuffer(c)
if err != nil { if err != nil {
log.Printf("Failed to determine receive buffer size: %s", err) return fmt.Errorf("failed to determine receive buffer size: %w", err)
return
} }
if newSize == size { if newSize == size {
log.Printf("Failed to determine receive buffer size: %s", err) return fmt.Errorf("failed to determine receive buffer size: %w", err)
return
} }
if newSize < protocol.DesiredReceiveBufferSize { 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 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)
return
} }
logger.Debugf("Increased receive buffer size to %d kiB", 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( func newPacketHandlerMap(
c net.PacketConn, c net.PacketConn,
connIDLen int, connIDLen int,
@ -95,7 +94,11 @@ func newPacketHandlerMap(
tracer logging.Tracer, tracer logging.Tracer,
logger utils.Logger, logger utils.Logger,
) (packetHandlerManager, error) { ) (packetHandlerManager, error) {
setReceiveBuffer(c, logger) if err := setReceiveBuffer(c, logger); err != nil {
receiveBufferWarningOnce.Do(func() {
log.Println(err)
})
}
conn, err := wrapConn(c) conn, err := wrapConn(c)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -4,7 +4,10 @@ import (
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"errors" "errors"
"io/ioutil"
"log"
"net" "net"
"os"
"time" "time"
mocklogging "github.com/lucas-clemente/quic-go/internal/mocks/logging" mocklogging "github.com/lucas-clemente/quic-go/internal/mocks/logging"
@ -63,6 +66,7 @@ var _ = Describe("Packet Handler Map", func() {
}) })
JustBeforeEach(func() { JustBeforeEach(func() {
log.SetOutput(ioutil.Discard)
conn = NewMockPacketConn(mockCtrl) conn = NewMockPacketConn(mockCtrl)
conn.EXPECT().LocalAddr().Return(&net.UDPAddr{}).AnyTimes() conn.EXPECT().LocalAddr().Return(&net.UDPAddr{}).AnyTimes()
conn.EXPECT().ReadFrom(gomock.Any()).DoAndReturn(func(b []byte) (int, net.Addr, error) { conn.EXPECT().ReadFrom(gomock.Any()).DoAndReturn(func(b []byte) (int, net.Addr, error) {
@ -77,6 +81,8 @@ var _ = Describe("Packet Handler Map", func() {
handler = phm.(*packetHandlerMap) handler = phm.(*packetHandlerMap)
}) })
AfterEach(func() { log.SetOutput(os.Stdout) })
It("closes", func() { It("closes", func() {
getMultiplexer() // make the sync.Once execute getMultiplexer() // make the sync.Once execute
// replace the clientMuxer. getClientMultiplexer will now return the MockMultiplexer // replace the clientMuxer. getClientMultiplexer will now return the MockMultiplexer