Arpit
Arpit

Reputation: 601

Limit number of goroutine picking task in loop

I am running a loop - 24 times which calls a downstream that is not able to handle all the work supplied concurrently by the go-routines spawned, I want to limit that such that only a specific number (3 or 4) of go-routine gets executed. Sample code looks like below, if anyone can point me to the right pattern to fulfil this would be a great help

for i:=0; i<24; i++ {
   go callSomeDownStream()
}

Upvotes: 3

Views: 731

Answers (2)

zangw
zangw

Reputation: 48366

You can use the channel of empty structs to control the number of concurrent worker goroutines

const MAX_CONCURRENT_JOBS = 3

func main() {
    waitChan := make(chan struct{}, MAX_CONCURRENT_JOBS)

    for i:=0; i < 24; i++ {
        waitChan <- struct{}{}
        go func() {
            callSomeDownStream()
            <-waitChan
        }()
    }
}

Upvotes: 3

moyrne
moyrne

Reputation: 11

func callSomeDownStream(wg *sync.WaitGroup, queue chan struct{}) {
    defer func() {
        <-queue
        wg.Done()
    }()
    // do something
}

func main() {
    wg := sync.WaitGroup{}
    queue := make(chan struct{}, 3)
    for i := 0; i < 24; i++ {
        queue <- struct{}{}
        wg.Add(1)
        go callSomeDownStream(&wg, queue)
    }
    wg.Wait()
    close(queue)
}

Upvotes: 1

Related Questions