1 回答

TA貢獻1812條經(jīng)驗 獲得超5個贊
一切都很好,您的處理程序針對每個請求在單獨的例程中運行??匆幌耯ttp.Server.Serve方法的源代碼。接受循環(huán)的最后一行說:
go c.serve()
問題可能出在您的測試上。如果您通過瀏覽器中的多個選項卡檢查行為,則匹配 URL 的請求可能會排隊,而不是同時運行(即您的客戶端沒有使用“例程”,而不是服務器)。
嘗試兩種不同的瀏覽器,或者只使用命令行,比如curl并行測試請求。例如(在 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
你的服務器就像一個魅力。
更新
我可以在 Chrome 47 上確認這種行為,但也注意到你可以打開多個標簽,比如http://localhost:8080/test1,http://localhost:8080/test2等等,你會得到預期的結(jié)果。這表明 Chrome 中確實有一些用于匹配 URL 的排隊機制。
- 1 回答
- 0 關注
- 178 瀏覽
添加回答
舉報