节点所属集群删除后,不再接收API请求

This commit is contained in:
刘祥超
2022-10-23 19:56:58 +08:00
parent 88dae56b6c
commit e21a3c5f8c
21 changed files with 1443 additions and 34 deletions

View File

@@ -0,0 +1,60 @@
// 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)
}

View File

@@ -0,0 +1,46 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package expires
import (
"github.com/iwind/TeaGo/assert"
"github.com/iwind/TeaGo/logs"
"testing"
)
func TestNewIdKeyMap(t *testing.T) {
var a = assert.NewAssertion(t)
var m = 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)
}

View File

@@ -0,0 +1,159 @@
package expires
import (
"github.com/TeaOSLab/EdgeAPI/internal/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
locker sync.Mutex
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.locker.Lock()
defer this.locker.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)
}
expireItemMap, ok := this.expireMap[expiresAt]
if ok {
expireItemMap[itemId] = zero.New()
} else {
expireItemMap = ItemMap{
itemId: zero.New(),
}
this.expireMap[expiresAt] = expireItemMap
}
this.itemsMap[itemId] = expiresAt
}
func (this *List) Remove(itemId uint64) {
this.locker.Lock()
defer this.locker.Unlock()
this.removeItem(itemId)
}
func (this *List) ExpiresAt(itemId uint64) int64 {
this.locker.Lock()
defer this.locker.Unlock()
return this.itemsMap[itemId]
}
func (this *List) GC(timestamp int64) ItemMap {
if this.lastTimestamp > timestamp+1 {
return nil
}
this.locker.Lock()
var itemMap = this.gcItems(timestamp)
if len(itemMap) == 0 {
this.locker.Unlock()
return itemMap
}
this.locker.Unlock()
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.locker.Lock()
this.itemsMap = map[uint64]int64{}
this.expireMap = map[int64]ItemMap{}
this.locker.Unlock()
}
func (this *List) Count() int {
this.locker.Lock()
var count = len(this.itemsMap)
this.locker.Unlock()
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) 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 {
expireItemsMap, ok := this.expireMap[timestamp]
if ok {
for itemId := range expireItemsMap {
delete(this.itemsMap, itemId)
}
delete(this.expireMap, timestamp)
}
return expireItemsMap
}

View File

@@ -0,0 +1,216 @@
package expires
import (
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/iwind/TeaGo/assert"
"github.com/iwind/TeaGo/logs"
timeutil "github.com/iwind/TeaGo/utils/time"
"math"
"runtime"
"testing"
"time"
)
func TestList_Add(t *testing.T) {
list := 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()
list := NewList()
list.Add(1, timestamp+1)
list.Add(1, 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) == 1)
a.IsTrue(len(list.expireMap) == 1)
a.IsTrue(list.itemsMap[1] == timestamp+2)
}
func TestList_Remove(t *testing.T) {
list := NewList()
list.Add(1, time.Now().Unix()+1)
list.Remove(1)
logs.PrintAsJSON(list.expireMap, t)
logs.PrintAsJSON(list.itemsMap, t)
}
func TestList_GC(t *testing.T) {
var unixTime = time.Now().Unix()
t.Log("unixTime:", unixTime)
var list = 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) {
list := 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 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) {
list := 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()+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() {
SharedManager.Add(list)
}()
time.Sleep(20 * time.Second)
}
func TestList_ManyItems(t *testing.T) {
list := 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)
}
now := time.Now()
count := 0
list.OnGC(func(itemId uint64) {
count++
})
list.GC(time.Now().Unix() + 1)
t.Log("gc", count, "items")
t.Log(time.Now().Sub(now))
}
func TestList_Map_Performance(t *testing.T) {
t.Log("max uint32", math.MaxUint32)
var timestamp = time.Now().Unix()
{
m := map[int64]int64{}
for i := 0; i < 1_000_000; i++ {
m[int64(i)] = timestamp
}
now := time.Now()
for i := 0; i < 100_000; i++ {
delete(m, int64(i))
}
t.Log(time.Now().Sub(now))
}
{
m := map[uint64]int64{}
for i := 0; i < 1_000_000; i++ {
m[uint64(i)] = timestamp
}
now := time.Now()
for i := 0; i < 100_000; i++ {
delete(m, uint64(i))
}
t.Log(time.Now().Sub(now))
}
{
m := map[uint32]int64{}
for i := 0; i < 1_000_000; i++ {
m[uint32(i)] = timestamp
}
now := time.Now()
for i := 0; i < 100_000; i++ {
delete(m, uint32(i))
}
t.Log(time.Now().Sub(now))
}
}
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
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(1)
var lists = []*List{}
for m := 0; m < 1_000; m++ {
var list = NewList()
for j := 0; j < 10_000; j++ {
list.Add(uint64(j), utils.UnixTime()+100)
}
lists = append(lists, list)
}
b.ResetTimer()
var timestamp = time.Now().Unix()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
for _, list := range lists {
list.GC(timestamp)
}
}
})
}

View File

@@ -0,0 +1,73 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package expires
import (
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/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 - 3600
}
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()
}