modelRT/diagram/component_chan_set.go

57 lines
1.5 KiB
Go
Raw Normal View History

package diagram
import (
"context"
"sync"
"modelRT/config"
"github.com/panjf2000/ants/v2"
)
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, pool *ants.PoolWithFunc) chan config.AnchorParamConfig {
globalComponentChanSet.RLock()
componentChan := globalComponentChanSet.AnchorChans[componentID]
if componentChan == nil {
globalComponentChanSet.RUnlock()
globalComponentChanSet.Lock()
defer globalComponentChanSet.Unlock()
return CreateComponentChan(ctx, componentID, pool)
}
globalComponentChanSet.RUnlock()
return componentChan
}
func CreateComponentChan(ctx context.Context, componentID int64, pool *ants.PoolWithFunc) 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,
}
pool.Invoke(chanConfig)
<-readyChan
return componentChan
}
return componentChan
}