我正在通过 singleflight
代理一堆 http GET 调用。但是返回的响应只能被第一个请求看到。
我在测试中也注意到了一个问题。如果第一个请求超时,响应将丢失。
假设 r1,r2,r3
是按顺序发出的请求。它们都分组在一个 groupKey
中。如果 r1
超时,r2
和 r3
将等待直到共享 HTTP 调用返回或直到它们自己的超时。
代理代码(贷记 here )
// add auth to the requst and proxy to target host
var serveReverseProxy = func(target string, res http.ResponseWriter, req *http.Request) {
log.Println("new request!")
requestURL, _ := url.Parse(target)
proxy := httputil.NewSingleHostReverseProxy(requestURL)
req1, _ := http.NewRequest(req.Method, req.RequestURI, req.Body)
for k, v := range req.Header {
for _, vv := range v {
req1.Header.Add(k, vv)
}
}
req1.Header.Set("Authorization", "Bearer "+"some token")
req1.Host = requestURL.Host
proxy.ServeHTTP(res, req1)
}
var requestGroup singleflight.Group
mockBackend := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
groupKey := req.Host + req.RequestURI
name := req.Header.Get("From")
ch := requestGroup.DoChan(groupKey, func() (interface{}, error) {
//increase key retention to 20s to make sure r1,r2,r3 are all in one group
go func() {
time.Sleep(20 * time.Second)
requestGroup.Forget(groupKey)
log.Println("Key deleted :", groupKey)
}()
// proxy to some host and expect the result to be written in res
serveReverseProxy("https://somehost.com", res, req)
return nil, nil
})
timeout := time.After(15 * time.Second)
var result singleflight.Result
select {
case <-timeout: // Timeout elapsed, send a timeout message (504)
log.Println(name, " timed out")
http.Error(res, "request timed out", http.StatusGatewayTimeout)
return
case result = <-ch: // Received result from channel
}
if result.Err != nil {
http.Error(res, result.Err.Error(), http.StatusInternalServerError)
return
}
if result.Shared {
log.Println(name, " is shared")
} else {
log.Println(name, " not shared")
}
}))
我希望 r2
,r3
要么
- 至少能看到自己reponseWriter的结果
- 与
r1
一起超时
最佳答案
https://github.com/golang/net/blob/master/http2/h2demo/h2demo.go#L181-L219 这有效。结果我需要在 singleFlight.Group.Do 中返回处理程序而不是响应。 不知道为什么
关于go - 如何使用SingleFlight分享下载的大文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55541067/