mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-08 03:00:27 +08:00
优化代码
This commit is contained in:
@@ -7,9 +7,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
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/dbs"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/dbs"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"net"
|
"net"
|
||||||
@@ -64,7 +64,7 @@ func (this *SQLiteFileListDB) Open(dbPath string) error {
|
|||||||
|
|
||||||
// 动态调整Cache值
|
// 动态调整Cache值
|
||||||
var cacheSize = 512
|
var cacheSize = 512
|
||||||
var memoryGB = utils.SystemMemoryGB()
|
var memoryGB = memutils.SystemMemoryGB()
|
||||||
if memoryGB >= 1 {
|
if memoryGB >= 1 {
|
||||||
cacheSize = 256 * memoryGB
|
cacheSize = 256 * memoryGB
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
package caches
|
package caches
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
||||||
"math/big"
|
"math/big"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -46,7 +46,7 @@ func NewSQLiteFileListHashMap() *SQLiteFileListHashMap {
|
|||||||
|
|
||||||
func (this *SQLiteFileListHashMap) Load(db *SQLiteFileListDB) error {
|
func (this *SQLiteFileListHashMap) Load(db *SQLiteFileListDB) error {
|
||||||
// 如果系统内存过小,我们不缓存
|
// 如果系统内存过小,我们不缓存
|
||||||
if utils.SystemMemoryGB() < 3 {
|
if memutils.SystemMemoryGB() < 3 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
@@ -299,7 +299,7 @@ func (this *Manager) MaxSystemMemoryBytesPerStorage() int64 {
|
|||||||
count = 1
|
count = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
var resultBytes = int64(utils.SystemMemoryBytes()) / 3 / int64(count) // 1/3 of the system memory
|
var resultBytes = int64(memutils.SystemMemoryBytes()) / 3 / int64(count) // 1/3 of the system memory
|
||||||
if resultBytes < 1<<30 {
|
if resultBytes < 1<<30 {
|
||||||
resultBytes = 1 << 30
|
resultBytes = 1 << 30
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ package caches
|
|||||||
import (
|
import (
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -93,7 +93,7 @@ func NewMemoryFragmentPool() *MemoryFragmentPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *MemoryFragmentPool) init() {
|
func (this *MemoryFragmentPool) init() {
|
||||||
var capacity = int64(utils.SystemMemoryGB()) << 30 / 16
|
var capacity = int64(memutils.SystemMemoryGB()) << 30 / 16
|
||||||
if capacity > 256<<20 {
|
if capacity > 256<<20 {
|
||||||
this.isOk = true
|
this.isOk = true
|
||||||
this.capacity = capacity
|
this.capacity = capacity
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ package caches
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/linkedlist"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/linkedlist"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
@@ -43,7 +43,7 @@ func NewOpenFileCache(maxCount int) (*OpenFileCache, error) {
|
|||||||
maxCount: maxCount,
|
maxCount: maxCount,
|
||||||
poolMap: map[string]*OpenFilePool{},
|
poolMap: map[string]*OpenFilePool{},
|
||||||
poolList: linkedlist.NewList[*OpenFilePool](),
|
poolList: linkedlist.NewList[*OpenFilePool](),
|
||||||
capacitySize: (int64(utils.SystemMemoryGB()) << 30) / 16,
|
capacitySize: (int64(memutils.SystemMemoryGB()) << 30) / 16,
|
||||||
}
|
}
|
||||||
|
|
||||||
watcher, err := fsnotify.NewWatcher()
|
watcher, err := fsnotify.NewWatcher()
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"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"
|
||||||
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
|
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
setutils "github.com/TeaOSLab/EdgeNode/internal/utils/sets"
|
setutils "github.com/TeaOSLab/EdgeNode/internal/utils/sets"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
||||||
"github.com/cespare/xxhash"
|
"github.com/cespare/xxhash"
|
||||||
@@ -66,7 +67,7 @@ func NewMemoryStorage(policy *serverconfigs.HTTPCachePolicy, parentStorage Stora
|
|||||||
|
|
||||||
if parentStorage != nil {
|
if parentStorage != nil {
|
||||||
if queueSize <= 0 {
|
if queueSize <= 0 {
|
||||||
queueSize = utils.SystemMemoryGB() * 100_000
|
queueSize = memutils.SystemMemoryGB() * 100_000
|
||||||
}
|
}
|
||||||
|
|
||||||
dirtyChan = make(chan string, queueSize)
|
dirtyChan = make(chan string, queueSize)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ package compressions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxSize = utils.SystemMemoryGB() * 256
|
var maxSize = memutils.SystemMemoryGB() * 256
|
||||||
if maxSize == 0 {
|
if maxSize == 0 {
|
||||||
maxSize = 256
|
maxSize = 256
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ package compressions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxSize = utils.SystemMemoryGB() * 256
|
var maxSize = memutils.SystemMemoryGB() * 256
|
||||||
if maxSize == 0 {
|
if maxSize == 0 {
|
||||||
maxSize = 256
|
maxSize = 256
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ package compressions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxSize = utils.SystemMemoryGB() * 256
|
var maxSize = memutils.SystemMemoryGB() * 256
|
||||||
if maxSize == 0 {
|
if maxSize == 0 {
|
||||||
maxSize = 256
|
maxSize = 256
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ package compressions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxSize = utils.SystemMemoryGB() * 256
|
var maxSize = memutils.SystemMemoryGB() * 256
|
||||||
if maxSize == 0 {
|
if maxSize == 0 {
|
||||||
maxSize = 256
|
maxSize = 256
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ package compressions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/andybalholm/brotli"
|
"github.com/andybalholm/brotli"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
@@ -16,7 +16,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxSize = utils.SystemMemoryGB() * 256
|
var maxSize = memutils.SystemMemoryGB() * 256
|
||||||
if maxSize == 0 {
|
if maxSize == 0 {
|
||||||
maxSize = 256
|
maxSize = 256
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ package compressions
|
|||||||
import (
|
import (
|
||||||
"compress/flate"
|
"compress/flate"
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxSize = utils.SystemMemoryGB() * 256
|
var maxSize = memutils.SystemMemoryGB() * 256
|
||||||
if maxSize == 0 {
|
if maxSize == 0 {
|
||||||
maxSize = 256
|
maxSize = 256
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ package compressions
|
|||||||
import (
|
import (
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxSize = utils.SystemMemoryGB() * 256
|
var maxSize = memutils.SystemMemoryGB() * 256
|
||||||
if maxSize == 0 {
|
if maxSize == 0 {
|
||||||
maxSize = 256
|
maxSize = 256
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ package compressions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/klauspost/compress/zstd"
|
"github.com/klauspost/compress/zstd"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
@@ -16,7 +16,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxSize = utils.SystemMemoryGB() * 256
|
var maxSize = memutils.SystemMemoryGB() * 256
|
||||||
if maxSize == 0 {
|
if maxSize == 0 {
|
||||||
maxSize = 256
|
maxSize = 256
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -26,7 +27,7 @@ type HTTPAccessLogQueue struct {
|
|||||||
// NewHTTPAccessLogQueue 获取新对象
|
// NewHTTPAccessLogQueue 获取新对象
|
||||||
func NewHTTPAccessLogQueue() *HTTPAccessLogQueue {
|
func NewHTTPAccessLogQueue() *HTTPAccessLogQueue {
|
||||||
// 队列中最大的值,超出此数量的访问日志会被丢弃
|
// 队列中最大的值,超出此数量的访问日志会被丢弃
|
||||||
var maxSize = 2_000 * (1 + utils.SystemMemoryGB()/2)
|
var maxSize = 2_000 * (1 + memutils.SystemMemoryGB()/2)
|
||||||
if maxSize > 20_000 {
|
if maxSize > 20_000 {
|
||||||
maxSize = 20_000
|
maxSize = 20_000
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import (
|
|||||||
_ "github.com/TeaOSLab/EdgeNode/internal/utils/agents" // 引入Agent管理器
|
_ "github.com/TeaOSLab/EdgeNode/internal/utils/agents" // 引入Agent管理器
|
||||||
_ "github.com/TeaOSLab/EdgeNode/internal/utils/clock" // 触发时钟更新
|
_ "github.com/TeaOSLab/EdgeNode/internal/utils/clock" // 触发时钟更新
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/jsonutils"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/jsonutils"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/waf"
|
"github.com/TeaOSLab/EdgeNode/internal/waf"
|
||||||
"github.com/andybalholm/brotli"
|
"github.com/andybalholm/brotli"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -1123,7 +1124,7 @@ func (this *Node) tuneSystemParameters() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// vm
|
// vm
|
||||||
var systemMemory = utils.SystemMemoryGB()
|
var systemMemory = memutils.SystemMemoryGB()
|
||||||
if systemMemory >= 128 {
|
if systemMemory >= 128 {
|
||||||
systemParameters = append(systemParameters, []variable{
|
systemParameters = append(systemParameters, []variable{
|
||||||
{name: "vm.dirty_background_ratio", minValue: 40},
|
{name: "vm.dirty_background_ratio", minValue: 40},
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
syncutils "github.com/TeaOSLab/EdgeNode/internal/utils/sync"
|
syncutils "github.com/TeaOSLab/EdgeNode/internal/utils/sync"
|
||||||
"github.com/mssola/useragent"
|
"github.com/mssola/useragent"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -50,7 +50,7 @@ func NewUserAgentParser() *UserAgentParser {
|
|||||||
// 初始化
|
// 初始化
|
||||||
func (this *UserAgentParser) init() {
|
func (this *UserAgentParser) init() {
|
||||||
var maxCacheItems = 10_000
|
var maxCacheItems = 10_000
|
||||||
var systemMemory = utils.SystemMemoryGB()
|
var systemMemory = memutils.SystemMemoryGB()
|
||||||
if systemMemory >= 16 {
|
if systemMemory >= 16 {
|
||||||
maxCacheItems = 40_000
|
maxCacheItems = 40_000
|
||||||
} else if systemMemory >= 8 {
|
} else if systemMemory >= 8 {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package ttlcache
|
package ttlcache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ type Cache[T any] struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewBigCache[T any]() *Cache[T] {
|
func NewBigCache[T any]() *Cache[T] {
|
||||||
var delta = utils.SystemMemoryGB() / 2
|
var delta = memutils.SystemMemoryGB() / 2
|
||||||
if delta <= 0 {
|
if delta <= 0 {
|
||||||
delta = 1
|
delta = 1
|
||||||
}
|
}
|
||||||
@@ -36,7 +36,7 @@ func NewCache[T any](opt ...OptionInterface) *Cache[T] {
|
|||||||
var countPieces = 256
|
var countPieces = 256
|
||||||
var maxItems = 1_000_000
|
var maxItems = 1_000_000
|
||||||
|
|
||||||
var totalMemory = utils.SystemMemoryGB()
|
var totalMemory = memutils.SystemMemoryGB()
|
||||||
if totalMemory < 2 {
|
if totalMemory < 2 {
|
||||||
// 我们限制内存过小的服务能够使用的数量
|
// 我们限制内存过小的服务能够使用的数量
|
||||||
maxItems = 500_000
|
maxItems = 500_000
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package ttlcache
|
package ttlcache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
||||||
"github.com/iwind/TeaGo/assert"
|
"github.com/iwind/TeaGo/assert"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
@@ -60,7 +60,7 @@ func TestCache_Memory(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
var count = 1_000_000
|
var count = 1_000_000
|
||||||
if utils.SystemMemoryGB() > 4 {
|
if memutils.SystemMemoryGB() > 4 {
|
||||||
count = 20_000_000
|
count = 20_000_000
|
||||||
}
|
}
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var BytePool1k = NewBytePool(1024)
|
var BytePool1k = NewBytePool(1 << 10)
|
||||||
var BytePool4k = NewBytePool(4 * 1024)
|
var BytePool4k = NewBytePool(4 << 10)
|
||||||
var BytePool16k = NewBytePool(16 * 1024)
|
var BytePool16k = NewBytePool(16 << 10)
|
||||||
var BytePool32k = NewBytePool(32 * 1024)
|
var BytePool32k = NewBytePool(32 << 10)
|
||||||
|
|
||||||
// BytePool pool for get byte slice
|
// BytePool pool for get byte slice
|
||||||
type BytePool struct {
|
type BytePool struct {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ package cachehits
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -38,7 +38,7 @@ func NewStat(goodRatio uint64) *Stat {
|
|||||||
goodRatio = 5
|
goodRatio = 5
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxItems = utils.SystemMemoryGB() * 10_000
|
var maxItems = memutils.SystemMemoryGB() * 10_000
|
||||||
if maxItems <= 0 {
|
if maxItems <= 0 {
|
||||||
maxItems = 100_000
|
maxItems = 100_000
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
package counters
|
package counters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||||
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
syncutils "github.com/TeaOSLab/EdgeNode/internal/utils/sync"
|
syncutils "github.com/TeaOSLab/EdgeNode/internal/utils/sync"
|
||||||
"github.com/cespare/xxhash"
|
"github.com/cespare/xxhash"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -31,7 +31,7 @@ type Counter[T SupportedUIntType] struct {
|
|||||||
|
|
||||||
// NewCounter create new counter
|
// NewCounter create new counter
|
||||||
func NewCounter[T SupportedUIntType]() *Counter[T] {
|
func NewCounter[T SupportedUIntType]() *Counter[T] {
|
||||||
var count = utils.SystemMemoryGB() * 8
|
var count = memutils.SystemMemoryGB() * 8
|
||||||
if count < 8 {
|
if count < 8 {
|
||||||
count = 8
|
count = 8
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||||
"github.com/cockroachdb/pebble"
|
"github.com/cockroachdb/pebble"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"io"
|
"io"
|
||||||
@@ -90,7 +90,7 @@ func (this *Store) Open() error {
|
|||||||
Logger: NewLogger(),
|
Logger: NewLogger(),
|
||||||
}
|
}
|
||||||
|
|
||||||
var memoryMB = utils.SystemMemoryGB() * 1
|
var memoryMB = memutils.SystemMemoryGB() * 1
|
||||||
if memoryMB > 256 {
|
if memoryMB > 256 {
|
||||||
memoryMB = 256
|
memoryMB = 256
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
package utils
|
package memutils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||||
//go:build go1.19
|
//go:build go1.19
|
||||||
|
|
||||||
package utils
|
package memutils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||||
//go:build !go1.19
|
//go:build !go1.19
|
||||||
|
|
||||||
package utils
|
package memutils
|
||||||
|
|
||||||
// 设置软内存最大值
|
// 设置软内存最大值
|
||||||
func setMaxMemory(memoryGB int) {
|
func setMaxMemory(memoryGB int) {
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
package utils
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestSystemMemoryGB(t *testing.T) {
|
|
||||||
t.Log(SystemMemoryGB())
|
|
||||||
t.Log(SystemMemoryGB())
|
|
||||||
t.Log(SystemMemoryGB())
|
|
||||||
t.Log(SystemMemoryBytes())
|
|
||||||
t.Log(SystemMemoryBytes())
|
|
||||||
t.Log(SystemMemoryBytes()>>30, "GB")
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user