优化代码

This commit is contained in:
刘祥超
2022-08-07 11:12:29 +08:00
parent 4096f11909
commit 10665c0f37
7 changed files with 87 additions and 126 deletions

View File

@@ -180,14 +180,14 @@ var httpRequestTimestamp int64
var httpRequestId int32 = 1_000_000
func httpRequestNextId() string {
var unixTime = utils.UnixTimeMilli()
unixTime, unixTimeString := utils.UnixTimeMilliString()
if unixTime > httpRequestTimestamp {
atomic.StoreInt32(&httpRequestId, 1_000_000)
httpRequestTimestamp = unixTime
}
// timestamp + requestId + nodeId
return strconv.FormatInt(unixTime, 10) + teaconst.NodeIdString + strconv.Itoa(int(atomic.AddInt32(&httpRequestId, 1)))
return unixTimeString + teaconst.NodeIdString + strconv.Itoa(int(atomic.AddInt32(&httpRequestId, 1)))
}
// 检查是否可以接受某个编码

View File

@@ -106,7 +106,7 @@ func (this *TrafficStatManager) Add(serverId int64, domain string, bytes int64,
this.totalRequests++
var timestamp = utils.FloorUnixTime(300)
key := strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10)
var key = strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10)
this.locker.Lock()
// 总的流量

View File

@@ -1,108 +1,46 @@
package utils
import (
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/iwind/TeaGo/Tea"
"time"
"sync"
)
var BytePool1k = NewBytePool(20480, 1024)
var BytePool4k = NewBytePool(20480, 4*1024)
var BytePool16k = NewBytePool(40960, 16*1024)
var BytePool32k = NewBytePool(20480, 32*1024)
var BytePool1k = NewBytePool(1024)
var BytePool4k = NewBytePool(4 * 1024)
var BytePool16k = NewBytePool(16 * 1024)
var BytePool32k = NewBytePool(32 * 1024)
// BytePool pool for get byte slice
type BytePool struct {
c chan []byte
maxSize int
length int
hasNew bool
rawPool *sync.Pool
}
// NewBytePool 创建新对象
func NewBytePool(maxSize, length int) *BytePool {
if maxSize <= 0 {
maxSize = 1024
func NewBytePool(length int) *BytePool {
if length < 0 {
length = 1024
}
if length <= 0 {
length = 128
return &BytePool{
length: length,
rawPool: &sync.Pool{
New: func() any {
return make([]byte, length)
},
},
}
var pool = &BytePool{
c: make(chan []byte, maxSize),
maxSize: maxSize,
length: length,
}
pool.init()
return pool
}
// 初始化
func (this *BytePool) init() {
var ticker = time.NewTicker(2 * time.Minute)
if Tea.IsTesting() {
ticker = time.NewTicker(5 * time.Second)
}
goman.New(func() {
for range ticker.C {
if this.hasNew {
this.hasNew = false
continue
}
this.Purge()
}
})
}
// Get 获取一个新的byte slice
func (this *BytePool) Get() (b []byte) {
select {
case b = <-this.c:
default:
b = make([]byte, this.length)
this.hasNew = true
}
return
func (this *BytePool) Get() []byte {
return this.rawPool.Get().([]byte)
}
// Put 放回一个使用过的byte slice
func (this *BytePool) Put(b []byte) {
if cap(b) != this.length {
return
}
select {
case this.c <- b:
default:
// 已达最大容量,则抛弃
}
this.rawPool.Put(b)
}
// Length 单个字节slice长度
func (this *BytePool) Length() int {
return this.length
}
// Size 当前的数量
func (this *BytePool) Size() int {
return len(this.c)
}
// Purge 清理
func (this *BytePool) Purge() {
// 1%
var count = len(this.c) / 100
if count == 0 {
return
}
Loop:
for i := 0; i < count; i++ {
select {
case <-this.c:
default:
break Loop
}
}
}

View File

@@ -1,37 +1,16 @@
package utils
import (
"github.com/iwind/TeaGo/assert"
"runtime"
"sync"
"testing"
)
func TestNewBytePool(t *testing.T) {
a := assert.NewAssertion(t)
pool := NewBytePool(5, 8)
buf := pool.Get()
a.IsTrue(len(buf) == 8)
a.IsTrue(len(pool.c) == 0)
pool.Put(buf)
a.IsTrue(len(pool.c) == 1)
pool.Get()
a.IsTrue(len(pool.c) == 0)
for i := 0; i < 10; i++ {
pool.Put(buf)
}
t.Log(len(pool.c))
a.IsTrue(len(pool.c) == 5)
}
func TestBytePool_Memory(t *testing.T) {
var stat1 = &runtime.MemStats{}
runtime.ReadMemStats(stat1)
var pool = NewBytePool(20480, 32*1024)
var pool = NewBytePool(32 * 1024)
for i := 0; i < 20480; i++ {
pool.Put(make([]byte, 32*1024))
}
@@ -44,18 +23,50 @@ func TestBytePool_Memory(t *testing.T) {
var stat2 = &runtime.MemStats{}
runtime.ReadMemStats(stat2)
t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB,", pool.Size(), "slices")
t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB,")
}
func BenchmarkBytePool_Get(b *testing.B) {
runtime.GOMAXPROCS(1)
pool := NewBytePool(1024, 1)
var pool = NewBytePool(1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf := pool.Get()
var buf = pool.Get()
_ = buf
pool.Put(buf)
}
b.Log(pool.Size())
}
func BenchmarkBytePool_Get_Parallel(b *testing.B) {
runtime.GOMAXPROCS(1)
var pool = NewBytePool(1024)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var buf = pool.Get()
pool.Put(buf)
}
})
}
func BenchmarkBytePool_Get_Sync(b *testing.B) {
runtime.GOMAXPROCS(1)
var pool = &sync.Pool{
New: func() any {
return make([]byte, 1024)
},
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var buf = pool.Get()
pool.Put(buf)
}
})
}

View File

@@ -2,11 +2,13 @@ package utils
import (
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/iwind/TeaGo/types"
"time"
)
var unixTime = time.Now().Unix()
var unixTimeMilli = time.Now().UnixMilli()
var unixTimeMilliString = types.String(unixTimeMilli)
func init() {
var ticker = time.NewTicker(200 * time.Millisecond)
@@ -14,6 +16,7 @@ func init() {
for range ticker.C {
unixTime = time.Now().Unix()
unixTimeMilli = time.Now().UnixMilli()
unixTimeMilliString = types.String(unixTimeMilli)
}
})
}
@@ -43,6 +46,10 @@ func UnixTimeMilli() int64 {
return unixTimeMilli
}
func UnixTimeMilliString() (int64, string) {
return unixTimeMilli, unixTimeMilliString
}
// GMTUnixTime 计算GMT时间戳
func GMTUnixTime(timestamp int64) int64 {
_, offset := time.Now().Zone()