[英]why does go mux block with time.sleep when called from same browser?
Here is my code:这是我的代码:
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func gotest(w http.ResponseWriter, req *http.Request) {
log.Printf("starting ... Go Test()")
for i := 0; i < 10; i++ {
time.Sleep(2 * time.Second)
log.Printf("Go Test %s", i)
fmt.Fprintf(w, "Go Test %s", i)
}
}
func main() {
http.HandleFunc("/gotest", gotest)
http.ListenAndServe(":3016", nil)
}
Running http://localhost:3016/gotest multiple windows using the same browser results in the gotest function running serial (not concurrent)使用同一浏览器运行http://localhost:3016/gotest多个窗口会导致 gotest 函数运行串行(非并发)
2020/02/22 01:24:26 starting ... Go Test()
2020/02/22 01:24:28 Go Test %!s(int=0)
2020/02/22 01:24:30 Go Test %!s(int=1)
2020/02/22 01:24:32 Go Test %!s(int=2)
2020/02/22 01:24:34 Go Test %!s(int=3)
2020/02/22 01:24:36 Go Test %!s(int=4)
2020/02/22 01:24:38 Go Test %!s(int=5)
2020/02/22 01:24:40 Go Test %!s(int=6)
2020/02/22 01:24:42 Go Test %!s(int=7)
2020/02/22 01:24:44 Go Test %!s(int=8)
2020/02/22 01:24:46 Go Test %!s(int=9)
2020/02/22 01:24:46 starting ... Go Test()
2020/02/22 01:24:48 Go Test %!s(int=0)
2020/02/22 01:24:50 Go Test %!s(int=1)
2020/02/22 01:24:52 Go Test %!s(int=2)
...
However, running http://localhost:3016/gotest in different browsers or via curl on the command line will result in concurrent execution:但是,在不同的浏览器中运行http://localhost:3016/gotest或者在命令行中通过 curl 会导致并发执行:
2020/02/22 01:28:55 starting ... Go Test()
2020/02/22 01:28:57 Go Test %!s(int=0)
2020/02/22 01:28:58 starting ... Go Test()
2020/02/22 01:28:59 Go Test %!s(int=1)
2020/02/22 01:29:00 Go Test %!s(int=0)
2020/02/22 01:29:01 Go Test %!s(int=2)
2020/02/22 01:29:02 Go Test %!s(int=1)
2020/02/22 01:29:03 Go Test %!s(int=3)
2020/02/22 01:29:04 Go Test %!s(int=2)
2020/02/22 01:29:05 Go Test %!s(int=4)
...
Why would it matter if the url is called from different windows or tabs of the same browser vs called from different browsers?如果从同一浏览器的不同窗口或选项卡调用 url 与从不同浏览器调用,为什么会很重要?
this is indeed a browser issue.这确实是浏览器问题。 Changing the url to include something unique will get the browser to stop "blocking" itself.更改 url 以包含一些独特的内容将使浏览器停止“阻止”自身。
func gotest(w http.ResponseWriter, req *http.Request) {
log.Printf("starting ... Go Test()")
key := req.URL.Query()["key"]
for i := 0; i < 10; i++ {
time.Sleep(2 * time.Second)
log.Printf("Go Test %s %s", key, i)
fmt.Fprintf(w, "Go Test %s %s", key, i)
}
}
and running:并运行:
http://localhost:3016/gotest?key=A
http://localhost:3016/gotest?key=B
will result in concurrency:将导致并发:
2020/02/22 09:09:17 starting ... Go Test()
2020/02/22 09:09:19 Go Test [A] %!s(int=0)
2020/02/22 09:09:21 Go Test [A] %!s(int=1)
2020/02/22 09:09:22 starting ... Go Test()
2020/02/22 09:09:23 Go Test [A] %!s(int=2)
2020/02/22 09:09:24 Go Test [B] %!s(int=0)
2020/02/22 09:09:25 Go Test [A] %!s(int=3)
2020/02/22 09:09:26 Go Test [B] %!s(int=1)
This seems to be a pretty standard behavior since both Google Chrome and Firefox both do this.这似乎是一种非常标准的行为,因为 Google Chrome 和 Firefox 都这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.