2 回答
TA贡献1993条经验 获得超5个赞
服务器在serveHandler返回时关闭响应。
一种解决方法是阻塞serveHandler直到请求被处理。在以下代码中,worker 关闭done以表示请求已完成。处理程序等待done关闭。
type clientRequest struct {
r *http.Request
w http.ResponseWriter
done chan struct{} // <-- add this line
}
func serveHandler(w http.ResponseWriter, r *http.Request) {
var newRequest = new(clientRequest)
newRequest.r = r
newRequest.w = w
newRequest.done = make(chan struct{})
reqChannel <- newRequest // send the new request to the request channel
fmt.Printf("Sent request to reqChannel for URL: %q\n", html.EscapeString(r.URL.Path))
<-newRequest.done // wait for worker goroutine to complete
}
func processRequest(instanceNbr int) {
fmt.Printf("processRequest started for instance #%d\n", instanceNbr)
for theRequest := range reqChannel { // receive requests from the channel until it is closed
fmt.Printf("Got request from reqChannel for URL: %q\n", html.EscapeString(theRequest.r.URL.Path))
fmt.Fprintf(theRequest.w, "processRequest instance #%d: URL is %q", instanceNbr, html.EscapeString(theRequest.r.URL.Path))
if f, ok := theRequest.w.(http.Flusher); ok {
f.Flush()
}
close(theRequest.done) // signal handler that request is complete
}
}
如果目标是限制活动处理程序的数量,那么您可以使用通道作为计数信号量来限制活动处理程序 goroutine 的数量:
var reqChannel = make(chan struct{}, MaxRequests)
func serveHandler(w http.ResponseWriter, r *http.Request) {
reqChannel <- struct{}{}
// handle the request
<-reqChannel
}
请注意,服务器在每个连接的 goroutine 中运行处理程序。
更简单的是只编写一个处理程序。大多数服务器不需要限制请求处理程序的并发性。
TA贡献1817条经验 获得超6个赞
您的答案在net/http 代码的这一部分:
// HTTP cannot have multiple simultaneous active requests.[*]
// Until the server replies to this request, it can't read another,
// so we might as well run the handler in this goroutine.
// [*] Not strictly true: HTTP pipelining. We could let them all process
// in parallel even if their responses need to be serialized.
serverHandler{c.server}.ServeHTTP(w, w.req)
if c.hijacked() {
return
}
w.finishRequest()
后ServeHTTP返回时,请求完成。
所以你有几个解决方案:
放弃你的工人模式并完成工作 serveHandler
在完成之前等待请求被完全处理serveHandler,如下所示:
(在我的本地测试)
type clientRequest struct {
r *http.Request
w http.ResponseWriter
done chan struct{}
}
func serveHandler(w http.ResponseWriter, r *http.Request) {
var newRequest = new(clientRequest)
newRequest.r = r
newRequest.w = w
newRequest.done = make(chan struct{})
reqChannel <- newRequest // send the new request to the request channel
fmt.Printf("Sent request to reqChannel for URL: %q\n", html.EscapeString(r.URL.Path))
<-newRequest.done // wait for the worker to finish
}
func processRequest(instanceNbr int) {
fmt.Printf("processRequest started for instance #%d\n", instanceNbr)
for theRequest := range reqChannel { // receive requests from the channel until it is closed
fmt.Printf("Got request from reqChannel for URL: %q\n", html.EscapeString(theRequest.r.URL.Path))
// xxx this isn't working:
fmt.Fprintf(theRequest.w, "processRequest instance #%d: URL is %q", instanceNbr, html.EscapeString(theRequest.r.URL.Path))
if f, ok := theRequest.w.(http.Flusher); ok {
f.Flush()
}
theRequest.done <- struct{}{}
}
}
- 2 回答
- 0 关注
- 247 浏览
添加回答
举报