github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/remotestorage/concurrency.go (about) 1 // Copyright 2019 Dolthub, Inc. 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 remotestorage 16 17 import ( 18 "context" 19 20 "golang.org/x/sync/errgroup" 21 ) 22 23 func concurrentExec(work []func() error, concurrency int) error { 24 if concurrency <= 0 { 25 panic("Invalid argument") 26 } 27 if len(work) == 0 { 28 return nil 29 } 30 if len(work) < concurrency { 31 concurrency = len(work) 32 } 33 34 ch := make(chan func() error) 35 36 eg, ctx := errgroup.WithContext(context.Background()) 37 38 // Push the work... 39 eg.Go(func() error { 40 defer close(ch) 41 for _, w := range work { 42 select { 43 case ch <- w: 44 case <-ctx.Done(): 45 return ctx.Err() 46 } 47 } 48 return nil 49 }) 50 51 // Do the work... 52 for i := 0; i < concurrency; i++ { 53 eg.Go(func() error { 54 for { 55 select { 56 case w, ok := <-ch: 57 if !ok { 58 return nil 59 } 60 if err := w(); err != nil { 61 return err 62 } 63 case <-ctx.Done(): 64 return ctx.Err() 65 } 66 } 67 }) 68 } 69 70 return eg.Wait() 71 } 72 73 func batchItr(elemCount, batchSize int, cb func(start, end int) (stop bool)) { 74 for st, end := 0, batchSize; st < elemCount; st, end = end, end+batchSize { 75 if end > elemCount { 76 end = elemCount 77 } 78 79 stop := cb(st, end) 80 81 if stop { 82 break 83 } 84 } 85 }