{"id":200033,"date":"2020-11-14T09:48:18","date_gmt":"2020-11-14T01:48:18","guid":{"rendered":"http:\/\/4563.org\/?p=200033"},"modified":"2020-11-14T09:48:18","modified_gmt":"2020-11-14T01:48:18","slug":"%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa-http-router-%e5%ba%93%e7%8e%a9%e7%8e%a9%e3%80%82%e3%80%82%e3%80%82","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=200033","title":{"rendered":"\u5b9e\u73b0\u4e00\u4e2a http router \u5e93\u73a9\u73a9\u3002\u3002\u3002"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5b9e\u73b0\u4e00\u4e2a http router \u5e93\u73a9\u73a9\u3002\u3002\u3002               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guonaihong <\/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<h2>brouter<\/h2>\n<p>\u9879\u76ee\u4e00\u5f00\u59cb\u65f6\u53ea\u662f\u4e00\u4e2a\u65b0\u7684\u5c1d\u8bd5\uff0c\u770b\u80fd\u5426\u6027\u80fd\u6bd4 httprouter \u66f4\u5feb\u3002<\/p>\n<h2>\u9879\u76ee\u5730\u5740<\/h2>\n<p>https:\/\/github.com\/antlabs\/brouter<\/p>\n<h2>demo<\/h2>\n<pre><code>package main  import (     \"fmt\"     \"net\/http\"     \"log\"      \"github.com\/antlabs\/brouter\" )  func Index(w http.ResponseWriter, r *http.Request, _ brouter.Params) {     fmt.Fprint(w, \"Welcome!n\") }  func Hello(w http.ResponseWriter, r *http.Request, ps brouter.Params) {     fmt.Fprintf(w, \"hello, %s!n\", ps.ByName(\"name\")) }  func main() {     router := brouter.New()     router.GET(\"\/\", Index)     router.GET(\"\/hello\/:name\", Hello)      log.Fatal( http.ListenAndServe(\":8080\", router)) }  <\/code><\/pre>\n<h2>\u6d4b\u8bd5\u7ed3\u679c<\/h2>\n<ul>\n<li>httprouter 1.3<\/li>\n<li>brouter 0.0.1<\/li>\n<\/ul>\n<pre><code>GithubAPI Routes: 180 GithubAPI2 Routes: 203    BeegoMuxRouter: 97024 Bytes    BoneRouter: 86368 Bytes    ChiRouter: 64584 Bytes    HttpRouter: 35360 Bytes    BRouter: 51096 Bytes    trie-mux: 121736 Bytes    MuxRouter: 1373064 Bytes    GoRouter1: 76528 Bytes    GoRouter2: 84624 Bytes #Static Routes: 157    HttpRouter: 21680 Bytes    BRouter: 35208 Bytes  goos: linux goarch: amd64 pkg: test BenchmarkBeegoMuxRouterWithGithubAPI-4       10000     111713 ns\/op   116352 B\/op      900 allocs\/op BenchmarkBoneRouterWithGithubAPI-4             721    1546779 ns\/op   562878 B\/op     6807 allocs\/op BenchmarkTrieMuxRouterWithGithubAPI-4        19060      64217 ns\/op    57024 B\/op      468 allocs\/op BenchmarkBRouterWithGithubAPI-4              70467      16971 ns\/op        0 B\/op        0 allocs\/op BenchmarkHttpRouterWithGithubAPI-4           45830      26609 ns\/op    11744 B\/op      144 allocs\/op BenchmarkGoRouter1WithGithubAPI-4            24042      49632 ns\/op    11920 B\/op      360 allocs\/op BenchmarkGoRouter2WithGithubAPI2-4           21088      57408 ns\/op    13832 B\/op      406 allocs\/op BenchmarkChiRouterWithGithubAPI2-4            8469     128427 ns\/op   106000 B\/op     1110 allocs\/op BenchmarkMuxRouterWithGithubAPI2-4             358    3414899 ns\/op    59373 B\/op      992 allocs\/op BenchmarkHttpRouter_StaticAll-4             120054       9994 ns\/op        0 B\/op        0 allocs\/op BenchmarkBRouter_StaticAll-4                111614      10838 ns\/op        0 B\/op        0 allocs\/op PASS ok   test 15.950s <\/code><\/pre>\n<ul>\n<li>httprouter fe77dd05ab5a80f54110cccf1b7d8681c2648323(\u5728\u6d4b\u8bd5\u8fd9\u7248\u672c\u65f6\u5019\u9047\u5230\u8fc7 panic)<\/li>\n<li>brouter 0.0.1<\/li>\n<\/ul>\n<pre><code>GithubAPI Routes: 180 GithubAPI2 Routes: 203    BeegoMuxRouter: 97232 Bytes    BoneRouter: 88296 Bytes    ChiRouter: 64592 Bytes    HttpRouter: 33480 Bytes    BRouter: 51096 Bytes    trie-mux: 123448 Bytes    MuxRouter: 1373064 Bytes    GoRouter1: 76320 Bytes    GoRouter2: 85040 Bytes #Static Routes: 157    HttpRouter: 21712 Bytes    BRouter: 35200 Bytes  goos: linux goarch: amd64 pkg: test BenchmarkBeegoMuxRouterWithGithubAPI-4       10000     111217 ns\/op   116352 B\/op      900 allocs\/op BenchmarkBoneRouterWithGithubAPI-4             780    1554833 ns\/op   562871 B\/op     6807 allocs\/op BenchmarkTrieMuxRouterWithGithubAPI-4        19020      62882 ns\/op    57024 B\/op      468 allocs\/op BenchmarkBRouterWithGithubAPI-4              69400      17426 ns\/op        0 B\/op        0 allocs\/op BenchmarkHttpRouterWithGithubAPI-4           75178      15907 ns\/op        0 B\/op        0 allocs\/op BenchmarkGoRouter1WithGithubAPI-4            24426      49969 ns\/op    11920 B\/op      360 allocs\/op BenchmarkGoRouter2WithGithubAPI2-4           21087      56849 ns\/op    13832 B\/op      406 allocs\/op BenchmarkChiRouterWithGithubAPI2-4            8490     129326 ns\/op   105974 B\/op     1110 allocs\/op BenchmarkMuxRouterWithGithubAPI2-4             349    3383843 ns\/op    59369 B\/op      992 allocs\/op BenchmarkHttpRouter_StaticAll-4             120390      10094 ns\/op        0 B\/op        0 allocs\/op BenchmarkBRouter_StaticAll-4                113125      10633 ns\/op        0 B\/op        0 allocs\/op PASS ok   test 15.887s  <\/code><\/pre>\n<h2>\u6d4b\u8bd5\u4ee3\u7801\u4f4d\u7f6e<\/h2>\n<p>https:\/\/github.com\/junelabs\/brouter-benchmark<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4276163\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : guonaihong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53d1\u4e86\u8fd9\u4e48\u4f1a\u513f\uff0c\u90fd\u6ca1\u4eba\u627e\u6211\u5439\u725b\u7684\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4276164\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : miniliuke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @guonaihong \u56e0\u4e3a\u4f60\u6ca1\u6709\u6574\u6d3b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4276165\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : guonaihong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @miniliuke \u5565\u662f\u6574\u6d3b\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4276166\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : eudore <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u6211\u7684\u6bd4\u4f60\u7b80\u5355\u70b9\uff0c\u6211\u4e5f\u8d34\u4e0b\u6211\u7684\u8def\u7531\u5668\u548c\u6027\u80fd\u6d4b\u8bd5\uff0c\u6027\u80fd\u662f httprouter \u7684 70-90%\u5185\u5b58\u66f4\u5c11\u529f\u80fd\u66f4\u591a\uff0c\u8282\u70b9\u5339\u914d\u987a\u5e8f\u6d4b\u8bd5\u548c httprouter \u4e0d\u540c\uff0cmux \u8fd9\u4e2a\u5e93\u611f\u89c9\u6027\u80fd\u4f4e\uff0c\u4f46\u662f\u6ca1\u60f3\u5230\u6027\u80fd\u5dee\u51e0\u767e\u500d\u4e86\u3002<\/p>\n<p>https:\/\/github.com\/eudore\/eudore\/blob\/master\/routerstd.go<\/p>\n<p>https:\/\/github.com\/eudore\/web-framework-benchmark                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4276167\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : guonaihong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @eudore \u54c8\u54c8\uff0c\u4e3a\u4e86\u6027\u80fd\u653e\u5f03\u4e86\u4e00\u4e9b\u53ef\u8bfb\u6027\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4276168\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : guonaihong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @eudore httprouter \u627e\u513f\u5b50\u8282\u70b9\u7684\u65b9\u5f0f\u4f1a\u751f\u6210\u66f4\u5c11\u7684\u6c47\u7f16\u4ee3\u7801\uff0c\u6240\u4ee5\u6548\u7387\u5f88\u9ad8\u3002\u4e00\u5f00\u59cb\u6211\u7528\u67e5\u8868\u7684\u65b9\u5f0f\uff0c\u90fd\u6162\u4e00\u70b9\u3002\u662f\u4e0d\u662f\u5f88\u8fdd\u80cc\u76f4\u89c9\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4276169\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : eudore <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @guonaihong \u9996\u5148\u9ad8\u6027\u80fd\u8def\u7531\u5668\u4f7f\u7528 radix \u548c trie \u662f\u6807\u914d\uff0c\u6ca1\u6709\u7684\u5c31\u662f\u4f4e\u6027\u80fd\u4f8b\u5982 mux \u3002<\/p>\n<p>\u5728 httprouter \u4e2d\u5982\u679c\u8282\u70b9\u662f\u4e0b\u9762\u7684\u8def\u7531\u89c4\u5219\u8d8a\u591a\uff0c\u4ed6\u7684\u6743\u91cd\u8d8a\u9ad8\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8282\u70b9\u5728 childs \u91cc\u9762\u662f\u987a\u5e8f\u8003\u524d\uff0c\u8fd9\u79cd\u89c4\u5219\u5728\u53ea\u8bbf\u95ee\u4e00\u6b21\u7684\u60c5\u51b5\u4e0b\uff0c\u9759\u6001\u8def\u7531\u4f1a\u5f3a\u53d8\u91cf\u6027\u80fd\u5f31\u3002<\/p>\n<p>\u800c httprouter \u4ee3\u7801\u771f\u5fc3\u4e11\uff0c\u51fd\u6570\u590d\u6742\u7684\u8d3c\u9ad8\uff0c\u8fd8\u8981\u6ca1\u6709\u5339\u914d\u4f18\u5148\u7ea7\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4276170\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : guonaihong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @eudore \u54c8\u54c8\uff0c\u540c\u611f\uff0chttprouter \u7684\u4ee3\u7801\u771f\u662f\u4e11\u7206\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u73b0\u4e00\u4e2a http router &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\/200033"}],"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=200033"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/200033\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200033"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}