[优化]设备数据上传并发处理,添加队列,以及并发数控制

This commit is contained in:
PandaX
2023-11-28 17:59:01 +08:00
parent 58341b0236
commit 287c8a1b05
8 changed files with 45 additions and 16 deletions

View File

@@ -1,6 +1,10 @@
package hook_message_work
import (
"context"
"encoding/json"
"github.com/golang-queue/queue"
"github.com/golang-queue/queue/core"
"pandax/iothub/netbase"
"pandax/pkg/global"
"sync"
@@ -8,19 +12,28 @@ import (
type HookService struct {
Cache sync.Map
Wg sync.WaitGroup // 优雅关闭
Queue *queue.Queue
Ch chan struct{} // 并发限制
Wg sync.WaitGroup // 优雅关闭
MessageCh chan *netbase.DeviceEventInfo
}
func NewHookService() *HookService {
hs := &HookService{
Cache: sync.Map{},
MessageCh: make(chan *netbase.DeviceEventInfo),
}
// 并发限制,代表服务器处理能力
if global.Conf.Queue.Enable && global.Conf.Queue.Num > 0 {
hs.Ch = make(chan struct{}, global.Conf.Queue.Num)
Ch: make(chan struct{}, global.Conf.Queue.ChNum),
MessageCh: make(chan *netbase.DeviceEventInfo, global.Conf.Queue.TaskNum),
}
pool := queue.NewPool(int(global.Conf.Queue.QueuePool), queue.WithFn(func(ctx context.Context, m core.QueuedMessage) error {
v, ok := m.(*netbase.DeviceEventInfo)
if !ok {
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
return err
}
}
hs.MessageCh <- v
return nil
}))
hs.Queue = pool
return hs
}