{"id":379237,"date":"2021-02-23T21:35:13","date_gmt":"2021-02-23T13:35:13","guid":{"rendered":"http:\/\/4563.org\/?p=379237"},"modified":"2021-02-23T21:35:13","modified_gmt":"2021-02-23T13:35:13","slug":"golang-http-%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=379237","title":{"rendered":"golang http \u5185\u5b58\u6cc4\u6f0f\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  golang http \u5185\u5b58\u6cc4\u6f0f\u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chenqh <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4ee3\u7801<\/p>\n<pre><code>package main  import (  \"crypto\/tls\"  \"io\/ioutil\"  \"log\"  \"net\/http\"  \"os\"  \"path\"  \"strings\"  \"time\" )  func panicIfNotNil(e error) {  if e != nil {   panic(e)  } }  func loop() {  ex, err := os.Executable()  panicIfNotNil(err)   ex = path.Dir(ex)  for {   now := time.Now()   tr := &amp;http.Transport{    TLSClientConfig:   &amp;tls.Config{InsecureSkipVerify: true},    DisableKeepAlives: true, \/\/ \u53ef\u4ee5\u5173\u95ed\u94fe\u63a5   }   req, err := http.NewRequest(\"GET\", \"https:\/\/httpbin.org\/ip\", nil)   panicIfNotNil(err)   client := &amp;http.Client{    Timeout:   time.Second * 8,    Transport: tr,   }    resp, err := client.Do(req)    if resp != nil {    bodyByteArr, err := ioutil.ReadAll(resp.Body)    panicIfNotNil(err)     log.Printf(\"status_code:%d, body:%s, cost:%v\", resp.StatusCode, string(bodyByteArr), time.Since(now))    defer resp.Body.Close()   }   if err != nil &amp;&amp; strings.Index(err.Error(), \"Client.Timeout exceeded while awaiting headers\") &gt; -1 {    continue   }   panicIfNotNil(err)    time.Sleep(time.Second * 3)   } }  func main() {  loop() }  <\/code><\/pre>\n<p>\u4e00\u5f00\u59cb\u53ea\u6709 4-m \u7684\u6837\u5b50\uff0c\u8fc7\u4e86\u4e24\u5206\u949f\u5c31\u5230 10M \u5de6\u53f3\u4e86\uff0c\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5440\uff0c\u6211\u53ef\u80fd\u786e\u4fdd\u94fe\u63a5\u65f6\u5173\u95ed\u4e86\u7684 \u6c42\u5927\u4f6c\u6307\u70b9\u4e0b<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>21<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5464811\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jworg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u7ec6\u770b\u4ee3\u7801\uff0c\u4f46\u770b\u5230 transport\uff0c\u8fd9\u6709\u4e00\u7bc7\u6587\u7ae0\uff0c http:\/\/www.linvon.cn\/posts\/go-transport-%E9%95%BF%E8%BF%9E%E6%8E%A5%E5%AF%BC%E8%87%B4-goroutine-%E6%B3%84%E9%9C%B2\/                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464812\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jinliming2 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             defer resp.Body.Close() \u53ea\u4f1a\u5728 func \u63a8\u51fa\u540e\u6267\u884c\uff0c\u4f60\u8fd9\u91cc\u4e00\u76f4\u5728\u6b7b\u5faa\u73af\uff0c\u51fd\u6570\u4e0d\u63a8\u51fa\uff0cresp.Body.Close() \u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\u3002<br \/>\u4f60\u8fd9\u91cc\u76f4\u63a5\u628a defer \u53bb\u6389\u5e94\u8be5\u5c31\u597d\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464813\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Mohanson <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             for \u5faa\u73af\u91cc\u9762 defer\uff0c\u4e0a\u6b63\u89e3                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464814\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chenqh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jinliming2 \u53bb\u6389\u4e86 defer \u5185\u5b58\u4e5f\u5728\u6da8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464815\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chenqh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Mohanson \u53bb\u6389\u4e86\u4e5f\u5728\u6da8\u5440                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464816\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chenqh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             [[email&#160;protected] simple]$ go version<br \/>go version go1.15.3 linux\/amd64                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464817\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jinliming2 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             emmmm\uff0c\u770b\u4e0a\u53bb\u5e94\u8be5\u6ca1\u6709\u5176\u4ed6\u6cc4\u9732\u7684\u5730\u65b9\u4e86\uff0c\u4f60 keepAlive \u5173\u4e86\uff0c\u5e94\u8be5\u4e0d\u7528\u624b\u52a8 CloseIdleConnections \u4e86\u3002<br \/>\u5efa\u8bae\u4f60\u518d\u89c2\u5bdf\u89c2\u5bdf\uff0c\u56e0\u4e3a\u4f60\u8fd9\u4e2a\u662f\u4e00\u76f4\u5728\u5faa\u73af\uff0c\u4f60\u591a\u89c2\u5bdf\u4e00\u4f1a\uff0c\u5b83\u662f\u4f1a\u4e00\u76f4\u589e\u957f\uff0c\u8fd8\u662f\u4f1a\u505c\u7559\u5728\u4e00\u5b9a\u7a0b\u5ea6\uff1f<br \/>\u5982\u679c\u6da8\u5230\u4e00\u5b9a\u7a0b\u5ea6\u5c31\u4e0d\u6da8\u4e86\uff0c\u90a3\u53ef\u80fd\u5c31\u8ddf go \u7684\u5783\u573e\u56de\u6536\u673a\u5236\u6709\u5173\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464818\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chenqh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jinliming2 \u73b0\u5728\u5df2\u7ecf 11m \u4e86<\/p>\n<p>&#8220;`<br \/>Every 2.0s: ~\/soft\/pstat &#8211;name=&#8221;simple&#8221; &#8211;exclude=&#8221;pstat&#8221; Sat Mar 6 22:57:49 2021<\/p>\n<p>name count mem open_files net_connections<br \/>&#8212;- &#8212;&#8211; &#8212; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;&#8212;<br \/>simple 1 11.03m 20 0<\/p>\n<p>&#8220;`                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464819\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chenqh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jinliming2 \u96be\u9053\u5927\u4f6c\u4eec\u90fd\u662f\u5168\u5c40 client \u7684\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464820\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jworg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u8fd8\u662f\u76f4\u63a5\u628a Transport \u4e2d\u6ce8\u91ca\u8d34\u51fa\u6765\u5427\uff0c\u4f60\u770b\u770b\u4f60\u521b\u5efa\u4e86\u591a\u5c11\u4e2a\uff0c\u6b63\u5e38\u5e94\u8be5 reused \u7684<\/p>\n<p>\/\/ Transport is an implementation of RoundTripper that supports HTTP,<br \/>\/\/ HTTPS, and HTTP proxies (for either HTTP or HTTPS with CONNECT).<br \/>\/\/<br \/>\/\/ By default, Transport caches connections for future re-use.<br \/>\/\/ This may leave many open connections when accessing many hosts.<br \/>\/\/ This behavior can be managed using Transport&#8217;s CloseIdleConnections method<br \/>\/\/ and the MaxIdleConnsPerHost and DisableKeepAlives fields.<br \/>\/\/<br \/>\/\/ Transports should be reused instead of created as needed.<br \/>\/\/ Transports are safe for concurrent use by multiple goroutines.<br \/>\/\/<br \/>\/\/ A Transport is a low-level primitive for making HTTP and HTTPS requests.<br \/>\/\/ For high-level functionality, such as cookies and redirects, see Client.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464821\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chenqh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jworg <\/p>\n<p>&#8220;`<br \/>[[email&#160;protected] simple]$ sudo lsof -p 9575 -a -i 4 <br \/>COMMAND PID USER FD TYPE DEVICE SIZE\/OFF NODE NAME<br \/>simple 9575 vagrant 6u IPv4 6958222 0t0 TCP localhost.localdomain:38460-&gt;ec2-34-231-30-52.compute-1.amazonaws.com:https (ESTABLISHED)<br \/>&#8220;`<\/p>\n<p>\u53ea\u6709\u4e00\u4e2a                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464822\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chenqh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u73b0\u5728\u597d\u50cf\u5361\u5728 11.39 \u4e0d\u52a8\u4e86<\/p>\n<p>&#8220;`<br \/>Every 2.0s: ~\/soft\/pstat &#8211;name=&#8221;simple&#8221; &#8211;exclude=&#8221;pstat&#8221; Sat Mar 6 23:50:46 2021<\/p>\n<p>name count mem open_files net_connections<br \/>&#8212;- &#8212;&#8211; &#8212; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;&#8212;<br \/>simple 1 11.39m 20 0<\/p>\n<p>&#8220;`<\/p>\n<p>\u660e\u5929\u5728\u770b\u4e00\u4e0b                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464823\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jworg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chenqh \u6211\u4e4b\u524d\u56de\u590d\u5f04\u9519\u4e86\uff0c\u6ca1\u6709\u4ed4\u7ec6\u770b\u4ee3\u7801\u3002\u6211\u8fd9\u8fb9\u662f\u5728 11M \u5230 12M \u6ce2\u52a8\uff0c\u5e94\u8be5\u5982 7 \u6240\u8bf4\u3002\u53e6\u5916\u5efa\u8bae\u5b89\u88c5\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\uff0c\u53ef\u4ee5\u63d0\u793a\u90a3\u4e2a defer \u4ee5\u53ca resp.Body.Close()\u7684\u8fd4\u56de\u503c\u7684\u68c0\u67e5\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464824\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Immortal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             pprof \u6253\u5f00\u89c2\u5bdf\u4e0b\u5c31\u597d\u4e86 \u4f55\u5fc5\u5728\u8fd9\u91cc\u731c <br \/>\u4ee3\u7801\u770b\u8d77\u6765\u6ca1\u5565\u95ee\u9898 defer \u7684\u95ee\u9898\u524d\u9762\u4e5f\u6709\u4eba\u8bf4\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464825\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nanmu42 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u518d\u6d4b\u4e45\u4e00\u4e9b\u8bd5\u8bd5\u770b\uff0c12M \u8fd8\u592a\u5c11\u4e86\u3002<br \/>\u6bd4\u5982 10 \u4e2a\u5c0f\u65f6\u540e\u5230\u4e86 100M\uff0c\u90a3\u624d\u662f\u771f\u7684\u6f0f\u4e86\u3002<\/p>\n<p>Go 1.16 \u4e4b\u524d\uff0c\u4e0d\u7528\u7684\u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7684\u65f6\u673a\u90fd\u6bd4\u8f83\u5ef6\u540e\uff0c\u4f60\u4e5f\u53ef\u4ee5\u66f4\u65b0\u7248\u672c\u540e\u8bd5\u8bd5\uff1a https:\/\/github.com\/golang\/go\/issues\/42330                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464826\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : binbinyouliiii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Go \u4e0d\u662f\u5e26 GC \u7684\u5417\uff1f\u6ca1\u89e6\u53d1 GC \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464827\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xkeyideal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             go 1.15 \u7248\u672c\u7684\u5185\u5b58\u56de\u6536\u673a\u5236\u5bfc\u81f4\u7684\uff0c\u5347\u7ea7\u81f3 1.16 \u7248\u672c\u8bd5\u8bd5\u5427<br \/>\u53e6\u5916 defer \u7684\u95ee\u9898\uff0c\u6bcf\u6b21 new http.Client \u7684\u65b9\u5f0f\u4e5f\u662f\u9519\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464828\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jitongxi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u662f\u8fd9\u6837\u7684, \u53ea\u8981\u4f60\u7528\u4e86 go \u7684\u6807\u51c6 http \u5e93,\u5185\u5b58\u5c31\u4f1a\u4e00\u76f4\u591a 5m \u5de6\u53f3,\u5e94\u8be5\u6709\u5f88\u591a\u521d\u59cb\u5316\u7684\u4e0a\u4e0b\u6587, \u4f30\u8ba1\u8981\u770b\u6e90\u7801\u624d\u77e5\u9053                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464829\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cheng6563 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @binbinyouliiii GC \u4e86\u4e5f\u4e0d\u4f1a\u628a\u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c1.16 \u4e4b\u524d\u5c24\u5176\u5982\u6b64                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464830\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DollarKiller <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u662f\u957f\u94fe\u63a5\u95ee\u9898\uff0c\u4f60\u770b\u4e0b\u6e90\u7801\u5c31\u77e5\u9053\u4e86\uff0c \u5916\u5a46\u5356\u74dc\u4e0b github.com\/dollarkillerx\/urllib \u8fd9\u4e2a\u5e93\u6709\u6548\u7684\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5464831\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xx6412223 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5f00\u4e00\u591c\u8bd5\u8bd5\u3002\u6211\u611f\u89c9\u95ee\u9898\u4e0d\u5927                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>golang http \u5185\u5b58\u6cc4\u6f0f\u7684&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/379237"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=379237"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/379237\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=379237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=379237"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=379237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}