148 lines
2.9 KiB
Go
148 lines
2.9 KiB
Go
package service
|
|
|
|
import (
|
|
"f5"
|
|
"q5"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
type mqManager struct {
|
|
topicMap q5.ConcurrentMap[string, *topicCache]
|
|
lock *sync.Mutex
|
|
publishcount int32
|
|
consumecount int32
|
|
expirecount int32
|
|
}
|
|
|
|
type topicCache struct {
|
|
lock *sync.Mutex
|
|
msgList []*msgItem
|
|
}
|
|
|
|
func (this *topicCache) Lock() {
|
|
this.lock.Lock()
|
|
}
|
|
|
|
func (this *topicCache) Unlock() {
|
|
this.lock.Unlock()
|
|
}
|
|
|
|
type msgItem struct {
|
|
content string
|
|
addTime int64
|
|
expire int64
|
|
}
|
|
|
|
func (this *mqManager) init() {
|
|
this.topicMap = q5.ConcurrentMap[string, *topicCache]{}
|
|
this.lock = new(sync.Mutex)
|
|
go this.outputMonitorLog()
|
|
}
|
|
|
|
func (this *mqManager) unInit() {
|
|
}
|
|
|
|
func (this *mqManager) Lock() {
|
|
this.lock.Lock()
|
|
}
|
|
|
|
func (this *mqManager) Unlock() {
|
|
this.lock.Unlock()
|
|
}
|
|
|
|
func (this *mqManager) PublishTopic(topic, message string, life int32) {
|
|
this.Lock()
|
|
t, exist := this.topicMap.Load(topic)
|
|
if !exist {
|
|
tc := new(topicCache)
|
|
tc.lock = new(sync.Mutex)
|
|
tc.msgList = make([]*msgItem, 0)
|
|
this.topicMap.Store(topic, tc)
|
|
t = &tc
|
|
}
|
|
this.Unlock()
|
|
|
|
(*t).Lock()
|
|
newitem := new(msgItem)
|
|
newitem.addTime = f5.GetApp().GetRealSeconds()
|
|
newitem.content = message
|
|
newitem.expire = newitem.addTime + int64(life)
|
|
(*t).msgList = append((*t).msgList, newitem)
|
|
(*t).Unlock()
|
|
this.IncPublishTimes()
|
|
}
|
|
|
|
func (this *mqManager) ConsumeTopic(topic string) (msg string) {
|
|
msg = ""
|
|
t, exist := this.topicMap.Load(topic)
|
|
if !exist {
|
|
return
|
|
}
|
|
|
|
(*t).Lock()
|
|
defer (*t).Unlock()
|
|
|
|
if len((*t).msgList) == 0 {
|
|
return
|
|
}
|
|
|
|
msg = (*t).msgList[0].content
|
|
(*t).msgList = (*t).msgList[1:]
|
|
|
|
this.IncConsumeTimes()
|
|
return
|
|
}
|
|
|
|
func (this *mqManager) IncPublishTimes() {
|
|
atomic.AddInt32(&this.publishcount, 1)
|
|
}
|
|
|
|
func (this *mqManager) IncConsumeTimes() {
|
|
atomic.AddInt32(&this.consumecount, 1)
|
|
}
|
|
|
|
func (this *mqManager) IncExpireTimes() {
|
|
atomic.AddInt32(&this.expirecount, 1)
|
|
|
|
}
|
|
|
|
func (this *mqManager) outputMonitorLog() {
|
|
logtimes := 0
|
|
for {
|
|
f5.GetSysLog().Info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
|
|
f5.GetSysLog().Info("publishcount:%d, consumecount:%d, expirecount:%d",
|
|
this.publishcount,
|
|
this.consumecount,
|
|
this.expirecount)
|
|
f5.GetSysLog().Info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
|
|
|
|
logtimes++
|
|
if logtimes > 6 {
|
|
logtimes = 0
|
|
this.publishcount = 0
|
|
this.consumecount = 0
|
|
this.expirecount = 0
|
|
|
|
this.Lock()
|
|
now := f5.GetApp().GetRealSeconds()
|
|
this.topicMap.Range(func(key string, value *topicCache) bool {
|
|
value.Lock()
|
|
defer value.Unlock()
|
|
for i := 0; i < len(value.msgList); {
|
|
if value.msgList[i].expire < now {
|
|
value.msgList = append(value.msgList[:i], value.msgList[i+1:]...)
|
|
this.IncExpireTimes()
|
|
} else {
|
|
i++
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
this.Unlock()
|
|
}
|
|
time.Sleep(time.Second * 10)
|
|
}
|
|
}
|