Просмотр исходного кода

Deprecate bufferSize

tags/v2.1.3^2
9seconds 4 лет назад
Родитель
Сommit
d19cfb1df4

+ 2
- 4
internal/cli/run_proxy.go Просмотреть файл

38
 	tcpTimeout := conf.Network.Timeout.TCP.Get(network.DefaultTimeout)
38
 	tcpTimeout := conf.Network.Timeout.TCP.Get(network.DefaultTimeout)
39
 	httpTimeout := conf.Network.Timeout.HTTP.Get(network.DefaultHTTPTimeout)
39
 	httpTimeout := conf.Network.Timeout.HTTP.Get(network.DefaultHTTPTimeout)
40
 	dohIP := conf.Network.DOHIP.Get(net.ParseIP(network.DefaultDOHHostname)).String()
40
 	dohIP := conf.Network.DOHIP.Get(net.ParseIP(network.DefaultDOHHostname)).String()
41
-	bufferSize := conf.TCPBuffer.Get(network.DefaultBufferSize)
42
 	userAgent := "mtg/" + version
41
 	userAgent := "mtg/" + version
43
 
42
 
44
-	baseDialer, err := network.NewDefaultDialer(tcpTimeout, int(bufferSize))
43
+	baseDialer, err := network.NewDefaultDialer(tcpTimeout, 0)
45
 	if err != nil {
44
 	if err != nil {
46
 		return nil, fmt.Errorf("cannot build a default dialer: %w", err)
45
 		return nil, fmt.Errorf("cannot build a default dialer: %w", err)
47
 	}
46
 	}
181
 		EventStream:     eventStream,
180
 		EventStream:     eventStream,
182
 
181
 
183
 		Secret:             conf.Secret,
182
 		Secret:             conf.Secret,
184
-		BufferSize:         conf.TCPBuffer.Get(mtglib.DefaultBufferSize),
185
 		DomainFrontingPort: conf.DomainFrontingPort.Get(mtglib.DefaultDomainFrontingPort),
183
 		DomainFrontingPort: conf.DomainFrontingPort.Get(mtglib.DefaultDomainFrontingPort),
186
 		PreferIP:           conf.PreferIP.Get(mtglib.DefaultPreferIP),
184
 		PreferIP:           conf.PreferIP.Get(mtglib.DefaultPreferIP),
187
 
185
 
193
 		return fmt.Errorf("cannot create a proxy: %w", err)
191
 		return fmt.Errorf("cannot create a proxy: %w", err)
194
 	}
192
 	}
195
 
193
 
196
-	listener, err := utils.NewListener(conf.BindTo.Get(""), int(opts.BufferSize))
194
+	listener, err := utils.NewListener(conf.BindTo.Get(""), 0)
197
 	if err != nil {
195
 	if err != nil {
198
 		return fmt.Errorf("cannot start proxy: %w", err)
196
 		return fmt.Errorf("cannot start proxy: %w", err)
199
 	}
197
 	}

+ 0
- 4
internal/cli/simple_run.go Просмотреть файл

38
 		return fmt.Errorf("incorrect concurrency: %w", err)
38
 		return fmt.Errorf("incorrect concurrency: %w", err)
39
 	}
39
 	}
40
 
40
 
41
-	if err := conf.TCPBuffer.Set(s.TCPBuffer); err != nil {
42
-		return fmt.Errorf("incorrect tcp-buffer: %w", err)
43
-	}
44
-
45
 	if err := conf.PreferIP.Set(s.PreferIP); err != nil {
41
 	if err := conf.PreferIP.Set(s.PreferIP); err != nil {
46
 		return fmt.Errorf("incorrect prefer-ip: %w", err)
42
 		return fmt.Errorf("incorrect prefer-ip: %w", err)
47
 	}
43
 	}

+ 0
- 1
internal/config/config.go Просмотреть файл

13
 	AllowFallbackOnUnknownDC TypeBool        `json:"allowFallbackOnUnknownDc"`
13
 	AllowFallbackOnUnknownDC TypeBool        `json:"allowFallbackOnUnknownDc"`
