sigs.k8s.io/kueue@v0.6.2/pkg/util/parallelize/parallelize.go (about) 1 /* 2 Copyright 2024 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package parallelize 18 19 import ( 20 "context" 21 22 "k8s.io/client-go/util/workqueue" 23 ) 24 25 const maxParallelism = 8 26 27 // ErrorChannel supports non-blocking send and receive operation to capture error. 28 // A maximum of one error is kept in the channel and the rest of the errors sent 29 // are ignored, unless the existing error is received and the channel becomes empty 30 // again. 31 type ErrorChannel struct { 32 ch chan error 33 } 34 35 func NewErrorChannel() *ErrorChannel { 36 return &ErrorChannel{ 37 ch: make(chan error, 1), 38 } 39 } 40 41 func (e *ErrorChannel) SendError(err error) { 42 if err == nil { 43 return 44 } 45 select { 46 case e.ch <- err: 47 default: 48 } 49 } 50 51 func (e *ErrorChannel) Receive() error { 52 select { 53 case err := <-e.ch: 54 return err 55 default: 56 return nil 57 } 58 } 59 60 func Until(ctx context.Context, pieces int, doWorkPiece func(i int) error) error { 61 errCh := NewErrorChannel() 62 workers := min(pieces, maxParallelism) 63 workqueue.ParallelizeUntil(ctx, workers, pieces, func(i int) { 64 errCh.SendError(doWorkPiece(i)) 65 }) 66 return errCh.Receive() 67 }