mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-09 03:50:27 +08:00
WebP转换限制为单线程,防止占用系统资源过高
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/caches"
|
"github.com/TeaOSLab/EdgeNode/internal/caches"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/compressions"
|
"github.com/TeaOSLab/EdgeNode/internal/compressions"
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||||
@@ -38,21 +37,9 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
var webpMaxBufferSize int64 = 1_000_000_000
|
var webpThreads int32
|
||||||
var webpTotalBufferSize int64 = 0
|
|
||||||
var webpIgnoreURLSet = setutils.NewFixedSet(131072)
|
var webpIgnoreURLSet = setutils.NewFixedSet(131072)
|
||||||
|
|
||||||
func init() {
|
|
||||||
if !teaconst.IsMain {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var systemMemory = utils.SystemMemoryGB() / 8
|
|
||||||
if systemMemory > 0 {
|
|
||||||
webpMaxBufferSize = int64(systemMemory) << 30
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HTTPWriter 响应Writer
|
// HTTPWriter 响应Writer
|
||||||
type HTTPWriter struct {
|
type HTTPWriter struct {
|
||||||
req *HTTPRequest
|
req *HTTPRequest
|
||||||
@@ -560,8 +547,8 @@ func (this *HTTPWriter) PrepareWebP(resp *http.Response, size int64) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查内存
|
// 检查当前是否正在转换
|
||||||
if atomic.LoadInt64(&webpTotalBufferSize) >= webpMaxBufferSize {
|
if atomic.LoadInt32(&webpThreads) == 1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1020,6 +1007,11 @@ 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)
|
||||||
|
defer func() {
|
||||||
|
atomic.StoreInt32(&webpThreads, 0)
|
||||||
|
}()
|
||||||
|
|
||||||
var webpCacheWriter caches.Writer
|
var webpCacheWriter caches.Writer
|
||||||
|
|
||||||
// 准备WebP Cache
|
// 准备WebP Cache
|
||||||
@@ -1100,12 +1092,6 @@ func (this *HTTPWriter) finishWebP() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var totalBytes = reader.TotalBytes()
|
|
||||||
atomic.AddInt64(&webpTotalBufferSize, totalBytes)
|
|
||||||
defer func() {
|
|
||||||
atomic.AddInt64(&webpTotalBufferSize, -totalBytes)
|
|
||||||
}()
|
|
||||||
|
|
||||||
var f = types.Float32(this.req.web.WebP.Quality)
|
var f = types.Float32(this.req.web.WebP.Quality)
|
||||||
if f > 100 {
|
if f > 100 {
|
||||||
f = 100
|
f = 100
|
||||||
|
|||||||
Reference in New Issue
Block a user