github.com/sagernet/quic-go@v0.43.1-beta.1/ech/sys_conn_buffers_write.go (about)

     1  // Code generated by go generate. DO NOT EDIT.
     2  // Source: sys_conn_buffers.go
     3  
     4  package quic
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"net"
    10  	"syscall"
    11  
    12  	"github.com/sagernet/quic-go/internal/protocol"
    13  	"github.com/sagernet/quic-go/internal/utils"
    14  )
    15  
    16  func setSendBuffer(c net.PacketConn) error {
    17  	conn, ok := c.(interface{ SetWriteBuffer(int) error })
    18  	if !ok {
    19  		return errors.New("connection doesn't allow setting of send buffer size. Not a *net.UDPConn?")
    20  	}
    21  
    22  	var syscallConn syscall.RawConn
    23  	if sc, ok := c.(interface {
    24  		SyscallConn() (syscall.RawConn, error)
    25  	}); ok {
    26  		var err error
    27  		syscallConn, err = sc.SyscallConn()
    28  		if err != nil {
    29  			syscallConn = nil
    30  		}
    31  	}
    32  	// The connection has a SetWriteBuffer method, but we couldn't obtain a syscall.RawConn.
    33  	// This shouldn't happen for a net.UDPConn, but is possible if the connection just implements the
    34  	// net.PacketConn interface and the SetWriteBuffer method.
    35  	// We have no way of checking if increasing the buffer size actually worked.
    36  	if syscallConn == nil {
    37  		return conn.SetWriteBuffer(protocol.DesiredSendBufferSize)
    38  	}
    39  
    40  	size, err := inspectWriteBuffer(syscallConn)
    41  	if err != nil {
    42  		return fmt.Errorf("failed to determine send buffer size: %w", err)
    43  	}
    44  	if size >= protocol.DesiredSendBufferSize {
    45  		utils.DefaultLogger.Debugf("Conn has send buffer of %d kiB (wanted: at least %d kiB)", size/1024, protocol.DesiredSendBufferSize/1024)
    46  		return nil
    47  	}
    48  	// Ignore the error. We check if we succeeded by querying the buffer size afterward.
    49  	_ = conn.SetWriteBuffer(protocol.DesiredSendBufferSize)
    50  	newSize, err := inspectWriteBuffer(syscallConn)
    51  	if newSize < protocol.DesiredSendBufferSize {
    52  		// Try again with RCVBUFFORCE on Linux
    53  		_ = forceSetSendBuffer(syscallConn, protocol.DesiredSendBufferSize)
    54  		newSize, err = inspectWriteBuffer(syscallConn)
    55  		if err != nil {
    56  			return fmt.Errorf("failed to determine send buffer size: %w", err)
    57  		}
    58  	}
    59  	if err != nil {
    60  		return fmt.Errorf("failed to determine send buffer size: %w", err)
    61  	}
    62  	if newSize == size {
    63  		return fmt.Errorf("failed to increase send buffer size (wanted: %d kiB, got %d kiB)", protocol.DesiredSendBufferSize/1024, newSize/1024)
    64  	}
    65  	if newSize < protocol.DesiredSendBufferSize {
    66  		return fmt.Errorf("failed to sufficiently increase send buffer size (was: %d kiB, wanted: %d kiB, got: %d kiB)", size/1024, protocol.DesiredSendBufferSize/1024, newSize/1024)
    67  	}
    68  	utils.DefaultLogger.Debugf("Increased send buffer size to %d kiB", newSize/1024)
    69  	return nil
    70  }