Home  >  Article  >  Backend Development  >  How to configure goroutine limits correctly?

How to configure goroutine limits correctly?

WBOY
WBOYforward
2024-02-13 15:39:08621browse

How to configure goroutine limits correctly?

php editor Yuzai will introduce you how to correctly configure goroutine restrictions. In the Go language, goroutine is a lightweight thread that can execute tasks concurrently. However, if not limited, too many goroutines may cause resource exhaustion or program crash. Therefore, it is very important to configure goroutine limits correctly. Next, we'll explore some common configuration methods to ensure your program can run stably and make full use of system resources.

Question content

I have a task that executes three functions if three functions are selected to work. I want to limit the number of horoutines per function. For example, each goroutine can only run a maximum of 10.

func main() {
    checkMethod1 := true
    checkMethod2 := false
    checkMethod3 := true

    list := []string{"info1", "info2", "info3", "info5"}

    for _, value := range list {
        value := value
        if checkMethod1 {
            go func() {
                //use value
                fmt.Println(value)
            }()
        }
        if checkMethod2 {
            go func() {
                //use value
                fmt.Println(value)
            }()
        }
        if checkMethod3 {
            go func() {
                //use value
                fmt.Println(value)
            }()
        }
    }
    //finish
    fmt.Println("All done")
}

I know you can limit the number of goroutines to a worker pool. However, if I create a worker pool limited to 10 goroutines, that number is divided by 3 tasks, and I need 10 goroutines per function.

I could create 3 pools, but that doesn't seem like a feasible approach to me.

I want to use this library to create a worker pool: https://github.com/sourcegraph/conc

Solution

Here is a method: use one buffer for each option Channels so you can limit active goroutines:

m1:=make(chan struct{},10)
m2:=make(chan struct{},10)
m3:=make(chan struct{},10)
wg:=sync.WaitGroup{}

for _, value := range list {
   value := value
   if checkMethod1 {
       m1<-struct{}{}
       wg.Add(1)
       go func() {
           defer func() { 
              <-m1 
              wg.Done()
           }()
           // do work
       }()
   }
   if checkMethod2 {
       m2<-struct{}{}
       wg.Add(1)
       go func() {
           defer func() { 
               <-m2 
               wg.Done()
           }()
           // do work
       }()
   }
   ...

  wg.Wait()
}

The above is the detailed content of How to configure goroutine limits correctly?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete