errgroup.go 3.2 KB

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