mirror of
https://github.com/kubernetes/client-go.git
synced 2025-08-14 21:43:27 +00:00
Not split nodes when searching for nodes but doing it all at once
Kubernetes-commit: 6c63dcfffebb9a8bcc5e1cee748ad16d7ed7e293
This commit is contained in:
parent
f06dbfd735
commit
35223a0681
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package workqueue
|
package workqueue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
@ -24,9 +25,20 @@ import (
|
|||||||
|
|
||||||
type DoWorkPieceFunc func(piece int)
|
type DoWorkPieceFunc func(piece int)
|
||||||
|
|
||||||
// Parallelize is a very simple framework that allow for parallelizing
|
// Parallelize is a very simple framework that allows for parallelizing
|
||||||
// N independent pieces of work.
|
// N independent pieces of work.
|
||||||
func Parallelize(workers, pieces int, doWorkPiece DoWorkPieceFunc) {
|
func Parallelize(workers, pieces int, doWorkPiece DoWorkPieceFunc) {
|
||||||
|
ParallelizeUntil(nil, workers, pieces, doWorkPiece)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParallelizeUntil is a framework that allows for parallelizing N
|
||||||
|
// independent pieces of work until done or the context is canceled.
|
||||||
|
func ParallelizeUntil(ctx context.Context, workers, pieces int, doWorkPiece DoWorkPieceFunc) {
|
||||||
|
var stop <-chan struct{}
|
||||||
|
if ctx != nil {
|
||||||
|
stop = ctx.Done()
|
||||||
|
}
|
||||||
|
|
||||||
toProcess := make(chan int, pieces)
|
toProcess := make(chan int, pieces)
|
||||||
for i := 0; i < pieces; i++ {
|
for i := 0; i < pieces; i++ {
|
||||||
toProcess <- i
|
toProcess <- i
|
||||||
@ -44,7 +56,12 @@ func Parallelize(workers, pieces int, doWorkPiece DoWorkPieceFunc) {
|
|||||||
defer utilruntime.HandleCrash()
|
defer utilruntime.HandleCrash()
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
for piece := range toProcess {
|
for piece := range toProcess {
|
||||||
doWorkPiece(piece)
|
select {
|
||||||
|
case <-stop:
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
doWorkPiece(piece)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user