modelRT/pool/component_chan_set.go

57 lines
1.4 KiB
Go

package pool
import (
"context"
"sync"
"modelRT/config"
)
func init() {
globalComponentChanSet = &ComponentChanSet{
AnchorChans: make([]chan config.AnchorParamConfig, 100),
}
}
var globalComponentChanSet *ComponentChanSet
// ComponentChanSet defines component anchor real time data process channel set
type ComponentChanSet struct {
sync.RWMutex
AnchorChans []chan config.AnchorParamConfig
}
func GetComponentChan(ctx context.Context, componentID int64) chan config.AnchorParamConfig {
globalComponentChanSet.RLock()
componentChan := globalComponentChanSet.AnchorChans[componentID]
if componentChan == nil {
globalComponentChanSet.RUnlock()
globalComponentChanSet.Lock()
defer globalComponentChanSet.Unlock()
return CreateNewComponentChan(ctx, componentID)
}
globalComponentChanSet.RUnlock()
return componentChan
}
func CreateNewComponentChan(ctx context.Context, componentID int64) chan config.AnchorParamConfig {
componentChan := globalComponentChanSet.AnchorChans[componentID]
if componentChan == nil {
componentChan = make(chan config.AnchorParamConfig, 100)
globalComponentChanSet.AnchorChans[componentID] = componentChan
readyChan := make(chan struct{})
chanConfig := config.AnchorChanConfig{
Ctx: ctx,
AnchorChan: componentChan,
ReadyChan: readyChan,
}
AnchorRealTimePool.Invoke(chanConfig)
<-readyChan
return componentChan
}
return componentChan
}