github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/logstore/sm/loop.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package sm 16 17 import ( 18 "context" 19 "sync" 20 ) 21 22 type Loop struct { 23 queue chan any 24 fn func([]any, chan any) 25 nextQueue chan any 26 queueCtx context.Context 27 cancelFn context.CancelFunc 28 wg *sync.WaitGroup 29 maxBatchSize int 30 31 Itemcount int 32 Itemtimes int 33 } 34 35 func NewLoop(queue, nextQueue chan any, fn func([]any, chan any), maxBatchSize int) *Loop { 36 return &Loop{ 37 queue: queue, 38 fn: fn, 39 nextQueue: nextQueue, 40 maxBatchSize: maxBatchSize, 41 } 42 } 43 44 func (l *Loop) Start() { 45 l.queueCtx, l.cancelFn = context.WithCancel(context.Background()) 46 l.wg = &sync.WaitGroup{} 47 l.wg.Add(1) 48 go l.loop() 49 } 50 51 func (l *Loop) loop() { 52 defer l.wg.Done() 53 for { 54 batch := make([]any, 0) 55 select { 56 case <-l.queueCtx.Done(): 57 return 58 case item := <-l.queue: 59 batch = append(batch, item) 60 Left: 61 for i := 0; i < l.maxBatchSize; i++ { 62 select { 63 case item = <-l.queue: 64 batch = append(batch, item) 65 default: 66 break Left 67 } 68 } 69 } 70 l.Itemcount += len(batch) 71 l.Itemtimes++ 72 l.fn(batch, l.nextQueue) 73 } 74 } 75 76 func (l *Loop) Stop() { 77 l.cancelFn() 78 l.wg.Wait() 79 }