package data import ( "context" "datart/data/influx" "datart/data/postgres" "datart/data/redis" "datart/log" "sync" "time" ) const ( workerNum = 5 ) const ( update104Duration time.Duration = time.Second * 1 ) type cl104Q struct { Station string CA string IOA string // Begin int64 // End int64 } type workerPool struct { workerNum int wg sync.WaitGroup qChan chan cl104Q } var cl104Pool *workerPool func init() { cl104Pool = newWorkerPool(workerNum) } func newWorkerPool(workerNum int) *workerPool { return &workerPool{ workerNum: workerNum, qChan: make(chan cl104Q, 128), } } func updatingRedisCL104(ctx context.Context) { go func() { for { select { case <-ctx.Done(): return case <-time.After(update104Duration): postgres.ChannelSizes.Range(func(key, value any) bool { if channelSize, ok := value.(postgres.ChannelSize); ok && channelSize.Type == 2 { cl104Pool.qChan <- cl104Q{ Station: channelSize.Station, CA: channelSize.MainPos, IOA: channelSize.SubPos, } } return true }) } } }() for range workerNum { go func() { for { select { case <-ctx.Done(): return default: q := <-cl104Pool.qChan tvs, err := cl104Pool.queryInflux104(ctx, q) if err != nil { log.Error(err) } else { cl104Pool.refreshRedis(ctx, q, tvs) } } } }() } } func (wp *workerPool) queryInflux104(ctx context.Context, q cl104Q) ([]influx.TV, error) { db, err := influx.GetDB("104") if err != nil { return nil, err } tb, err := influx.GetTable("104", q.CA) if err != nil { return nil, err } tvs, err := influx.Get104PointLast(ctx, &influx.Request{ DB: db, Table: tb, Type: "104", Station: q.Station, MainPos: q.CA, SubPos: q.IOA, }) if err != nil { return nil, err } return tvs, nil } func (wp *workerPool) refreshRedis(ctx context.Context, q cl104Q, tvs []influx.TV) { key := genRedis104Key(q.Station, q.CA, q.IOA) members := convertTVsToMenmbers(tvs) redis.ZAtomicReplace(ctx, key, members) } func genRedis104Key(station string, ca string, ioa string) string { return station + ":104:" + ca + ":" + ioa }