{"id":126822,"date":"2020-01-19T17:47:32","date_gmt":"2020-01-19T09:47:32","guid":{"rendered":"http:\/\/4563.org\/?p=126822"},"modified":"2020-01-19T17:47:32","modified_gmt":"2020-01-19T09:47:32","slug":"golang-%e7%9a%84-http-%e8%af%b7%e6%b1%82%ef%bc%8c-transport-cancelrequest-%e4%b8%8e-client-timeout-%e6%9c%89%e5%86%b2%e7%aa%81","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=126822","title":{"rendered":"golang \u7684 http \u8bf7\u6c42\uff0c transport.CancelRequest \u4e0e client.Timeout \u6709\u51b2\u7a81"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  golang \u7684 http \u8bf7\u6c42\uff0c transport.CancelRequest \u4e0e client.Timeout \u6709\u51b2\u7a81               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : howellz <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e24\u4e2a\u9700\u6c42\uff1a<\/p>\n<ol>\n<li>\u7528\u6237\u53ef\u4ee5\u968f\u65f6\u7ec8\u6b62\u8bf7\u6c42\u3002<\/li>\n<li>\u7528\u6237\u53ef\u4ee5\u4fee\u6539\u8d85\u65f6\u503c\uff0c\u6bd4\u5982 20s \uff1b<\/li>\n<\/ol>\n<p>\u5bf9\u4e8e\u7b2c\u4e00\u4e2a\u9700\u6c42\uff0c\u8bbe\u7f6e\u4e86 client.Transport\uff0c\u5e76\u5728\u5408\u9002\u7684\u4f4d\u7f6e\u8c03\u7528\u5176 CancelRequest()\u63a5\u53e3\uff1b \u5bf9\u4e8e\u7b2c\u4e8c\u4e2a\u9700\u6c42\uff0c\u8bbe\u7f6e\u4e86 client.Timeout \uff1b<\/p>\n<p>\u4f46\u662f\u8c03\u8bd5\u4e2d\u53d1\u73b0\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86 client.Timeout\uff0c\u5219 Transport.CancelRequest()\u5c31\u4e0d\u518d\u8d77\u6548\u3002 \u6ce8\u91ca\u6389 Timeout \u5c31\u53ef\u4ee5\u751f\u6548\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>package main  import (  \"fmt\"  \"net\/http\"  \"time\" )  func main() {  trans := &amp;http.Transport{}  client := &amp;http.Client{   Transport: trans,   Timeout:   time.Second * (30), \/\/ \u8fd9\u4e00\u884c\u4e0d\u6ce8\u91ca\u5c31\u65e0\u6cd5 CancelRequest  }  req, err := http.NewRequest(\"GET\", \"https:\/\/www.google.com\", nil)  if err != nil {   panic(err)  }   go func() {   time.Sleep(time.Second * time.Duration(1))   fmt.Printf(\"%v: abortn\", time.Now())   trans.CancelRequest(req)  }()   fmt.Printf(\"%v: start ...n\", time.Now())  _, err = client.Do(req)  fmt.Printf(\"%v: donen\", time.Now())  if err != nil {   fmt.Println(err.Error())  } } <\/code><\/pre>\n<p>\u8bf7\u95ee\u6211\u8be5\u600e\u4e48\u529e\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2179833\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : howellz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5bf9\u4e86\uff0c\u81ea\u5df1\u4e0d\u5e0c\u671b\u7528 Timeou \u6765\u8c03\u7528 CanceRequest\uff0c\u8fd8\u662f\u5e0c\u671b\u7528 client \u81ea\u5df1\u7684 timeout \u53ef\u4ee5\u4e48\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2179834\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guonaihong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e86\u89e3\u4e0b WithContext \u3002\u6211\u5728 gout \u91cc\u9762\u7684\u8d85\u65f6\u5c31\u662f\u7528\u8fd9\u4e2a API \u5b9e\u73b0\u7684\u3002context \u53ef\u8d85\u65f6\u53d6\u6d88\uff0c\u53ef\u4ee5\u8c03\u7528 cancel \u53d6\u6d88\u3002<br \/>\u8fd9\u662f\u5b98\u65b9 WithContext \u7684\u6587\u6863<br \/>https:\/\/golang.google.cn\/pkg\/net\/http\/#Request.WithContext<\/p>\n<p>## \u8fd9\u662f gout \u91cc\u9762\u4f7f\u7528 WithContext \u7684\u4f8b\u5b50<br \/>https:\/\/github.com\/guonaihong\/gout\/blob\/master\/_example\/05b-cancel.go                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2179835\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SingeeKing <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Deprecated: Use Request.WithContext to create a request with a cancelable context instead                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2179836\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reus <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/gist.github.com\/reusee\/ba2a6f44fec5d70c81dcc51b6a71e744 <button onclick=\"lazyGist(this)\">\u00a0\u663e\u793a Gist \u4ee3\u7801\u00a0<\/button> <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>golang \u7684 http \u8bf7\u6c42\uff0c&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\/126822"}],"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=126822"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/126822\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=126822"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=126822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}