1 回答

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