【问题标题】:Golang WaitGroup Timeout Error handlingGolang WaitGroup 超时错误处理
【发布时间】:2020-11-10 00:29:42
【问题描述】:

在下面的代码中,如果启动的 go 例程之一需要太长时间(例如 > 10 秒)才能完成,我该如何添加适当的超时错误处理?请注意,我不想有一个“整体”超时,而是每个 go 例程都有一个超时,这样我也可以在我的错误报告中知道哪个 go 例程超时。

var wg sync.WaitGroup

for _, element:= range elements{
    wg.Add(1)
    go doWork(element, &wg)
}
wg.Wait()

亲切的问候

【问题讨论】:

    标签: go


    【解决方案1】:

    不错的方法是使用context.WithDeadline

    // WithDeadline returns a copy of the parent context with the deadline adjusted
    // to be no later than d. If the parent's deadline is already earlier than d,
    // WithDeadline(parent, d) is semantically equivalent to parent. The returned
    // context's Done channel is closed when the deadline expires, when the returned
    // cancel function is called, or when the parent context's Done channel is
    // closed, whichever happens first.
    //
    // Canceling this context releases resources associated with it, so code should
    // call cancel as soon as the operations running in this Context complete.
    
    package main
    
    import (
        "context"
        "fmt"
        "time"
    )
    
    func main() {
        ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(100*time.Millisecond))
    
        // Even though ctx will be expired, it is good practice to call its
        // cancelation function in any case. Failure to do so may keep the
        // context and its parent alive longer than necessary.
        defer cancel()
    
        select {
        case <-time.After(1 * time.Second):
            fmt.Println("overslept")
        case <-ctx.Done():
            fmt.Println(ctx.Err())
        }
    
    }
    

    【讨论】:

    • context.WithDeadline 调用可以简化为 context.WithTimeout(context.Background(), 100*time.Millisecond)
    • @Peter:WithTimeout 本身调用WithDeadline 见内:func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) { return WithDeadline(parent, time.Now().Add(timeout)) }
    【解决方案2】:

    您可以通过以下方式使用Context

    func doWork(ctx context.Context, element Element, wg &sync.WaitGroup) {
        defer wg.Done()
    
        done := make(chan struct{})
        go func() {
           // do some work on element
           done <- struct{}{} // signal work is done
        }
    
        select {
           case <- done: 
           { 
              // work completed in time
           }
           case <- ctx.Done:
           {
             // timeout reached
           }
        }
    }
    
    contexts := make([]*context.Context, len(elements))
    
    for _, element:= range elements{
        wg.Add(1)
        ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
        defer cancel()
        contexts = append(contexts, ctx)
        go doWork(ctx, element, &wg)
    }
    wg.Wait()
    
    for i, ctx := range contexts {
      if ctx.Err() {
         fmt.Println("Go routine ", i, "canceled due to", ctx.Err())
      }
    }
    

    【讨论】:

      【解决方案3】:

      我确实有同样的问题并想出了这个方法:

      https://play.golang.org/p/9F9T_sYIof

      使用:context.WithTimeout(context.Background(), 10*time.Second)

      https://play.golang.org/p/WK0ebe0c9t

      不知道这样做是否正确但有效:

      package main
      
      import (
          "context"
          "fmt"
          "sync"
          "time"
      )
      
      func doWork(element int, wg *sync.WaitGroup) {
          ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
          defer cancel()
      
          ch := make(chan struct{})
      
          go func() {
              time.Sleep(time.Second)
              fmt.Printf("element = %+v\n", element)
              ch <- struct{}{}
          }()
      
          select {
          case <-ch:
          case <-ctx.Done():
              fmt.Println(ctx.Err())
          }
          wg.Done()
      }
      
      func main() {
          var wg sync.WaitGroup
      
          elements := []int{1, 2, 3}
      
          for _, element := range elements {
              wg.Add(1)
              go doWork(element, &wg)
          }
          wg.Wait()
      }
      

      注意doWork 函数中的goroutine:

      go func(ch chan struct{}) {
         // your code logic goes here
      }(ch)
      

      这部分我不知道是否是最好的方法,但似乎是主要在想要处理ctx.Done()时使用上下文时遵循的模式

      【讨论】:

      • context.WithDeadline 调用可以简化为context.WithTimeout(context.Background(), 10*time.Second)
      • 此代码将造成内存泄漏,因为这是无缓冲通道ch := make(chan struct{}),因此更改为 ch := make(chan struct{}, 1) 以避免内存泄漏。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 2015-03-29
      • 2017-04-07
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多