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

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,7 +40,6 @@ func (b *base) ReadConfig(path, version string) error {
40 40
 
41 41
 func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
42 42
 	tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
43
-	idleTimeout := conf.Network.Timeout.Idle.Value(network.DefaultIdleTimeout)
44 43
 	httpTimeout := conf.Network.Timeout.HTTP.Value(network.DefaultHTTPTimeout)
45 44
 	dohIP := conf.Network.DOHIP.Value(net.ParseIP(network.DefaultDOHHostname)).String()
46 45
 	bufferSize := conf.TCPBuffer.Value(network.DefaultBufferSize)
@@ -61,14 +60,14 @@ func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network,
61 60
 
62 61
 	switch len(proxyURLs) {
63 62
 	case 0:
64
-		return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout, idleTimeout)
63
+		return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout)
65 64
 	case 1:
66 65
 		socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
67 66
 		if err != nil {
68 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 73
 	socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
@@ -76,5 +75,5 @@ func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network,
76 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,7 +119,7 @@ func (suite *FireholTestSuite) TestRemoteFail() {
119 119
 
120 120
 func (suite *FireholTestSuite) TestMixed() {
121 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 124
 	blocklist, err := ipblocklist.NewFirehol(logger.NewNoopLogger(),
125 125
 		ntw, 2,

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

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

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

@@ -9,7 +9,6 @@ import (
9 9
 
10 10
 const (
11 11
 	DefaultTimeout     = 10 * time.Second
12
-	DefaultIdleTimeout = time.Minute
13 12
 	DefaultHTTPTimeout = 10 * time.Second
14 13
 	DefaultBufferSize  = 4096
15 14
 

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

@@ -27,7 +27,6 @@ func (n networkHTTPTransport) RoundTrip(req *http.Request) (*http.Response, erro
27 27
 type network struct {
28 28
 	dialer      Dialer
29 29
 	dns         doh.Resolver
30
-	idleTimeout time.Duration
31 30
 	httpTimeout time.Duration
32 31
 	userAgent   string
33 32
 }
@@ -71,10 +70,6 @@ func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
71 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 73
 func (n *network) dnsResolve(protocol, address string) ([]string, error) {
79 74
 	if net.ParseIP(address) != nil {
80 75
 		return []string{address}, nil
@@ -131,7 +126,7 @@ func (n *network) dnsResolve(protocol, address string) ([]string, error) {
131 126
 
132 127
 func NewNetwork(dialer Dialer,
133 128
 	userAgent, dohHostname string,
134
-	httpTimeout, idleTimeout time.Duration) (mtglib.Network, error) {
129
+	httpTimeout time.Duration) (mtglib.Network, error) {
135 130
 	switch {
136 131
 	case httpTimeout < 0:
137 132
 		return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
@@ -139,20 +134,12 @@ func NewNetwork(dialer Dialer,
139 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 137
 	if net.ParseIP(dohHostname) == nil {
150 138
 		return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
151 139
 	}
152 140
 
153 141
 	return &network{
154 142
 		dialer:      dialer,
155
-		idleTimeout: idleTimeout,
156 143
 		httpTimeout: httpTimeout,
157 144
 		userAgent:   userAgent,
158 145
 		dns: doh.Resolver{

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

@@ -14,6 +14,8 @@ import (
14 14
 	"github.com/stretchr/testify/suite"
15 15
 )
16 16
 
17
+const statsdSleepTime = 3 * statsd.DefaultFlushInterval
18
+
17 19
 type statsdFakeServer struct {
18 20
 	conn *net.UDPConn
19 21
 	buf  *bytes.Buffer
@@ -100,7 +102,7 @@ func (suite *StatsdTestSuite) TestEventStartFinish() {
100 102
 		RemoteIP:  net.ParseIP("10.0.0.10"),
101 103
 	})
102 104
 
103
-	time.Sleep(2 * statsd.DefaultFlushInterval)
105
+	time.Sleep(statsdSleepTime)
104 106
 	suite.Equal("mtg.active_connections:+1|g|#ip_type:ipv4", suite.statsdServer.String())
105 107
 
106 108
 	suite.statsd.EventFinish(mtglib.EventFinish{
@@ -108,7 +110,7 @@ func (suite *StatsdTestSuite) TestEventStartFinish() {
108 110
 		ConnID:    "connID",
109 111
 	})
110 112
 
111
-	time.Sleep(2 * statsd.DefaultFlushInterval)
113
+	time.Sleep(statsdSleepTime)
112 114
 	suite.Contains(suite.statsdServer.String(), "mtg.session_duration")
113 115
 }
114 116
 
@@ -117,7 +119,7 @@ func (suite *StatsdTestSuite) TestEventConcurrencyLimited() {
117 119
 		CreatedAt: time.Now(),
118 120
 	})
119 121
 
120
-	time.Sleep(2 * statsd.DefaultFlushInterval)
122
+	time.Sleep(statsdSleepTime)
121 123
 	suite.Equal("mtg.concurrency_limited:1|c", suite.statsdServer.String())
122 124
 }
123 125
 
@@ -127,7 +129,7 @@ func (suite *StatsdTestSuite) TestEventIPBlocklisted() {
127 129
 		RemoteIP:  net.ParseIP("10.0.0.10"),
128 130
 	})
129 131
 
130
-	time.Sleep(2 * statsd.DefaultFlushInterval)
132
+	time.Sleep(statsdSleepTime)
131 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,7 +4,6 @@ import (
4 4
 	"context"
5 5
 	"net"
6 6
 	"net/http"
7
-	"time"
8 7
 
9 8
 	"github.com/stretchr/testify/mock"
10 9
 )
@@ -29,7 +28,3 @@ func (m *MtglibNetworkMock) MakeHTTPClient(dialFunc func(ctx context.Context,
29 28
 	network, address string) (net.Conn, error)) *http.Client {
30 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
-}

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