Переглянути джерело

Add new TCPBufferSize parameter to network

tags/v2.0.0-rc1
9seconds 5 роки тому
джерело
коміт
57cb1b5aa0

+ 1
- 0
mtglib/init.go Переглянути файл

27
 	DialContext(ctx context.Context, network, address string) (net.Conn, error)
27
 	DialContext(ctx context.Context, network, address string) (net.Conn, error)
28
 	MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client
28
 	MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client
29
 	IdleTimeout() time.Duration
29
 	IdleTimeout() time.Duration
30
+	TCPBufferSize() int
30
 }
31
 }
31
 
32
 
32
 type AntiReplayCache interface {
33
 type AntiReplayCache interface {

+ 4
- 0
network/default.go Переглянути файл

60
 	return tcpConn, nil
60
 	return tcpConn, nil
61
 }
61
 }
62
 
62
 
63
+func (d *defaultDialer) TCPBufferSize() int {
64
+	return d.bufferSize
65
+}
66
+
63
 func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
67
 func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
64
 	switch {
68
 	switch {
65
 	case timeout < 0:
69
 	case timeout < 0:

+ 4
- 0
network/default_test.go Переглянути файл

71
 	suite.Equal(http.StatusOK, resp.StatusCode)
71
 	suite.Equal(http.StatusOK, resp.StatusCode)
72
 }
72
 }
73
 
73
 
74
+func (suite *DefaultDialerTestSuite) TestTCPBufferSize() {
75
+	suite.Equal(network.DefaultBufferSize, suite.d.TCPBufferSize())
76
+}
77
+
74
 func TestDefaultDialer(t *testing.T) {
78
 func TestDefaultDialer(t *testing.T) {
75
 	t.Parallel()
79
 	t.Parallel()
76
 	suite.Run(t, &DefaultDialerTestSuite{})
80
 	suite.Run(t, &DefaultDialerTestSuite{})

+ 1
- 0
network/init.go Переглянути файл

29
 type Dialer interface {
29
 type Dialer interface {
30
 	Dial(network, address string) (net.Conn, error)
30
 	Dial(network, address string) (net.Conn, error)
31
 	DialContext(ctx context.Context, network, address string) (net.Conn, error)
31
 	DialContext(ctx context.Context, network, address string) (net.Conn, error)
32
+	TCPBufferSize() int
32
 }
33
 }

+ 4
- 0
network/init_internal_test.go Переглянути файл

22
 
22
 
23
 	return args.Get(0).(net.Conn), args.Error(1)
23
 	return args.Get(0).(net.Conn), args.Error(1)
24
 }
24
 }
25
+
26
+func (d *DialerMock) TCPBufferSize() int {
27
+	return d.Called().Int(0)
28
+}

+ 4
- 0
network/init_test.go Переглянути файл

30
 	return args.Get(0).(net.Conn), args.Error(1)
30
 	return args.Get(0).(net.Conn), args.Error(1)
31
 }
31
 }
32
 
32
 
33
+func (d *DialerMock) TCPBufferSize() int {
34
+	return d.Called().Int(0)
35
+}
36
+
33
 type HTTPServerTestSuite struct {
37
 type HTTPServerTestSuite struct {
34
 	httpServer *httptest.Server
38
 	httpServer *httptest.Server
35
 }
39
 }

+ 8
- 2
network/load_balanced_socks5.go Переглянути файл

9
 )
9
 )
10
 
10
 
11
 type loadBalancedSocks5Dialer struct {
11
 type loadBalancedSocks5Dialer struct {
12
-	dialers []Dialer
12
+	dialers    []Dialer
13
+	bufferSize int
13
 }
14
 }
14
 
15
 
15
 func (l loadBalancedSocks5Dialer) Dial(network, address string) (net.Conn, error) {
16
 func (l loadBalancedSocks5Dialer) Dial(network, address string) (net.Conn, error) {
16
 	return l.DialContext(context.Background(), network, address)
17
 	return l.DialContext(context.Background(), network, address)
17
 }
18
 }
18
 
19
 
20
+func (l loadBalancedSocks5Dialer) TCPBufferSize() int {
21
+	return l.bufferSize
22
+}
23
+
19
 func (l loadBalancedSocks5Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
24
 func (l loadBalancedSocks5Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
20
 	length := len(l.dialers)
25
 	length := len(l.dialers)
21
 	start := rand.Intn(length)
26
 	start := rand.Intn(length)
45
 	}
50
 	}