14
 	Secret                   mtglib.Secret   `json:"secret"`
14
 	Secret                   mtglib.Secret   `json:"secret"`
15
 	BindTo                   TypeHostPort    `json:"bindTo"`
15
 	BindTo                   TypeHostPort    `json:"bindTo"`
16
-	TCPBuffer                TypeBytes       `json:"tcpBuffer"`
17
 	PreferIP                 TypePreferIP    `json:"preferIp"`
16
 	PreferIP                 TypePreferIP    `json:"preferIp"`
18
 	DomainFrontingPort       TypePort        `json:"domainFrontingPort"`
17
 	DomainFrontingPort       TypePort        `json:"domainFrontingPort"`
19
 	TolerateTimeSkewness     TypeDuration    `json:"tolerateTimeSkewness"`
18
 	TolerateTimeSkewness     TypeDuration    `json:"tolerateTimeSkewness"`

+ 0
- 1
internal/config/parse.go Просмотреть файл

13
 	AllowFallbackOnUnknownDC bool   `toml:"allow-fallback-on-unknown-dc" json:"allowFallbackOnUnknownDc,omitempty"`
13
 	AllowFallbackOnUnknownDC bool   `toml:"allow-fallback-on-unknown-dc" json:"allowFallbackOnUnknownDc,omitempty"`
14
 	Secret                   string `toml:"secret" json:"secret"`
14
 	Secret                   string `toml:"secret" json:"secret"`
15
 	BindTo                   string `toml:"bind-to" json:"bindTo"`
15
 	BindTo                   string `toml:"bind-to" json:"bindTo"`
16
-	TCPBuffer                string `toml:"tcp-buffer" json:"tcpBuffer,omitempty"`
17
 	PreferIP                 string `toml:"prefer-ip" json:"preferIp,omitempty"`
16
 	PreferIP                 string `toml:"prefer-ip" json:"preferIp,omitempty"`
18
 	DomainFrontingPort       uint   `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
17
 	DomainFrontingPort       uint   `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
19
 	TolerateTimeSkewness     string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
18
 	TolerateTimeSkewness     string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`

+ 2
- 5
internal/utils/net_listener.go Просмотреть файл

9
 
9
 
10
 type Listener struct {
10
 type Listener struct {
11
 	net.Listener
11
 	net.Listener
12
-
13
-	bufferSize int
14
 }
12
 }
15
 
13
 
16
 func (l Listener) Accept() (net.Conn, error) {
14
 func (l Listener) Accept() (net.Conn, error) {
19
 		return nil, err // nolint: wrapcheck
17
 		return nil, err // nolint: wrapcheck
20
 	}
18
 	}
21
 
19
 
22
-	if err := network.SetClientSocketOptions(conn, l.bufferSize); err != nil {
20
+	if err := network.SetClientSocketOptions(conn, 0); err != nil {
23
 		conn.Close()
21
 		conn.Close()
24
 
22
 
25
 		return nil, fmt.Errorf("cannot set TCP options: %w", err)
23
 		return nil, fmt.Errorf("cannot set TCP options: %w", err)
35
 	}
33
 	}
36
 
34
 
37
 	return Listener{
35
 	return Listener{
38
-		Listener:   base,
39
-		bufferSize: bufferSize,
36
+		Listener: base,
40
 	}, nil
37
 	}, nil
41
 }
38
 }

+ 2
- 0
mtglib/proxy_opts.go Просмотреть файл

45
 	// buffers: to and from.
45
 	// buffers: to and from.
46
 	//
46
 	//
47
 	// This is an optional setting.
47
 	// This is an optional setting.
48
+	//
49
+	// Deprecated: this setting is no longer makes any effect.
48
 	BufferSize uint
50
 	BufferSize uint
49
 
51
 
50
 	// Concurrency is a size of the worker pool for connection management.
52
 	// Concurrency is a size of the worker pool for connection management.

+ 5
- 13
network/default.go Просмотреть файл

9
 
9
 
10
 type defaultDialer struct {
10
 type defaultDialer struct {
11
 	net.Dialer
11
 	net.Dialer
12
-
13
-	bufferSize int
14
 }
12
 }
15
 
13
 
16
 func (d *defaultDialer) Dial(network, address string) (net.Conn, error) {
14
 func (d *defaultDialer) Dial(network, address string) (net.Conn, error) {
30
 	}
28
 	}
31
 
29
 
32
 	// we do not need to call to end user. End users call us.
30
 	// we do not need to call to end user. End users call us.
33
-	if err := SetServerSocketOptions(conn, d.bufferSize); err != nil {
31
+	if err := SetServerSocketOptions(conn, 0); err != nil {
34
 		conn.Close()
32
 		conn.Close()
35
 
33
 
36
 		return nil, fmt.Errorf("cannot set socket options: %w", err)
34
 		return nil, fmt.Errorf("cannot set socket options: %w", err)
44
 //
42
 //
45
 // The most default one you can imagine. But it has tunes TCP
43
 // The most default one you can imagine. But it has tunes TCP
46
 // connections and setups SO_REUSEPORT.
44
 // connections and setups SO_REUSEPORT.
45
+//
46
+// bufferSize is deprecated and ignored. It is kept here for backward
47
+// compatibility.
47
 func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
48
 func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
48
 	switch {
49
 	switch {
49
 	case timeout < 0:
50
 	case timeout < 0:
50
 		return nil, fmt.Errorf("timeout %v should be positive number", timeout)
51
 		return nil, fmt.Errorf("timeout %v should be positive number", timeout)
51
-	case bufferSize < 0:
52
-		return nil, fmt.Errorf("buffer size %d should be positive number", bufferSize)
53
-	}
54
-
55
-	if timeout == 0 {
52
+	case timeout == 0:
56
 		timeout = DefaultTimeout
53
 		timeout = DefaultTimeout
57
 	}
54
 	}
58
 
55
 
59
-	if bufferSize == 0 {
60
-		bufferSize = DefaultBufferSize
61
-	}
62
-
63
 	return &defaultDialer{
56
 	return &defaultDialer{
64
 		Dialer: net.Dialer{
57
 		Dialer: net.Dialer{
65
 			Timeout: timeout,
58
 			Timeout: timeout,
66
 		},
59
 		},
67
-		bufferSize: bufferSize,
68
 	}, nil
60
 	}, nil
69
 }
61
 }

+ 6
- 0
network/init.go Просмотреть файл

33
 	// request.
33
 	// request.
34
 	DefaultHTTPTimeout = 10 * time.Second
34
 	DefaultHTTPTimeout = 10 * time.Second
35
 
35
 
36
+	// Deprecated:
37
+	//
36
 	// DefaultBufferSize defines a TCP buffer size. Both read and write, so
38
 	// DefaultBufferSize defines a TCP buffer size. Both read and write, so
37
 	// for real size, please multiply this number by 2.
39
 	// for real size, please multiply this number by 2.
38
 	DefaultBufferSize = 16 * 1024 // 16 kib
40
 	DefaultBufferSize = 16 * 1024 // 16 kib
39
 
41
 
42
+	// DefaultTCPKeepAlivePeriod defines a time period between 2
43
+	// consequitive probes.
44
+	DefaultTCPKeepAlivePeriod = 10 * time.Second
45
+
40
 	// ProxyDialerOpenThreshold is used for load balancing SOCKS5 dialer
46
 	// ProxyDialerOpenThreshold is used for load balancing SOCKS5 dialer
41
 	// only.
47
 	// only.
42
 	//
48
 	//

+ 10
- 24
network/sockopts.go Просмотреть файл

7
 
7
 
8
 // SetClientSocketOptions tunes a TCP socket that represents a connection to
8
 // SetClientSocketOptions tunes a TCP socket that represents a connection to
9
 // end user (not Telegram service or fronting domain).
9
 // end user (not Telegram service or fronting domain).
10
+//
11
+// bufferSize setting is deprecated and ignored.
10
 func SetClientSocketOptions(conn net.Conn, bufferSize int) error {
12
 func SetClientSocketOptions(conn net.Conn, bufferSize int) error {
11
-	tcpConn := conn.(*net.TCPConn) // nolint: forcetypeassert
12
-
13
-	if err := tcpConn.SetNoDelay(false); err != nil {
14
-		return fmt.Errorf("cannot disable TCP_NO_DELAY: %w", err)
15
-	}
16
-
17
-	return setCommonSocketOptions(tcpConn, bufferSize)
13
+	return setCommonSocketOptions(conn.(*net.TCPConn))
18
 }
14
 }
19
 
15
 
20
 // SetServerSocketOptions tunes a TCP socket that represents a connection to
16
 // SetServerSocketOptions tunes a TCP socket that represents a connection to
21
 // remote server like Telegram or fronting domain (but not end user).
17
 // remote server like Telegram or fronting domain (but not end user).
22
 func SetServerSocketOptions(conn net.Conn, bufferSize int) error {
18
 func SetServerSocketOptions(conn net.Conn, bufferSize int) error {
23
-	tcpConn := conn.(*net.TCPConn) // nolint: forcetypeassert
24
-
25
-	if err := tcpConn.SetNoDelay(true); err != nil {
26
-		return fmt.Errorf("cannot enable TCP_NO_DELAY: %w", err)
27
-	}
28
-
29
-	return setCommonSocketOptions(tcpConn, bufferSize)
19
+	return setCommonSocketOptions(conn.(*net.TCPConn))
30
 }
20
 }
31
 
21
 
32
-func setCommonSocketOptions(conn *net.TCPConn, bufferSize int) error {
33
-	if err := conn.SetReadBuffer(bufferSize); err != nil {
34
-		return fmt.Errorf("cannot set read buffer size: %w", err)
35
-	}
36
-
37
-	if err := conn.SetWriteBuffer(bufferSize); err != nil {
38
-		return fmt.Errorf("cannot set write buffer size: %w", err)
22
+func setCommonSocketOptions(conn *net.TCPConn) error {
23
+	if err := conn.SetKeepAlive(true); err != nil {
24
+		return fmt.Errorf("cannot disable TCP keepalive probes: %w", err)
39
 	}
25
 	}
40
 
26
 
41
-	if err := conn.SetKeepAlive(false); err != nil {
42
-		return fmt.Errorf("cannot disable TCP keepalive probes: %w", err)
27
+	if err := conn.SetKeepAlivePeriod(DefaultTCPKeepAlivePeriod); err != nil {
28
+		return fmt.Errorf("cannot set time period of TCP keepalive probes: %w", err)
43
 	}
29
 	}
44
 
30
 
45
 	if err := conn.SetLinger(tcpLingerTimeout); err != nil {
31
 	if err := conn.SetLinger(tcpLingerTimeout); err != nil {
51
 		return fmt.Errorf("cannot get underlying raw connection: %w", err)
37
 		return fmt.Errorf("cannot get underlying raw connection: %w", err)
52
 	}
38
 	}
53
 
39
 
54
-	if err := setSocketReuseAddrPort(rawConn, bufferSize); err != nil {
40
+	if err := setSocketReuseAddrPort(rawConn); err != nil {
55
 		return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
41
 		return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
56
 	}
42
 	}
57
 
43
 

+ 1
- 1
network/sockopts_unix.go Просмотреть файл

10
 	"golang.org/x/sys/unix"
10
 	"golang.org/x/sys/unix"
11
 )
11
 )
12
 
12
 
13
-func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
13
+func setSocketReuseAddrPort(conn syscall.RawConn) error {
14
 	var err error
14
 	var err error
15
 
15
 
16
 	conn.Control(func(fd uintptr) { // nolint: errcheck
16
 	conn.Control(func(fd uintptr) { // nolint: errcheck

+ 1
- 1
network/sockopts_windows.go Просмотреть файл

5
 
5
 
6
 import "syscall"
6
 import "syscall"
7
 
7
 
8
-func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
8
+func setSocketReuseAddrPort(conn syscall.RawConn) error {
9
 	return nil
9
 	return nil
10
 }
10
 }

Загрузка…
Отмена
Сохранить