mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 10:44:02 +03:00
Deprecate bufferSize
This commit is contained in:
+5
-13
@@ -9,8 +9,6 @@ import (
|
||||
|
||||
type defaultDialer struct {
|
||||
net.Dialer
|
||||
|
||||
bufferSize int
|
||||
}
|
||||
|
||||
func (d *defaultDialer) Dial(network, address string) (net.Conn, error) {
|
||||
@@ -30,7 +28,7 @@ func (d *defaultDialer) DialContext(ctx context.Context, network, address string
|
||||
}
|
||||
|
||||
// we do not need to call to end user. End users call us.
|
||||
if err := SetServerSocketOptions(conn, d.bufferSize); err != nil {
|
||||
if err := SetServerSocketOptions(conn, 0); err != nil {
|
||||
conn.Close()
|
||||
|
||||
return nil, fmt.Errorf("cannot set socket options: %w", err)
|
||||
@@ -44,26 +42,20 @@ func (d *defaultDialer) DialContext(ctx context.Context, network, address string
|
||||
//
|
||||
// The most default one you can imagine. But it has tunes TCP
|
||||
// connections and setups SO_REUSEPORT.
|
||||
//
|
||||
// bufferSize is deprecated and ignored. It is kept here for backward
|
||||
// compatibility.
|
||||
func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
|
||||
switch {
|
||||
case timeout < 0:
|
||||
return nil, fmt.Errorf("timeout %v should be positive number", timeout)
|
||||
case bufferSize < 0:
|
||||
return nil, fmt.Errorf("buffer size %d should be positive number", bufferSize)
|
||||
}
|
||||
|
||||
if timeout == 0 {
|
||||
case timeout == 0:
|
||||
timeout = DefaultTimeout
|
||||
}
|
||||
|
||||
if bufferSize == 0 {
|
||||
bufferSize = DefaultBufferSize
|
||||
}
|
||||
|
||||
return &defaultDialer{
|
||||
Dialer: net.Dialer{
|
||||
Timeout: timeout,
|
||||
},
|
||||
bufferSize: bufferSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -33,10 +33,16 @@ const (
|
||||
// request.
|
||||
DefaultHTTPTimeout = 10 * time.Second
|
||||
|
||||
// Deprecated:
|
||||
//
|
||||
// DefaultBufferSize defines a TCP buffer size. Both read and write, so
|
||||
// for real size, please multiply this number by 2.
|
||||
DefaultBufferSize = 16 * 1024 // 16 kib
|
||||
|
||||
// DefaultTCPKeepAlivePeriod defines a time period between 2
|
||||
// consequitive probes.
|
||||
DefaultTCPKeepAlivePeriod = 10 * time.Second
|
||||
|
||||
// ProxyDialerOpenThreshold is used for load balancing SOCKS5 dialer
|
||||
// only.
|
||||
//
|
||||
|
||||
+11
-25
@@ -7,41 +7,27 @@ import (
|
||||
|
||||
// SetClientSocketOptions tunes a TCP socket that represents a connection to
|
||||
// end user (not Telegram service or fronting domain).
|
||||
//
|
||||
// bufferSize setting is deprecated and ignored.
|
||||
func SetClientSocketOptions(conn net.Conn, bufferSize int) error {
|
||||
tcpConn := conn.(*net.TCPConn) // nolint: forcetypeassert
|
||||
|
||||
if err := tcpConn.SetNoDelay(false); err != nil {
|
||||
return fmt.Errorf("cannot disable TCP_NO_DELAY: %w", err)
|
||||
}
|
||||
|
||||
return setCommonSocketOptions(tcpConn, bufferSize)
|
||||
return setCommonSocketOptions(conn.(*net.TCPConn))
|
||||
}
|
||||
|
||||
// SetServerSocketOptions tunes a TCP socket that represents a connection to
|
||||
// remote server like Telegram or fronting domain (but not end user).
|
||||
func SetServerSocketOptions(conn net.Conn, bufferSize int) error {
|
||||
tcpConn := conn.(*net.TCPConn) // nolint: forcetypeassert
|
||||
|
||||
if err := tcpConn.SetNoDelay(true); err != nil {
|
||||
return fmt.Errorf("cannot enable TCP_NO_DELAY: %w", err)
|
||||
}
|
||||
|
||||
return setCommonSocketOptions(tcpConn, bufferSize)
|
||||
return setCommonSocketOptions(conn.(*net.TCPConn))
|
||||
}
|
||||
|
||||
func setCommonSocketOptions(conn *net.TCPConn, bufferSize int) error {
|
||||
if err := conn.SetReadBuffer(bufferSize); err != nil {
|
||||
return fmt.Errorf("cannot set read buffer size: %w", err)
|
||||
}
|
||||
|
||||
if err := conn.SetWriteBuffer(bufferSize); err != nil {
|
||||
return fmt.Errorf("cannot set write buffer size: %w", err)
|
||||
}
|
||||
|
||||
if err := conn.SetKeepAlive(false); err != nil {
|
||||
func setCommonSocketOptions(conn *net.TCPConn) error {
|
||||
if err := conn.SetKeepAlive(true); err != nil {
|
||||
return fmt.Errorf("cannot disable TCP keepalive probes: %w", err)
|
||||
}
|
||||
|
||||
if err := conn.SetKeepAlivePeriod(DefaultTCPKeepAlivePeriod); err != nil {
|
||||
return fmt.Errorf("cannot set time period of TCP keepalive probes: %w", err)
|
||||
}
|
||||
|
||||
if err := conn.SetLinger(tcpLingerTimeout); err != nil {
|
||||
return fmt.Errorf("cannot set TCP linger timeout: %w", err)
|
||||
}
|
||||
@@ -51,7 +37,7 @@ func setCommonSocketOptions(conn *net.TCPConn, bufferSize int) error {
|
||||
return fmt.Errorf("cannot get underlying raw connection: %w", err)
|
||||
}
|
||||
|
||||
if err := setSocketReuseAddrPort(rawConn, bufferSize); err != nil {
|
||||
if err := setSocketReuseAddrPort(rawConn); err != nil {
|
||||
return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
|
||||
func setSocketReuseAddrPort(conn syscall.RawConn) error {
|
||||
var err error
|
||||
|
||||
conn.Control(func(fd uintptr) { // nolint: errcheck
|
||||
|
||||
@@ -5,6 +5,6 @@ package network
|
||||
|
||||
import "syscall"
|
||||
|
||||
func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
|
||||
func setSocketReuseAddrPort(conn syscall.RawConn) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user