mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
[优化]设备数据上传并发处理,添加队列,以及并发数控制
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user