修复源站重试时可能产生的file is writing错误

This commit is contained in:
刘祥超
2023-03-12 16:09:06 +08:00
parent 50f3ad641c
commit 28e7664eb7

View File

@@ -104,6 +104,18 @@ func NewHTTPWriter(req *HTTPRequest, httpResponseWriter http.ResponseWriter) *HT
// Prepare 准备输出
func (this *HTTPWriter) Prepare(resp *http.Response, size int64, status int, enableCache bool) (delayHeaders bool) {
// 清理以前数据,防止重试时发生异常错误
if this.compressionCacheWriter != nil {
_ = this.compressionCacheWriter.Discard()
this.compressionCacheWriter = nil
}
if this.cacheWriter != nil {
_ = this.cacheWriter.Discard()
this.cacheWriter = nil
}
// 新的请求相关数据
this.size = size
this.statusCode = status
@@ -334,6 +346,9 @@ func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) {
}
return
}
if this.cacheWriter != nil {
_ = this.cacheWriter.Discard()
}
this.cacheWriter = cacheWriter
if this.isPartial {
@@ -696,6 +711,9 @@ func (this *HTTPWriter) PrepareCompression(resp *http.Response, size int64) {
}
if compressionCacheWriter != nil {
if this.compressionCacheWriter != nil {
_ = this.compressionCacheWriter.Close()
}
this.compressionCacheWriter = compressionCacheWriter
var teeWriter = writers.NewTeeWriterCloser(this.writer, compressionCacheWriter)
teeWriter.OnFail(func(err error) {