errgroup.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package errgroup provides synchronization, error propagation, and Context
  5. // cancelation for groups of goroutines working on subtasks of a common task.
  6. //
  7. // [errgroup.Group] is related to [sync.WaitGroup] but adds handling of tasks
  8. // returning errors.
  9. package errgroup
  10. import (
  11. "context"
  12. "fmt"
  13. "sync"
  14. )
  15. type token struct{}
  16. // A Group is a collection of goroutines working on subtasks that are part of
  17. // the same overall task.
  18. //
  19. // A zero Group is valid, has no limit on the number of active goroutines,
  20. // and does not cancel on error.
  21. type Group struct {
  22. cancel func(error)
  23. wg sync.WaitGroup
  24. sem chan token
  25. errOnce sync.Once
  26. err error
  27. }
  28. func (g *Group) done() {
  29. if g.sem != nil {
  30. <-g.sem
  31. }
  32. g.wg.Done()
  33. }
  34. // WithContext returns a new Group and an associated Context derived from ctx.
  35. //
  36. // The derived Context is canceled the first time a function passed to Go
  37. // returns a non-nil error or the first time Wait returns, whichever occurs
  38. // first.
  39. func WithContext(ctx context.Context) (*Group, context.Context) {
  40. ctx, cancel := withCancelCause(ctx)
  41. return &Group{cancel: cancel}, ctx
  42. }
  43. // Wait blocks until all function calls from the Go method have returned, then
  44. // returns the first non-nil error (if any) from them.
  45. func (g *Group) Wait() error {
  46. g.wg.Wait()
  47. if g.cancel != nil {
  48. g.cancel(g.err)
  49. }
  50. return g.err
  51. }
  52. // Go calls the given function in a new goroutine.
  53. // It blocks until the new goroutine can be added without the number of
  54. // active goroutines in the group exceeding the configured limit.
  55. //
  56. // The first call to return a non-nil error cancels the group's context, if the
  57. // group was created by calling WithContext. The error will be returned by Wait.
  58. func (g *Group) Go(f func() error) {
  59. if g.sem != nil {
  60. g.sem <- token{}
  61. }
  62. g.wg.Add(1)
  63. go func() {
  64. defer g.done()
  65. if err := f(); err != nil {
  66. g.errOnce.Do(func() {
  67. g.err = err
  68. if g.cancel != nil {
  69. g.cancel(g.err)
  70. }
  71. })
  72. }
  73. }()
  74. }
  75. // TryGo calls the given function in a new goroutine only if the number of
  76. // active goroutines in the group is currently below the configured limit.
  77. //
  78. // The return value reports whether the goroutine was started.
  79. func (g *Group) TryGo(f func() error) bool {
  80. if g.sem != nil {
  81. select {
  82. case g.sem <- token{}:
  83. // Note: this allows barging iff channels in general allow barging.
  84. default:
  85. return false
  86. }
  87. }
  88. g.wg.Add(1)
  89. go func() {
  90. defer g.done()
  91. if err := f(); err != nil {
  92. g.errOnce.Do(func() {
  93. g.err = err
  94. if g.cancel != nil {
  95. g.cancel(g.err)
  96. }
  97. })
  98. }
  99. }()
  100. return true
  101. }
  102. // SetLimit limits the number of active goroutines in this group to at most n.
  103. // A negative value indicates no limit.
  104. //
  105. // Any subsequent call to the Go method will block until it can add an active
  106. // goroutine without exceeding the configured limit.
  107. //
  108. // The limit must not be modified while any goroutines in the group are active.
  109. func (g *Group) SetLimit(n int) {
  110. if n < 0 {
  111. g.sem = nil
  112. return
  113. }
  114. if len(g.sem) != 0 {
  115. panic(fmt.Errorf("errgroup: modify limit while %v goroutines in the group are still active", len(g.sem)))
  116. }
  117. g.sem = make(chan token, n)
  118. }