缓存条件增加是否允许异步读取源站选项

This commit is contained in:
GoEdgeLab
2023-07-31 15:49:04 +08:00
parent 7adbb451b0
commit 7f594a47c6
3 changed files with 125 additions and 2 deletions

View File

@@ -524,7 +524,28 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId
}
}
} else {
_, err = io.CopyBuffer(this.writer, resp.Body, buf)
if this.cacheRef != nil &&
this.cacheRef.EnableReadingOriginAsync &&
resp.ContentLength > 0 &&
resp.ContentLength < (128<<20) { // TODO configure max content-length in cache policy OR CacheRef
var requestIsCanceled = false
for {
n, readErr := resp.Body.Read(buf)
if n > 0 && !requestIsCanceled {
_, err = this.writer.Write(buf[:n])
if err != nil {
requestIsCanceled = true
}
}
if readErr != nil {
err = readErr
break
}
}
} else {
_, err = io.CopyBuffer(this.writer, resp.Body, buf)
}
}
pool.Put(buf)