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

Remove idle timeout from network

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
f21ee40baf
7 измененных файлов: 11 добавлений и 31 удалений
  1. 3
    4
      cli/base.go
  2. 1
    1
      ipblocklist/firehol_test.go
  3. 0
    2
      mtglib/init.go
  4. 0
    1
      network/init.go
  5. 1
    14
      network/network.go
  6. 6
    4
      stats/statsd_test.go
  7. 0
    5
      testlib/mtglib_network_mock.go

+ 3
- 4
cli/base.go Просмотреть файл

40
 
40
 
41
 func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
41
 func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
42
 	tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
42
 	tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
43
-	idleTimeout := conf.Network.Timeout.Idle.Value(network.DefaultIdleTimeout)
44
 	httpTimeout := conf.Network.Timeout.HTTP.Value(network.DefaultHTTPTimeout)
43
 	httpTimeout := conf.Network.Timeout.HTTP.Value(network.DefaultHTTPTimeout)
45
 	dohIP := conf.Network.DOHIP.Value(net.ParseIP(network.DefaultDOHHostname)).String()
44
 	dohIP := conf.Network.DOHIP.Value(net.ParseIP(network.DefaultDOHHostname)).String()
46
 	bufferSize := conf.TCPBuffer.Value(network.DefaultBufferSize)
45
 	bufferSize := conf.TCPBuffer.Value(network.DefaultBufferSize)
61
 
60
 
62
 	switch len(proxyURLs) {
61
 	switch len(proxyURLs) {
63
 	case 0:
62
 	case 0:
64
-		return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout, idleTimeout)
63
+		return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout)
65
 	case 1:
64
 	case 1:
66
 		socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
65
 		socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
67
 		if err != nil {
66
 		if err != nil {
68
 			return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
67
 			return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
69
 		}
68
 		}
70
 
69
 
71
-		return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout, idleTimeout)
70
+		return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout)
72
 	}
71
 	}
73
 
72
 
74
 	socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
73
 	socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
76
 		return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
75
 		return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
77
 	}
76
 	}
78
 
77
 
79
-	return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout, idleTimeout)
78
+	return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout)
80
 }
79
 }

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

119
 
119
 
