github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/asyncbuffer/init.go (about) 1 package asyncbuffer 2 3 import ( 4 "fmt" 5 6 "github.com/spf13/viper" 7 "github.com/weedge/lib/log" 8 ) 9 10 var gBufferSendDataInstances map[string]*SendData 11 12 func init() { 13 gBufferSendDataInstances = map[string]*SendData{} 14 } 15 16 // init instances by conf 17 func InitInstancesByConf() { 18 asyncBuffer := viper.GetStringMap(`async_buffer`) 19 for bufferName := range asyncBuffer { 20 bufferWindowSize := viper.GetInt(fmt.Sprintf(`async_buffer.%s.buffer_win_size`, bufferName)) 21 delaySendTime := viper.GetInt(fmt.Sprintf(`async_buffer.%s.delay_do_ms`, bufferName)) 22 chs := viper.GetStringMap(fmt.Sprintf(`async_buffer.%s.chs`, bufferName)) 23 for chName := range chs { 24 chLen := viper.GetInt(fmt.Sprintf(`async_buffer.%s.chs.%s.ch_len`, bufferName, chName)) 25 subWorkNum := viper.GetInt(fmt.Sprintf(`async_buffer.%s.chs.%s.sub_worker_num`, bufferName, chName)) 26 InitInstance(&Conf{ 27 BufferName: bufferName, 28 BufferSendChannel: map[string]*SendChannel{ 29 chName: { 30 ChName: chName, 31 ChLen: chLen, 32 SubWorkerNum: subWorkNum, 33 }}, 34 BufferWindowSize: bufferWindowSize, 35 DelaySendTime: delaySendTime, 36 }) 37 } //end for 38 } //end for 39 } 40 41 // get instance 42 func GetInstance(bufferName string) (err error, sd *SendData) { 43 if _, ok := gBufferSendDataInstances[bufferName]; !ok { 44 err = fmt.Errorf("bufferName: %s un init instance", bufferName) 45 return 46 } 47 sd = gBufferSendDataInstances[bufferName] 48 49 return 50 } 51 52 // init instance 53 func InitInstance(conf *Conf) { 54 if sd, ok := gBufferSendDataInstances[conf.BufferName]; !ok { 55 if conf.BufferWindowSize <= 0 { 56 conf.BufferWindowSize = DefaultBufferWindowSize 57 } 58 59 if conf.DelaySendTime <= 0 { 60 conf.DelaySendTime = DefaultDelaySendTimeMs 61 } 62 63 mapDataCh := map[string]chan []byte{} 64 for chName, sendCh := range conf.BufferSendChannel { 65 if sendCh.ChLen <= 0 { 66 sendCh.ChLen = 0 67 } 68 mapDataCh[chName] = make(chan []byte, sendCh.ChLen) 69 } 70 71 sd = &SendData{ 72 BufferName: conf.BufferName, 73 MapDataCh: mapDataCh, 74 BufferData: make([][]byte, conf.BufferWindowSize), 75 BufferIndex: 0, 76 BufferWindowSize: conf.BufferWindowSize, 77 IsFlushCh: make(chan bool), 78 DelaySendTime: conf.DelaySendTime, 79 BufferDayCounter: 0, 80 } 81 for chName, sendCh := range conf.BufferSendChannel { 82 if sendCh.SubWorkerNum <= 0 { 83 sendCh.SubWorkerNum = 1 84 } 85 sd.InitAsyncSub(chName, sendCh.SubWorkerNum) 86 } 87 88 sd.initFlushTicker(conf.BufferName) 89 gBufferSendDataInstances[conf.BufferName] = sd 90 log.Infof("GetInstance: %s new instance: %v", conf.BufferName, sd) 91 return 92 } 93 log.Debugf("GetInstance: %s instance: %v", conf.BufferName, gBufferSendDataInstances[conf.BufferName]) 94 return 95 } 96 97 func FlushAll() { 98 for bufferName, instance := range gBufferSendDataInstances { 99 log.Infof("flush bufferName:%s nmq send buffer~!", bufferName) 100 instance.FlushBuffer() 101 } 102 } 103 104 func FlushOne(bufferName string) { 105 log.Infof("flushOne bufferName:%s nmq send buffer~!", bufferName) 106 if _, ok := gBufferSendDataInstances[bufferName]; !ok { 107 log.Errorf("flush bufferName: %s error~!", bufferName) 108 return 109 } 110 gBufferSendDataInstances[bufferName].FlushBuffer() 111 }