通过对等机进行httputil.ReverseProxy连接重置

通过对等机进行httputil.ReverseProxy连接重置,http,go,proxy,Http,Go,Proxy,我的任务是制作一个反向代理应用程序,从请求/响应正文中提取一些特定信息,并将其发送到logstash。所以我决定先做一些负载测试。我编写了非常简单的后端web服务器和基本代理 后端服务器代码: 反向代理服务器代码: 正如您所看到的,后端服务器正在侦听localhost:3001和代理侦听localhost:3000 我正在使用ApacheBenchmark进行负载测试 当我运行时$ab-c20-n20000-s10http://127.0.0.1:3000/大约12k-14k请求代理开始写入错误

我的任务是制作一个反向代理应用程序,从请求/响应正文中提取一些特定信息,并将其发送到logstash。所以我决定先做一些负载测试。我编写了非常简单的后端web服务器和基本代理

后端服务器代码:

反向代理服务器代码:

正如您所看到的,后端服务器正在侦听
localhost:3001
和代理侦听
localhost:3000

我正在使用ApacheBenchmark进行负载测试

当我运行
时$ab-c20-n20000-s10http://127.0.0.1:3000/
大约12k-14k请求代理开始写入错误,如:

2016/12/23 13:02:50 http: proxy error: read tcp [::1]:58330->[::1]:3001: read: connection reset by peer
2016/12/23 13:02:50 http: proxy error: read tcp [::1]:58461->[::1]:3001: read: connection reset by peer
2016/12/23 13:02:50 http: proxy error: read tcp [::1]:58977->[::1]:3001: read: connection reset by peer
2016/12/23 13:02:50 http: proxy error: read tcp [::1]:59459->[::1]:3001: read: connection reset by peer
...
不久之后,阿帕奇板凳超时死亡。一些试验结果:

Benchmarking 127.0.0.1 (be patient)
Completed 2000 requests
Completed 4000 requests
Completed 6000 requests
Completed 8000 requests
Completed 10000 requests
^C

Server Software:        
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /
Document Length:        4 bytes

Concurrency Level:      20
Time taken for tests:   4.868 seconds
Complete requests:      11514
Failed requests:        22
   (Connect: 0, Receive: 0, Length: 22, Exceptions: 0)
Non-2xx responses:      22
Total transferred:      1381790 bytes
HTML transferred:       45968 bytes
Requests per second:    2365.28 [#/sec] (mean)
Time per request:       8.456 [ms] (mean)
Time per request:       0.423 [ms] (mean, across all concurrent requests)
Transfer rate:          277.20 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   4.2      1     111
Processing:     0    3   4.8      3     113
Waiting:        0    3   4.7      3     113
Total:          0    4   6.4      4     116

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      5
  80%      5
  90%      5
  95%      6
  98%      6
  99%      7
 100%    116 (longest request)
还有更多,如果我在第一次测试结束后立即再次启动
ab
,我将收到如下信息:

Benchmarking 127.0.0.1 (be patient)
apr_pollset_poll: The timeout specified has expired (70007)
Total of 52 requests completed
(可能是之前测试中的某些连接仍然有效)

互联网称Go中有一些DoS保护机制,显然这是连接重置的原因。 所以我的问题是:

  • 重置连接的原因(读取缓冲区已满、发生超时、连接太多或可能我的代码已断开)
  • 我可以调整这个机制吗?例如,扩展更改最大超时的缓冲区
  • 一些提示说明如何正确处理此类错误

  • 谢谢。

    ab
    正在使用http/1.0而不使用keepalive。不要使用
    ab
    。ab不是你的最佳选择。看看wrk,它像ab,但在很多方面都更好,或者vegeta()可能不是最好的,我肯定会尝试其他解决方案(谢谢),但我不能保证这个问题不会在生产中发生。所以我现在想知道如何修复/调试它。@MarkBelotskiy:see,和@JimB,这样它可以是OS限制或http.Transport限制。。。谢谢你的提示。我两个都试试,然后报告。
    Benchmarking 127.0.0.1 (be patient)
    Completed 2000 requests
    Completed 4000 requests
    Completed 6000 requests
    Completed 8000 requests
    Completed 10000 requests
    ^C
    
    Server Software:        
    Server Hostname:        127.0.0.1
    Server Port:            3000
    
    Document Path:          /
    Document Length:        4 bytes
    
    Concurrency Level:      20
    Time taken for tests:   4.868 seconds
    Complete requests:      11514
    Failed requests:        22
       (Connect: 0, Receive: 0, Length: 22, Exceptions: 0)
    Non-2xx responses:      22
    Total transferred:      1381790 bytes
    HTML transferred:       45968 bytes
    Requests per second:    2365.28 [#/sec] (mean)
    Time per request:       8.456 [ms] (mean)
    Time per request:       0.423 [ms] (mean, across all concurrent requests)
    Transfer rate:          277.20 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    1   4.2      1     111
    Processing:     0    3   4.8      3     113
    Waiting:        0    3   4.7      3     113
    Total:          0    4   6.4      4     116
    
    Percentage of the requests served within a certain time (ms)
      50%      4
      66%      4
      75%      5
      80%      5
      90%      5
      95%      6
      98%      6
      99%      7
     100%    116 (longest request)
    
    Benchmarking 127.0.0.1 (be patient)
    apr_pollset_poll: The timeout specified has expired (70007)
    Total of 52 requests completed