1.4.5.2
This commit is contained in:
68
EdgeNode/internal/utils/expires/id_key_map.go
Normal file
68
EdgeNode/internal/utils/expires/id_key_map.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package expires
|
||||
|
||||
type IdKeyMap struct {
|
||||
idKeys map[int64]string // id => key
|
||||
keyIds map[string]int64 // key => id
|
||||
}
|
||||
|
||||
func NewIdKeyMap() *IdKeyMap {
|
||||
return &IdKeyMap{
|
||||
idKeys: map[int64]string{},
|
||||
keyIds: map[string]int64{},
|
||||
}
|
||||
}
|
||||
|
||||
func (this *IdKeyMap) Add(id int64, key string) {
|
||||
oldKey, ok := this.idKeys[id]
|
||||
if ok {
|
||||
delete(this.keyIds, oldKey)
|
||||
}
|
||||
|
||||
oldId, ok := this.keyIds[key]
|
||||
if ok {
|
||||
delete(this.idKeys, oldId)
|
||||
}
|
||||
|
||||
this.idKeys[id] = key
|
||||
this.keyIds[key] = id
|
||||
}
|
||||
|
||||
func (this *IdKeyMap) Key(id int64) (key string, ok bool) {
|
||||
key, ok = this.idKeys[id]
|
||||
return
|
||||
}
|
||||
|
||||
func (this *IdKeyMap) Id(key string) (id int64, ok bool) {
|
||||
id, ok = this.keyIds[key]
|
||||
return
|
||||
}
|
||||
|
||||
func (this *IdKeyMap) DeleteId(id int64) {
|
||||
key, ok := this.idKeys[id]
|
||||
if ok {
|
||||
delete(this.keyIds, key)
|
||||
}
|
||||
delete(this.idKeys, id)
|
||||
}
|
||||
|
||||
func (this *IdKeyMap) DeleteKey(key string) {
|
||||
id, ok := this.keyIds[key]
|
||||
if ok {
|
||||
delete(this.idKeys, id)
|
||||
}
|
||||
delete(this.keyIds, key)
|
||||
}
|
||||
|
||||
func (this *IdKeyMap) Len() int {
|
||||
return len(this.idKeys)
|
||||
}
|
||||
|
||||
func (this *IdKeyMap) IdKeys() map[int64]string {
|
||||
return this.idKeys
|
||||
}
|
||||
|
||||
func (this *IdKeyMap) KeyIds() map[string]int64 {
|
||||
return this.keyIds
|
||||
}
|
||||
47
EdgeNode/internal/utils/expires/id_key_map_test.go
Normal file
47
EdgeNode/internal/utils/expires/id_key_map_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package expires_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/expires"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewIdKeyMap(t *testing.T) {
|
||||
var a = assert.NewAssertion(t)
|
||||
|
||||
var m = expires.NewIdKeyMap()
|
||||
m.Add(1, "1")
|
||||
m.Add(1, "2")
|
||||
m.Add(100, "100")
|
||||
logs.PrintAsJSON(m.IdKeys(), t)
|
||||
logs.PrintAsJSON(m.KeyIds(), t)
|
||||
|
||||
{
|
||||
k, ok := m.Key(1)
|
||||
a.IsTrue(ok)
|
||||
a.IsTrue(k == "2")
|
||||
}
|
||||
|
||||
{
|
||||
_, ok := m.Key(2)
|
||||
a.IsFalse(ok)
|
||||
}
|
||||
|
||||
m.DeleteKey("2")
|
||||
|
||||
{
|
||||
_, ok := m.Key(1)
|
||||
a.IsFalse(ok)
|
||||
}
|
||||
|
||||
logs.PrintAsJSON(m.IdKeys(), t)
|
||||
logs.PrintAsJSON(m.KeyIds(), t)
|
||||
|
||||
m.DeleteId(100)
|
||||
|
||||
logs.PrintAsJSON(m.IdKeys(), t)
|
||||
logs.PrintAsJSON(m.KeyIds(), t)
|
||||
}
|
||||
176
EdgeNode/internal/utils/expires/list.go
Normal file
176
EdgeNode/internal/utils/expires/list.go
Normal file
@@ -0,0 +1,176 @@
|
||||
package expires
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/zero"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ItemMap = map[uint64]zero.Zero
|
||||
|
||||
type List struct {
|
||||
expireMap map[int64]ItemMap // expires timestamp => map[id]ItemMap
|
||||
itemsMap map[uint64]int64 // itemId => timestamp
|
||||
|
||||
mu sync.RWMutex
|
||||
|
||||
gcCallback func(itemId uint64)
|
||||
gcBatchCallback func(itemIds ItemMap)
|
||||
|
||||
lastTimestamp int64
|
||||
}
|
||||
|
||||
func NewList() *List {
|
||||
var list = &List{
|
||||
expireMap: map[int64]ItemMap{},
|
||||
itemsMap: map[uint64]int64{},
|
||||
}
|
||||
|
||||
SharedManager.Add(list)
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
func NewSingletonList() *List {
|
||||
var list = &List{
|
||||
expireMap: map[int64]ItemMap{},
|
||||
itemsMap: map[uint64]int64{},
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
// Add 添加条目
|
||||
// 如果条目已经存在,则覆盖
|
||||
func (this *List) Add(itemId uint64, expiresAt int64) {
|
||||
this.mu.Lock()
|
||||
defer this.mu.Unlock()
|
||||
|
||||
if this.lastTimestamp == 0 || this.lastTimestamp > expiresAt {
|
||||
this.lastTimestamp = expiresAt
|
||||
}
|
||||
|
||||
// 是否已经存在
|
||||
oldExpiresAt, ok := this.itemsMap[itemId]
|
||||
if ok {
|
||||
if oldExpiresAt == expiresAt {
|
||||
return
|
||||
}
|
||||
delete(this.expireMap[oldExpiresAt], itemId)
|
||||
if len(this.expireMap[oldExpiresAt]) == 0 {
|
||||
delete(this.expireMap, oldExpiresAt)
|
||||
}
|
||||
}
|
||||
|
||||
expireItemMap, ok := this.expireMap[expiresAt]
|
||||
if ok {
|
||||
expireItemMap[itemId] = zero.New()
|
||||
} else {
|
||||
this.expireMap[expiresAt] = ItemMap{
|
||||
itemId: zero.New(),
|
||||
}
|
||||
}
|
||||
|
||||
this.itemsMap[itemId] = expiresAt
|
||||
}
|
||||
|
||||
func (this *List) Remove(itemId uint64) {
|
||||
this.mu.Lock()
|
||||
defer this.mu.Unlock()
|
||||
this.removeItem(itemId)
|
||||
}
|
||||
|
||||
func (this *List) ExpiresAt(itemId uint64) int64 {
|
||||
this.mu.RLock()
|
||||
defer this.mu.RUnlock()
|
||||
return this.itemsMap[itemId]
|
||||
}
|
||||
|
||||
func (this *List) GC(timestamp int64) ItemMap {
|
||||
if this.lastTimestamp > timestamp+1 {
|
||||
return nil
|
||||
}
|
||||
var itemMap = this.gcItems(timestamp)
|
||||
if len(itemMap) == 0 {
|
||||
return itemMap
|
||||
}
|
||||
|
||||
if this.gcCallback != nil {
|
||||
for itemId := range itemMap {
|
||||
this.gcCallback(itemId)
|
||||
}
|
||||
}
|
||||
if this.gcBatchCallback != nil {
|
||||
this.gcBatchCallback(itemMap)
|
||||
}
|
||||
|
||||
return itemMap
|
||||
}
|
||||
|
||||
func (this *List) Clean() {
|
||||
this.mu.Lock()
|
||||
this.itemsMap = map[uint64]int64{}
|
||||
this.expireMap = map[int64]ItemMap{}
|
||||
this.mu.Unlock()
|
||||
}
|
||||
|
||||
func (this *List) Count() int {
|
||||
this.mu.RLock()
|
||||
var count = len(this.itemsMap)
|
||||
this.mu.RUnlock()
|
||||
return count
|
||||
}
|
||||
|
||||
func (this *List) OnGC(callback func(itemId uint64)) *List {
|
||||
this.gcCallback = callback
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *List) OnGCBatch(callback func(itemMap ItemMap)) *List {
|
||||
this.gcBatchCallback = callback
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *List) ExpireMap() map[int64]ItemMap {
|
||||
return this.expireMap
|
||||
}
|
||||
|
||||
func (this *List) ItemsMap() map[uint64]int64 {
|
||||
return this.itemsMap
|
||||
}
|
||||
|
||||
func (this *List) LastTimestamp() int64 {
|
||||
return this.lastTimestamp
|
||||
}
|
||||
|
||||
func (this *List) removeItem(itemId uint64) {
|
||||
expiresAt, ok := this.itemsMap[itemId]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
delete(this.itemsMap, itemId)
|
||||
|
||||
expireItemMap, ok := this.expireMap[expiresAt]
|
||||
if ok {
|
||||
delete(expireItemMap, itemId)
|
||||
if len(expireItemMap) == 0 {
|
||||
delete(this.expireMap, expiresAt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *List) gcItems(timestamp int64) ItemMap {
|
||||
this.mu.RLock()
|
||||
expireItemsMap, ok := this.expireMap[timestamp]
|
||||
this.mu.RUnlock()
|
||||
|
||||
if ok {
|
||||
this.mu.Lock()
|
||||
for itemId := range expireItemsMap {
|
||||
delete(this.itemsMap, itemId)
|
||||
}
|
||||
delete(this.expireMap, timestamp)
|
||||
this.mu.Unlock()
|
||||
}
|
||||
|
||||
return expireItemsMap
|
||||
}
|
||||
260
EdgeNode/internal/utils/expires/list_test.go
Normal file
260
EdgeNode/internal/utils/expires/list_test.go
Normal file
@@ -0,0 +1,260 @@
|
||||
package expires_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/expires"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"math"
|
||||
"math/rand"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestList_Add(t *testing.T) {
|
||||
var list = expires.NewList()
|
||||
list.Add(1, time.Now().Unix())
|
||||
t.Log("===BEFORE===")
|
||||
logs.PrintAsJSON(list.ExpireMap(), t)
|
||||
logs.PrintAsJSON(list.ItemsMap(), t)
|
||||
|
||||
list.Add(1, time.Now().Unix()+1)
|
||||
list.Add(2, time.Now().Unix()+1)
|
||||
list.Add(3, time.Now().Unix()+2)
|
||||
t.Log("===AFTER===")
|
||||
logs.PrintAsJSON(list.ExpireMap(), t)
|
||||
logs.PrintAsJSON(list.ItemsMap(), t)
|
||||
}
|
||||
|
||||
func TestList_Add_Overwrite(t *testing.T) {
|
||||
var timestamp = time.Now().Unix()
|
||||
|
||||
var list = expires.NewList()
|
||||
list.Add(1, timestamp+1)
|
||||
list.Add(1, timestamp+1)
|
||||
list.Add(2, timestamp+1)
|
||||
list.Add(1, timestamp+2)
|
||||
logs.PrintAsJSON(list.ExpireMap(), t)
|
||||
logs.PrintAsJSON(list.ItemsMap(), t)
|
||||
|
||||
var a = assert.NewAssertion(t)
|
||||
a.IsTrue(len(list.ItemsMap()) == 2)
|
||||
a.IsTrue(len(list.ExpireMap()) == 2)
|
||||
a.IsTrue(list.ItemsMap()[1] == timestamp+2)
|
||||
}
|
||||
|
||||
func TestList_Remove(t *testing.T) {
|
||||
var a = assert.NewAssertion(t)
|
||||
|
||||
var list = expires.NewList()
|
||||
list.Add(1, time.Now().Unix()+1)
|
||||
list.Remove(1)
|
||||
logs.PrintAsJSON(list.ExpireMap(), t)
|
||||
logs.PrintAsJSON(list.ItemsMap(), t)
|
||||
|
||||
a.IsTrue(len(list.ExpireMap()) == 0)
|
||||
a.IsTrue(len(list.ItemsMap()) == 0)
|
||||
}
|
||||
|
||||
func TestList_GC(t *testing.T) {
|
||||
var unixTime = time.Now().Unix()
|
||||
t.Log("unixTime:", unixTime)
|
||||
|
||||
var list = expires.NewList()
|
||||
list.Add(1, unixTime+1)
|
||||
list.Add(2, unixTime+1)
|
||||
list.Add(3, unixTime+2)
|
||||
list.OnGC(func(itemId uint64) {
|
||||
t.Log("gc:", itemId)
|
||||
})
|
||||
t.Log("last unixTime:", list.LastTimestamp())
|
||||
list.GC(time.Now().Unix() + 2)
|
||||
logs.PrintAsJSON(list.ExpireMap(), t)
|
||||
logs.PrintAsJSON(list.ItemsMap(), t)
|
||||
|
||||
t.Log(list.Count())
|
||||
}
|
||||
|
||||
func TestList_GC_Batch(t *testing.T) {
|
||||
var list = expires.NewList()
|
||||
list.Add(1, time.Now().Unix()+1)
|
||||
list.Add(2, time.Now().Unix()+1)
|
||||
list.Add(3, time.Now().Unix()+2)
|
||||
list.Add(4, time.Now().Unix()+2)
|
||||
list.OnGCBatch(func(itemMap expires.ItemMap) {
|
||||
t.Log("gc:", itemMap)
|
||||
})
|
||||
list.GC(time.Now().Unix() + 2)
|
||||
logs.PrintAsJSON(list.ExpireMap(), t)
|
||||
logs.PrintAsJSON(list.ItemsMap(), t)
|
||||
}
|
||||
|
||||
func TestList_Start_GC(t *testing.T) {
|
||||
if !testutils.IsSingleTesting() {
|
||||
return
|
||||
}
|
||||
|
||||
var list = expires.NewList()
|
||||
list.Add(1, time.Now().Unix()+1)
|
||||
list.Add(2, time.Now().Unix()+1)
|
||||
list.Add(3, time.Now().Unix()+2)
|
||||
list.Add(3, time.Now().Unix()+10)
|
||||
list.Add(4, time.Now().Unix()+5)
|
||||
list.Add(5, time.Now().Unix()+5)
|
||||
list.Add(6, time.Now().Unix()+6)
|
||||
list.Add(7, time.Now().Unix()+6)
|
||||
list.Add(8, time.Now().Unix()+6)
|
||||
|
||||
list.OnGC(func(itemId uint64) {
|
||||
t.Log("gc:", itemId, timeutil.Format("H:i:s"))
|
||||
time.Sleep(2 * time.Second)
|
||||
})
|
||||
|
||||
go func() {
|
||||
expires.SharedManager.Add(list)
|
||||
}()
|
||||
|
||||
time.Sleep(20 * time.Second)
|
||||
logs.PrintAsJSON(list.ItemsMap())
|
||||
logs.PrintAsJSON(list.ExpireMap())
|
||||
}
|
||||
|
||||
func TestList_ManyItems(t *testing.T) {
|
||||
var list = expires.NewList()
|
||||
for i := 0; i < 1_000; i++ {
|
||||
list.Add(uint64(i), time.Now().Unix())
|
||||
}
|
||||
for i := 0; i < 1_000; i++ {
|
||||
list.Add(uint64(i), time.Now().Unix()+1)
|
||||
}
|
||||
|
||||
var now = time.Now()
|
||||
var count = 0
|
||||
list.OnGC(func(itemId uint64) {
|
||||
count++
|
||||
})
|
||||
list.GC(time.Now().Unix() + 1)
|
||||
t.Log("gc", count, "items")
|
||||
t.Log(time.Since(now))
|
||||
}
|
||||
|
||||
func TestList_Memory(t *testing.T) {
|
||||
if !testutils.IsSingleTesting() {
|
||||
return
|
||||
}
|
||||
|
||||
var list = expires.NewList()
|
||||
|
||||
testutils.StartMemoryStats(t, func() {
|
||||
t.Log(list.Count(), "items")
|
||||
})
|
||||
|
||||
for i := 0; i < 10_000_000; i++ {
|
||||
list.Add(uint64(i), time.Now().Unix()+1800)
|
||||
}
|
||||
|
||||
time.Sleep(1 * time.Hour)
|
||||
}
|
||||
|
||||
func TestList_Map_Performance(t *testing.T) {
|
||||
t.Log("max uint32", math.MaxUint32)
|
||||
|
||||
var timestamp = time.Now().Unix()
|
||||
|
||||
{
|
||||
var m = map[int64]int64{}
|
||||
for i := 0; i < 1_000_000; i++ {
|
||||
m[int64(i)] = timestamp
|
||||
}
|
||||
|
||||
var now = time.Now()
|
||||
for i := 0; i < 100_000; i++ {
|
||||
delete(m, int64(i))
|
||||
}
|
||||
t.Log(time.Since(now))
|
||||
}
|
||||
|
||||
{
|
||||
var m = map[uint64]int64{}
|
||||
for i := 0; i < 1_000_000; i++ {
|
||||
m[uint64(i)] = timestamp
|
||||
}
|
||||
|
||||
var now = time.Now()
|
||||
for i := 0; i < 100_000; i++ {
|
||||
delete(m, uint64(i))
|
||||
}
|
||||
t.Log(time.Since(now))
|
||||
}
|
||||
|
||||
{
|
||||
var m = map[uint32]int64{}
|
||||
for i := 0; i < 1_000_000; i++ {
|
||||
m[uint32(i)] = timestamp
|
||||
}
|
||||
|
||||
var now = time.Now()
|
||||
for i := 0; i < 100_000; i++ {
|
||||
delete(m, uint32(i))
|
||||
}
|
||||
t.Log(time.Since(now))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkList_Add(b *testing.B) {
|
||||
var list = expires.NewList()
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
list.Add(rand.Uint64(), fasttime.Now().Unix()+int64(rand.Int()%10_000_000))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Benchmark_Map_Uint64(b *testing.B) {
|
||||
runtime.GOMAXPROCS(1)
|
||||
var timestamp = uint64(time.Now().Unix())
|
||||
|
||||
var i uint64
|
||||
var count uint64 = 1_000_000
|
||||
|
||||
var m = map[uint64]uint64{}
|
||||
for i = 0; i < count; i++ {
|
||||
m[i] = timestamp
|
||||
}
|
||||
|
||||
for n := 0; n < b.N; n++ {
|
||||
for i = 0; i < count; i++ {
|
||||
_ = m[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkList_GC(b *testing.B) {
|
||||
runtime.GOMAXPROCS(4)
|
||||
|
||||
var lists = []*expires.List{}
|
||||
|
||||
for m := 0; m < 1_000; m++ {
|
||||
var list = expires.NewList()
|
||||
for j := 0; j < 10_000; j++ {
|
||||
list.Add(uint64(j), fasttime.Now().Unix()+int64(rand.Int()%10_000_000))
|
||||
}
|
||||
lists = append(lists, list)
|
||||
}
|
||||
|
||||
var timestamp = time.Now().Unix()
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
for _, list := range lists {
|
||||
list.GC(timestamp + int64(rand.Int()%1_000_000))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
73
EdgeNode/internal/utils/expires/manager.go
Normal file
73
EdgeNode/internal/utils/expires/manager.go
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package expires
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/zero"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var SharedManager = NewManager()
|
||||
|
||||
type Manager struct {
|
||||
listMap map[*List]zero.Zero
|
||||
locker sync.Mutex
|
||||
ticker *time.Ticker
|
||||
}
|
||||
|
||||
func NewManager() *Manager {
|
||||
var manager = &Manager{
|
||||
listMap: map[*List]zero.Zero{},
|
||||
ticker: time.NewTicker(1 * time.Second),
|
||||
}
|
||||
goman.New(func() {
|
||||
manager.init()
|
||||
})
|
||||
return manager
|
||||
}
|
||||
|
||||
func (this *Manager) init() {
|
||||
var lastTimestamp = int64(0)
|
||||
for range this.ticker.C {
|
||||
var currentTime = time.Now().Unix()
|
||||
if lastTimestamp == 0 {
|
||||
lastTimestamp = currentTime - 86400 // prevent timezone changes
|
||||
}
|
||||
|
||||
if currentTime >= lastTimestamp {
|
||||
for i := lastTimestamp; i <= currentTime; i++ {
|
||||
this.locker.Lock()
|
||||
for list := range this.listMap {
|
||||
list.GC(i)
|
||||
}
|
||||
this.locker.Unlock()
|
||||
}
|
||||
} else {
|
||||
// 如果过去的时间比现在大,则从这一秒重新开始
|
||||
for i := currentTime; i <= currentTime; i++ {
|
||||
this.locker.Lock()
|
||||
for list := range this.listMap {
|
||||
list.GC(i)
|
||||
}
|
||||
this.locker.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// 这样做是为了防止系统时钟突变
|
||||
lastTimestamp = currentTime
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Manager) Add(list *List) {
|
||||
this.locker.Lock()
|
||||
this.listMap[list] = zero.New()
|
||||
this.locker.Unlock()
|
||||
}
|
||||
|
||||
func (this *Manager) Remove(list *List) {
|
||||
this.locker.Lock()
|
||||
delete(this.listMap, list)
|
||||
this.locker.Unlock()
|
||||
}
|
||||
Reference in New Issue
Block a user