From 048c6f213b0d0ba3f3ea62ad68ccd99a33e3a335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Fri, 1 Apr 2022 16:18:15 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E7=BE=A4=E5=8F=AF=E4=BB=A5=E8=AE=BE?= =?UTF-8?q?=E7=BD=AEWebP=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request_cache.go | 52 ++++++++++++++++------------ internal/nodes/http_writer.go | 17 ++++++--- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/internal/nodes/http_request_cache.go b/internal/nodes/http_request_cache.go index a40338c..718deba 100644 --- a/internal/nodes/http_request_cache.go +++ b/internal/nodes/http_request_cache.go @@ -203,20 +203,35 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { webPIsEnabled = true } - // 检查压缩缓存 + // 检查WebP压缩缓存 + if webPIsEnabled && !isPartialRequest && !isHeadMethod && reader == nil { + if this.web.Compression != nil && this.web.Compression.IsOn { + _, encoding, ok := this.web.Compression.MatchAcceptEncoding(this.RawReq.Header.Get("Accept-Encoding")) + if ok { + reader, _ = storage.OpenReader(key+caches.SuffixWebP+caches.SuffixCompression+encoding, useStale, false) + if reader != nil { + tags = append(tags, "webp", encoding) + } + } + } + } + + // 检查WebP + if webPIsEnabled && !isPartialRequest && + !isHeadMethod && + reader == nil { + reader, _ = storage.OpenReader(key+caches.SuffixWebP, useStale, false) + if reader != nil { + this.writer.cacheReaderSuffix = caches.SuffixWebP + tags = append(tags, "webp") + } + } + + // 检查普通压缩缓存 if !isPartialRequest && !isHeadMethod && reader == nil { if this.web.Compression != nil && this.web.Compression.IsOn { _, encoding, ok := this.web.Compression.MatchAcceptEncoding(this.RawReq.Header.Get("Accept-Encoding")) if ok { - // 检查支持WebP的压缩缓存 - if webPIsEnabled { - reader, _ = storage.OpenReader(key+caches.SuffixWebP+caches.SuffixCompression+encoding, useStale, false) - if reader != nil { - tags = append(tags, "webp", encoding) - } - } - - // 检查普通压缩缓存 if reader == nil { reader, _ = storage.OpenReader(key+caches.SuffixCompression+encoding, useStale, false) if reader != nil { @@ -227,18 +242,6 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { } } - // 检查WebP - if !isPartialRequest && - !isHeadMethod && - reader == nil && - webPIsEnabled { - reader, _ = storage.OpenReader(key+caches.SuffixWebP, useStale, false) - if reader != nil { - this.writer.cacheReaderSuffix = caches.SuffixWebP - tags = append(tags, "webp") - } - } - // 检查正常的文件 var isPartialCache = false var partialRanges []rangeutils.Range @@ -530,7 +533,10 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { return true } } else { // 没有Range - var resp = &http.Response{Body: reader} + var resp = &http.Response{ + Body: reader, + ContentLength: reader.BodySize(), + } this.writer.Prepare(resp, fileSize, reader.Status(), false) this.writer.WriteHeader(reader.Status()) diff --git a/internal/nodes/http_writer.go b/internal/nodes/http_writer.go index aa9f818..cd6a7fe 100644 --- a/internal/nodes/http_writer.go +++ b/internal/nodes/http_writer.go @@ -6,13 +6,13 @@ import ( "bufio" "bytes" "errors" + "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeNode/internal/caches" "github.com/TeaOSLab/EdgeNode/internal/compressions" "github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils/readers" - "github.com/TeaOSLab/EdgeNode/internal/utils/sizes" "github.com/TeaOSLab/EdgeNode/internal/utils/writers" _ "github.com/biessek/golang-ico" "github.com/iwind/TeaGo/lists" @@ -444,18 +444,27 @@ func (this *HTTPWriter) PrepareWebP(resp *http.Response, size int64) { return } + // 集群配置 + var policy = sharedNodeConfig.FindWebPImagePolicyWithClusterId(this.req.ReqServer.ClusterId) + if policy == nil { + policy = nodeconfigs.DefaultWebPImagePolicy + } + if !policy.IsOn { + return + } + // 只有在开启了缓存之后,才会转换,防止占用的系统资源过高 - if this.req.cacheRef == nil { + if policy.RequireCache && this.req.cacheRef == nil { return } // 限制最小和最大尺寸 - // TODO 需要可以在集群里WebP选项里设置 // TODO 需要将reader修改为LimitReader if resp.ContentLength == 0 { return } - if resp.ContentLength > 128*sizes.M { + + if resp.ContentLength > 0 && (resp.ContentLength < policy.MinLengthBytes() || (policy.MaxLengthBytes() > 0 && resp.ContentLength > policy.MaxLengthBytes())) { return }