简体   繁体   English

为什么从同一浏览器调用时 go mux 会阻塞 time.sleep?

[英]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.

相关问题 为什么apache不会同时处理来自同一浏览器的多个请求 - Why does apache not process multiple requests from the same browser simultaneously Docker 容器连接中的服务器被拒绝,我应该将 time.Sleep(100 * time.Millisecond) 添加到我的测试中吗? - Server in Docker container connection refused, should I add time.Sleep(100 * time.Millisecond) to my tests? 为什么浏览器在刷新时会为同一页面发送两个请求? - Why does a browser send two requests for the same page when it's refreshed? Go Gorilla Mux没有按预期路由 - Go Gorilla Mux not routing as expected 带有静态文件的Gorilla Mux子路由器 - Go Gorilla mux subrouter with static files 不使用时,node.js似乎进入睡眠模式 - node.js seems to go into sleep mode when it's not in use Java 客户端 - Go Http Mux Server - go 方法中的参数为空 - Java Client - Go Http Mux Server - Parameter empty on go method 为什么 document.cookie 存在,DOM 何时在浏览器中使用它? - Why does document.cookie exist and when is it used by DOM in a browser? 为什么getAllResponseHeaders()输出与浏览器看到的不同? - Why does getAllResponseHeaders() output differ from what the browser sees? 为什么浏览器将cookie附加到源自另一个来源的请求? - Why does a browser attach cookies to requests originating from another origin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM