mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-08 03:00:27 +08:00
根据系统环境调整WebP转换线程数
This commit is contained in:
@@ -33,12 +33,21 @@ import (
|
|||||||
"net/textproto"
|
"net/textproto"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
var webpThreads int32
|
var webPThreads int32
|
||||||
var webpIgnoreURLSet = setutils.NewFixedSet(131072)
|
var webPMaxThreads int32 = 1
|
||||||
|
var webPIgnoreURLSet = setutils.NewFixedSet(131072)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
webPMaxThreads = int32(runtime.NumCPU() / 4)
|
||||||
|
if webPMaxThreads < 1 {
|
||||||
|
webPMaxThreads = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// HTTPWriter 响应Writer
|
// HTTPWriter 响应Writer
|
||||||
type HTTPWriter struct {
|
type HTTPWriter struct {
|
||||||
@@ -539,7 +548,7 @@ func (this *HTTPWriter) PrepareWebP(resp *http.Response, size int64) {
|
|||||||
this.req.web.WebP.MatchResponse(contentType, size, filepath.Ext(this.req.Path()), this.req.Format) &&
|
this.req.web.WebP.MatchResponse(contentType, size, filepath.Ext(this.req.Path()), this.req.Format) &&
|
||||||
this.req.web.WebP.MatchAccept(this.req.requestHeader("Accept")) {
|
this.req.web.WebP.MatchAccept(this.req.requestHeader("Accept")) {
|
||||||
// 检查是否已经因为尺寸过大而忽略
|
// 检查是否已经因为尺寸过大而忽略
|
||||||
if webpIgnoreURLSet.Has(this.req.URL()) {
|
if webPIgnoreURLSet.Has(this.req.URL()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -550,7 +559,7 @@ func (this *HTTPWriter) PrepareWebP(resp *http.Response, size int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检查当前是否正在转换
|
// 检查当前是否正在转换
|
||||||
if atomic.LoadInt32(&webpThreads) == 1 {
|
if atomic.LoadInt32(&webPThreads) >= webPMaxThreads {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1009,9 +1018,9 @@ func (this *HTTPWriter) calculateStaleLife() int {
|
|||||||
func (this *HTTPWriter) finishWebP() {
|
func (this *HTTPWriter) finishWebP() {
|
||||||
// 处理WebP
|
// 处理WebP
|
||||||
if this.webpIsEncoding {
|
if this.webpIsEncoding {
|
||||||
atomic.StoreInt32(&webpThreads, 1)
|
atomic.AddInt32(&webPThreads, 1)
|
||||||
defer func() {
|
defer func() {
|
||||||
atomic.StoreInt32(&webpThreads, 0)
|
atomic.AddInt32(&webPThreads, -1)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var webpCacheWriter caches.Writer
|
var webpCacheWriter caches.Writer
|
||||||
@@ -1074,7 +1083,7 @@ func (this *HTTPWriter) finishWebP() {
|
|||||||
if isGif {
|
if isGif {
|
||||||
gifImage, err = gif.DecodeAll(reader)
|
gifImage, err = gif.DecodeAll(reader)
|
||||||
if gifImage != nil && (gifImage.Config.Width > gowebp.WebPMaxDimension || gifImage.Config.Height > gowebp.WebPMaxDimension) {
|
if gifImage != nil && (gifImage.Config.Width > gowebp.WebPMaxDimension || gifImage.Config.Height > gowebp.WebPMaxDimension) {
|
||||||
webpIgnoreURLSet.Push(this.req.URL())
|
webPIgnoreURLSet.Push(this.req.URL())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -1082,7 +1091,7 @@ func (this *HTTPWriter) finishWebP() {
|
|||||||
if imageData != nil {
|
if imageData != nil {
|
||||||
var bound = imageData.Bounds()
|
var bound = imageData.Bounds()
|
||||||
if bound.Max.X > gowebp.WebPMaxDimension || bound.Max.Y > gowebp.WebPMaxDimension {
|
if bound.Max.X > gowebp.WebPMaxDimension || bound.Max.Y > gowebp.WebPMaxDimension {
|
||||||
webpIgnoreURLSet.Push(this.req.URL())
|
webPIgnoreURLSet.Push(this.req.URL())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1090,7 +1099,7 @@ func (this *HTTPWriter) finishWebP() {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 发生了错误终止处理
|
// 发生了错误终止处理
|
||||||
webpIgnoreURLSet.Push(this.req.URL())
|
webPIgnoreURLSet.Push(this.req.URL())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user