简体   繁体   English

为什么这不是HTTP服务器在Chrome 47中为每个请求生成一个goroutine?

[英]Why isn't this go HTTP server spawning a goroutine per request in Chrome 47?

Previously titled: How to spawn goroutine per HTTP request? 以前标题为: 如何根据HTTP请求生成goroutine?

The code below is a simple HTTP server that echos the path back to the requester. 下面的代码是一个简单的HTTP服务器,它将路径回送给请求者。 I used it to test if the ListenAndServe method of the net/http package by default fires a goroutine to handle each request; 我用它来测试net/http包的ListenAndServe方法是否默认触发goroutine来处理每个请求; which I discovered it doesn't. 我发现它没有。 If I make three requests at the same time, the first takes 10 seconds to return, the second 20 (10 seconds after the first returns), and the third 30 seconds. 如果我同时发出三个请求,则第一个请求返回10秒,第二个20秒(第一个返回后10秒),第三个30秒。

package main

import (
  "fmt"
  "net/http"
  "time"
)

func handler(w http.ResponseWriter, r *http.Request) {
  time.Sleep(10000 * time.Millisecond)
  fmt.Fprint(w, r.URL.Path)
}

func main() {
  http.HandleFunc("/", handler)
  http.ListenAndServe(":8080", nil)
}

Basically what I want is the main goroutine to listen for an HTTP connection, then pass the reading of the request and the response to a handler spawned in another goroutine. 基本上我想要的是监听HTTP连接的主要goroutine,然后将请求的读取和响应传递给在另一个goroutine中生成的处理程序。

Could someone point me in the right direction for accomplishing this? 有人能指出我正确的方向来实现这个目标吗? Preferably using the net/http package in go. 最好在go中使用net/http包。

UPDATE 12/21/15 08:46 AM MST 更新12/21/15 08:46 AM MST

I conducted the same exact test you did using my web browser (chrome 47), five requests at the root of localhost:8080, and the results were roughly: 我使用我的网络浏览器(chrome 47)执行了相同的测试,在localhost的根目录下执行了五个请求:8080,结果大致如下:

1st: 10 seconds
2nd: 20 seconds
3rd: 30 seconds
4th: 36 seconds     
5th: 38 seconds

So, hopefully, the folks who would otherwise down-vote my question would understand my confusion and why I made the assumption I did. 所以,希望那些否则会对我的问题进行投票的人会理解我的困惑以及为什么我做了我的假设。 I have no idea why I got the results I did on the 4th and 5th request. 我不知道为什么我得到了我在第4和第5次请求时所做的结果。

I ran the same test using curl and had the same results as @tomasz. 我使用curl运行相同的测试,结果与@tomasz相同。

I'm using go1.2.1 . 我正在使用go1.2.1

UPDATE 12/21/15 02:08 PM MST 更新12/21/15 02:08 PM MST

Per @tomasz's suggestion below, I retitled this question from "How to spawn goroutine per HTTP request" to "Why isn't this go HTTP server spawning a goroutine per request in Chrome 47?" 根据Per tomasz的建议,我将这个问题从“如何根据每个HTTP请求生成goroutine”改为“为什么这不是用于在Chrome 47中为每个请求生成goroutine的HTTP服务器?”

All is good, your handler is run in a separate routine for each request. 一切都很好,您的处理程序在每个请求的单独例程中运行。 Take a look at the source code of http.Server.Serve method. 看一下http.Server.Serve方法的源代码。 The last line in accepting loop says: 接受循环的最后一行说:

go c.serve()

The problem is probably with your testing. 问题可能在于您的测试。 If you check the behaviour through the multiple tabs in browser the requests for matching URLs are probably queued instead of running them simultaneously (ie your client is not using "routines", not the server). 如果通过浏览器中的多个选项卡检查行为,则匹配URL的请求可能会排队,而不是同时运行它们(即您的客户端不使用“例程”,而不是服务器)。

Try two different browsers or just use command line and, say, curl to test requests in parallel. 尝试使用两种不同的浏览器或只使用命令行,例如curl来并行测试请求。 For example (with some help from bash): 例如(在bash的帮助下):

$ for i in {1..5}; do time curl localhost:8080 &; done
# after ignoring some mess...
curl localhost:8080  0.00s user 0.00s system 0% cpu 10.013 total
curl localhost:8080  0.00s user 0.00s system 0% cpu 10.014 total
curl localhost:8080  0.00s user 0.00s system 0% cpu 10.012 total
curl localhost:8080  0.00s user 0.00s system 0% cpu 10.019 total

You server works like a charm. 你的服务器就像一个魅力。

Update 更新

I can confirm this behaviour on Chrome 47, but also noticed you can open multiple tabs with, say, http://localhost:8080/test1 , http://localhost:8080/test2 etc. and you will get expected results. 我可以在Chrome 47上确认此行为,但也注意到您可以打开多个标签,例如http://localhost:8080/test1http://localhost:8080/test2等,您将获得预期的结果。 That indicates there's indeed some queuing mechanism in Chrome for matching URLs. 这表明Chrome中确实存在一些用于匹配URL的排队机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM