doubi6898 2018-01-11 23:43
浏览 144
已采纳

ListenAndServe:是否可以同时允许多个连接?

I made a small web server using gorilla/mux.

I took the small example at the end of the README file in their Github page.

r := mux.NewRouter()
r.HandleFunc("/", listUrlsHandler)
r.HandleFunc("/foo", fooHandler)
http.Handle("/", r)

err := http.ListenAndServe(":9090", nil) // set listen port
if err != nil {
    log.Fatal("ListenAndServe: ", err)
}

To emulate a long processing I added a timer in the callback function.

func fooHandler(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    fmt.Fprintln(w, "foo is downloading...")
    log.Println("foo is downloading...")
    time.Sleep(15*time.Second)
}

I run 2x /foo at the same time:

2018/01/12 09:15:03 foo is downloading...
2018/01/12 09:15:18 foo is downloading...
  • Only one is processing, the second one is not called. Once the first one is returned (15sec later), the second one starts.
  • Why? I want them to be processed in parallel...
  • This is deal-breaker, that means clients cannot access to the same page at the same moment.

How can I reply to the client and process later? Adding a goroutine in the handler?

func fooHandler(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    fmt.Fprintln(w, "foo is downloading...")
    log.Println("foo is downloading...")
    go time.Sleep(15*time.Second) // HERE <------------
    // go myCustomFunc()          // HERE <------------
}

I had an idea of how it's working but obviously I am totally wrong. I thought the router created a goroutine at each call.

Could you tell me what's the best practice to do what I want ?

I found the problem using tcp dump:

The second request opens a TCP connection that gets closed 150ms later (FIN from client). Then the second request is processed by the first TCP connection opened by the first request after the first request is done.

Might be Firefox's behavior that close the TCP connection and use the previous connection.

  • 写回答

1条回答 默认 最新

  • dsf5632 2018-01-11 23:57
    关注

    If you add a log call to your handler you can see when responses are being processed:

    func downloadFileHandler(w http.ResponseWriter, r *http.Request) {
       vars := mux.Vars(r)
       w.WriteHeader(http.StatusOK)
       fmt.Fprintf(w, "%s is downloading...
    ", vars["namefile"])
       log.Printf("%s is downloading...
    ", vars["namefile"])
       time.Sleep(15*time.Second)
    }
    

    Eg:

    2018/01/12 10:52:42 foo is downloading...
    2018/01/12 10:52:44 bar is downloading...
    

    Both calls are being processed synchronously due to Gorilla Mux using Go's underlying go routines but the 15-second time.Sleep is preventing you from seeing the results.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?