46
 
51
 
47
 	return loadBalancedSocks5Dialer{
52
 	return loadBalancedSocks5Dialer{
48
-		dialers: dialers,
53
+		dialers:    dialers,
54
+		bufferSize: baseDialer.TCPBufferSize(),
49
 	}, nil
55
 	}, nil
50
 }
56
 }

+ 1
- 0
network/load_balanced_socks5_test.go Переглянути файл

57
 	baseDialer.On("DialContext", mock.Anything, "tcp", "127.0.0.2:1080").
57
 	baseDialer.On("DialContext", mock.Anything, "tcp", "127.0.0.2:1080").
58
 		Times(network.ProxyDialerOpenThreshold).
58
 		Times(network.ProxyDialerOpenThreshold).
59
 		Return(&net.TCPConn{}, io.EOF)
59
 		Return(&net.TCPConn{}, io.EOF)
60
+	baseDialer.On("TCPBufferSize").Return(network.DefaultBufferSize)
60
 
61
 
61
 	lbDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, []*url.URL{
62
 	lbDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, []*url.URL{
62
 		{Scheme: "socks5", User: url.UserPassword("user", "password"), Host: "127.0.0.1:1080"},
63
 		{Scheme: "socks5", User: url.UserPassword("user", "password"), Host: "127.0.0.1:1080"},

+ 4
- 0
network/network.go Переглянути файл

75
 	return n.idleTimeout
75
 	return n.idleTimeout
76
 }
76
 }
77
 
77
 
78
+func (n *network) TCPBufferSize() int {
79
+	return n.dialer.TCPBufferSize()
80
+}
81
+
78
 func (n *network) dnsResolve(protocol, address string) ([]string, error) {
82
 func (n *network) dnsResolve(protocol, address string) ([]string, error) {
79
 	if net.ParseIP(address) != nil {
83
 	if net.ParseIP(address) != nil {
80
 		return []string{address}, nil
84
 		return []string{address}, nil

+ 19
- 1
network/socks5.go Переглянути файл

2
 
2
 
3
 import (
3
 import (
4
 	"fmt"
4
 	"fmt"
5
+	"net"
5
 	"net/url"
6
 	"net/url"
6
 
7
 
7
 	"golang.org/x/net/proxy"
8
 	"golang.org/x/net/proxy"
8
 )
9
 )
9
 
10
 
11
+type socks5Dialer struct {
12
+	proxy.ContextDialer
13
+
14
+	bufferSize int
15
+}
16
+
17
+func (s socks5Dialer) Dial(protocol, address string) (net.Conn, error) {
18
+	return s.ContextDialer.(proxy.Dialer).Dial(protocol, address)
19
+}
20
+
21
+func (s socks5Dialer) TCPBufferSize() int {
22
+	return s.bufferSize
23
+}
24
+
10
 func NewSocks5Dialer(baseDialer Dialer, proxyURL *url.URL) (Dialer, error) {
25
 func NewSocks5Dialer(baseDialer Dialer, proxyURL *url.URL) (Dialer, error) {
11
 	rv, err := proxy.FromURL(proxyURL, baseDialer)
26
 	rv, err := proxy.FromURL(proxyURL, baseDialer)
12
 	if err != nil {
27
 	if err != nil {
13
 		return nil, fmt.Errorf("cannot initialize socks5 proxy dialer: %w", err)
28
 		return nil, fmt.Errorf("cannot initialize socks5 proxy dialer: %w", err)
14
 	}
29
 	}
15
 
30
 
16
-	return rv.(Dialer), nil
31
+	return socks5Dialer{
32
+		ContextDialer: rv.(proxy.ContextDialer),
33
+		bufferSize:    baseDialer.TCPBufferSize(),
34
+	}, nil
17
 }
35
 }

+ 4
- 0
testlib/mtglib_network_mock.go Переглянути файл

33
 func (m *MtglibNetworkMock) IdleTimeout() time.Duration {
33
 func (m *MtglibNetworkMock) IdleTimeout() time.Duration {
34
 	return m.Called().Get(0).(time.Duration)
34
 	return m.Called().Get(0).(time.Duration)
35
 }
35
 }
36
+
37
+func (m *MtglibNetworkMock) TCPBufferSize() int {
38
+	return m.Called().Int(0)
39
+}

Завантаження…
Відмінити
Зберегти