120
 func (suite *FireholTestSuite) TestMixed() {
120
 func (suite *FireholTestSuite) TestMixed() {
121
 	dialer, _ := network.NewDefaultDialer(0, 0)
121
 	dialer, _ := network.NewDefaultDialer(0, 0)
122
-	ntw, _ := network.NewNetwork(dialer, "mtg", "1.1.1.1", 0, 0)
122
+	ntw, _ := network.NewNetwork(dialer, "mtg", "1.1.1.1", 0)
123
 
123
 
124
 	blocklist, err := ipblocklist.NewFirehol(logger.NewNoopLogger(),
124
 	blocklist, err := ipblocklist.NewFirehol(logger.NewNoopLogger(),
125
 		ntw, 2,
125
 		ntw, 2,

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

5
 	"errors"
5
 	"errors"
6
 	"net"
6
 	"net"
7
 	"net/http"
7
 	"net/http"
8
-	"time"
9
 )
8
 )
10
 
9
 
11
 var (
10
 var (
26
 	Dial(network, address string) (net.Conn, error)
25
 	Dial(network, address string) (net.Conn, error)
27
 	DialContext(ctx context.Context, network, address string) (net.Conn, error)
26
 	DialContext(ctx context.Context, network, address string) (net.Conn, error)
28
 	MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client
27
 	MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client
29
-	IdleTimeout() time.Duration
30
 }
28
 }
31
 
29
 
32
 type AntiReplayCache interface {
30
 type AntiReplayCache interface {

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

9
 
9
 
10
 const (
10
 const (
11
 	DefaultTimeout     = 10 * time.Second
11
 	DefaultTimeout     = 10 * time.Second
12
-	DefaultIdleTimeout = time.Minute
13
 	DefaultHTTPTimeout = 10 * time.Second
12
 	DefaultHTTPTimeout = 10 * time.Second
14
 	DefaultBufferSize  = 4096
13
 	DefaultBufferSize  = 4096
15
 
14
 

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

27
 type network struct {
27
 type network struct {
28
 	dialer      Dialer
28
 	dialer      Dialer
29
 	dns         doh.Resolver
29
 	dns         doh.Resolver
30
-	idleTimeout time.Duration
31
 	httpTimeout time.Duration
30
 	httpTimeout time.Duration
32
 	userAgent   string
31
 	userAgent   string
33
 }
32
 }
71
 	return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
70
 	return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
72
 }
71
 }
73
 
72
 
74
-func (n *network) IdleTimeout() time.Duration {
75
-	return n.idleTimeout
76
-}
77
-
78
 func (n *network) dnsResolve(protocol, address string) ([]string, error) {
73
 func (n *network) dnsResolve(protocol, address string) ([]string, error) {
79
 	if net.ParseIP(address) != nil {
74
 	if net.ParseIP(address) != nil {
80
 		return []string{address}, nil
75
 		return []string{address}, nil
131
 
126
 
132
 func NewNetwork(dialer Dialer,
127
 func NewNetwork(dialer Dialer,
133
 	userAgent, dohHostname string,
128
 	userAgent, dohHostname string,
134
-	httpTimeout, idleTimeout time.Duration) (mtglib.Network, error) {
129
+	httpTimeout time.Duration) (mtglib.Network, error) {
135
 	switch {
130
 	switch {
136
 	case httpTimeout < 0:
131
 	case httpTimeout < 0:
137
 		return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
132
 		return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
139
 		httpTimeout = DefaultHTTPTimeout
134
 		httpTimeout = DefaultHTTPTimeout
140
 	}
135
 	}
141
 
136
 
142
-	switch {
143
-	case idleTimeout < 0:
144
-		return nil, fmt.Errorf("timeout should be positive number %s", idleTimeout)
145
-	case idleTimeout == 0:
146
-		idleTimeout = DefaultIdleTimeout
147
-	}
148
-
149
 	if net.ParseIP(dohHostname) == nil {
137
 	if net.ParseIP(dohHostname) == nil {
150
 		return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
138
 		return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
151
 	}
139
 	}
152
 
140
 
153
 	return &network{
141
 	return &network{
154
 		dialer:      dialer,
142
 		dialer:      dialer,
155
-		idleTimeout: idleTimeout,
156
 		httpTimeout: httpTimeout,
143
 		httpTimeout: httpTimeout,
157
 		userAgent:   userAgent,
144
 		userAgent:   userAgent,
158
 		dns: doh.Resolver{
145
 		dns: doh.Resolver{

+ 6
- 4
stats/statsd_test.go Просмотреть файл

14
 	"github.com/stretchr/testify/suite"
14
 	"github.com/stretchr/testify/suite"
15
 )
15
 )
16
 
16
 
17
+const statsdSleepTime = 3 * statsd.DefaultFlushInterval
18
+
17
 type statsdFakeServer struct {
19
 type statsdFakeServer struct {
18
 	conn *net.UDPConn
20
 	conn *net.UDPConn
19
 	buf  *bytes.Buffer
21
 	buf  *bytes.Buffer
100
 		RemoteIP:  net.ParseIP("10.0.0.10"),
102
 		RemoteIP:  net.ParseIP("10.0.0.10"),
101
 	})
103
 	})
102
 
104
 
103
-	time.Sleep(2 * statsd.DefaultFlushInterval)
105
+	time.Sleep(statsdSleepTime)
104
 	suite.Equal("mtg.active_connections:+1|g|#ip_type:ipv4", suite.statsdServer.String())
106
 	suite.Equal("mtg.active_connections:+1|g|#ip_type:ipv4", suite.statsdServer.String())
105
 
107
 
106
 	suite.statsd.EventFinish(mtglib.EventFinish{
108
 	suite.statsd.EventFinish(mtglib.EventFinish{
108
 		ConnID:    "connID",
110
 		ConnID:    "connID",
109
 	})
111
 	})
110
 
112
 
111
-	time.Sleep(2 * statsd.DefaultFlushInterval)
113
+	time.Sleep(statsdSleepTime)
112
 	suite.Contains(suite.statsdServer.String(), "mtg.session_duration")
114
 	suite.Contains(suite.statsdServer.String(), "mtg.session_duration")
113
 }
115
 }
114
 
116
 
117
 		CreatedAt: time.Now(),
119
 		CreatedAt: time.Now(),
118
 	})
120
 	})
119
 
121
 
120
-	time.Sleep(2 * statsd.DefaultFlushInterval)
122
+	time.Sleep(statsdSleepTime)
121
 	suite.Equal("mtg.concurrency_limited:1|c", suite.statsdServer.String())
123
 	suite.Equal("mtg.concurrency_limited:1|c", suite.statsdServer.String())
122
 }
124
 }
123
 
125
 
127
 		RemoteIP:  net.ParseIP("10.0.0.10"),
129
 		RemoteIP:  net.ParseIP("10.0.0.10"),
128
 	})
130
 	})
129
 
131
 
130
-	time.Sleep(2 * statsd.DefaultFlushInterval)
132
+	time.Sleep(statsdSleepTime)
131
 	suite.Equal("mtg.ip_blocklisted:1|c|#ip_type:ipv4", suite.statsdServer.String())
133
 	suite.Equal("mtg.ip_blocklisted:1|c|#ip_type:ipv4", suite.statsdServer.String())
132
 }
134
 }
133
 
135
 

+ 0
- 5
testlib/mtglib_network_mock.go Просмотреть файл

4
 	"context"
4
 	"context"
5
 	"net"
5
 	"net"
6
 	"net/http"
6
 	"net/http"
7
-	"time"
8
 
7
 
9
 	"github.com/stretchr/testify/mock"
8
 	"github.com/stretchr/testify/mock"
10
 )
9
 )
29
 	network, address string) (net.Conn, error)) *http.Client {
28
 	network, address string) (net.Conn, error)) *http.Client {
30
 	return m.Called(dialFunc).Get(0).(*http.Client)
29
 	return m.Called(dialFunc).Get(0).(*http.Client)
31
 }
30
 }
32
-
33
-func (m *MtglibNetworkMock) IdleTimeout() time.Duration {
34
-	return m.Called().Get(0).(time.Duration)
35
-}

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