Is gorilla mux router or the http package concurrent?

xuanbao · · 496 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am skimming through the source for gorilla and http in github right now and I am having a hard time finding where exactly is the currency happening.</p> <p>For example</p> <pre><code>r := mux.NewRouter() r.HandleFunc(&#34;/products/{key}&#34;, ProductHandler) r.HandleFunc(&#34;/articles/{category}/&#34;, ArticlesCategoryHandler) r.HandleFunc(&#34;/articles/{category}/{id:[0-9]+}&#34;, ArticleHandler) http.Handle(&#34;/&#34;, r) </code></pre> <p>Do we know if each api request correspond to 1 goroutine?</p> <p>Some pointer would be helpful</p> <p>Thanks</p> <hr/>**评论:**<br/><br/>alexcrownus: <pre><p>Yes it is. Each handler is scheduled to run in a goroutine. Check net/http source code in the stdlib to confirm. Gorilla is just a wrapper around the stdlib net/http.</p></pre>adelowo: <pre><p>Yes. </p> <p>Gorilla mux is just a multipleer. All it does is map routes (and dispatch them) to handlers. It still uses <code>http.ListenAndServe</code> (since you register the mux with <code>net/http</code>) which starts a new goroutine on every request.</p></pre>lstokeworth: <pre><p>The net/http server starts a goroutine per connection <a href="https://github.com/golang/go/blob/c8b889cc4824f4dbd64a51a3f7b5b6dce4b87ed2/src/net/http/server.go#L2705" rel="nofollow">on this line</a>. </p> <p>The Gorilla mux supports concurrent calls to its SeveHTTP method as should any handler registered with the Gorilla mux. The Gorilla mux does not add any concurrency of its own.</p></pre>aboukirev: <pre><p>To add to what others said. Typically you build all the routes before you start listening on the port. There is no telling what happens if you try to add a route when server is already running, i.e. that mux is not fully concurrent: neither Router, Route, matchers, etc. use any synchronization primitives. It heavily relies on net/http concurrency. </p></pre>mcouturier: <pre><p>You are already in a new goroutine when the router is choosing its route.</p> <p>The std lib accepts only one handler for an http server. It starts a new goroutine on each request and hands off the work to that handler.</p> <p>That handler often happens to be a router like gorilla mux.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

496 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传