grpool.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package grpool implements a goroutine reusable pool.
  7. package grpool
  8. import (
  9. "context"
  10. "time"
  11. "github.com/gogf/gf/v2/container/glist"
  12. "github.com/gogf/gf/v2/container/gtype"
  13. "github.com/gogf/gf/v2/os/gtimer"
  14. "github.com/gogf/gf/v2/util/grand"
  15. )
  16. // Func is the pool function which contains context parameter.
  17. type Func func(ctx context.Context)
  18. // RecoverFunc is the pool runtime panic recover function which contains context parameter.
  19. type RecoverFunc func(ctx context.Context, exception error)
  20. // Pool manages the goroutines using pool.
  21. type Pool struct {
  22. limit int // Max goroutine count limit.
  23. count *gtype.Int // Current running goroutine count.
  24. list *glist.List // List for asynchronous job adding purpose.
  25. closed *gtype.Bool // Is pool closed or not.
  26. }
  27. // localPoolItem is the job item storing in job list.
  28. type localPoolItem struct {
  29. Ctx context.Context // Context.
  30. Func Func // Job function.
  31. }
  32. const (
  33. minSupervisorTimerDuration = 500 * time.Millisecond
  34. maxSupervisorTimerDuration = 1500 * time.Millisecond
  35. )
  36. // Default goroutine pool.
  37. var (
  38. defaultPool = New()
  39. )
  40. // New creates and returns a new goroutine pool object.
  41. // The parameter `limit` is used to limit the max goroutine count,
  42. // which is not limited in default.
  43. func New(limit ...int) *Pool {
  44. var (
  45. pool = &Pool{
  46. limit: -1,
  47. count: gtype.NewInt(),
  48. list: glist.New(true),
  49. closed: gtype.NewBool(),
  50. }
  51. timerDuration = grand.D(
  52. minSupervisorTimerDuration,
  53. maxSupervisorTimerDuration,
  54. )
  55. )
  56. if len(limit) > 0 && limit[0] > 0 {
  57. pool.limit = limit[0]
  58. }
  59. gtimer.Add(context.Background(), timerDuration, pool.supervisor)
  60. return pool
  61. }
  62. // Add pushes a new job to the default goroutine pool.
  63. // The job will be executed asynchronously.
  64. func Add(ctx context.Context, f Func) error {
  65. return defaultPool.Add(ctx, f)
  66. }
  67. // AddWithRecover pushes a new job to the default pool with specified recover function.
  68. //
  69. // The optional `recoverFunc` is called when any panic during executing of `userFunc`.
  70. // If `recoverFunc` is not passed or given nil, it ignores the panic from `userFunc`.
  71. // The job will be executed asynchronously.
  72. func AddWithRecover(ctx context.Context, userFunc Func, recoverFunc RecoverFunc) error {
  73. return defaultPool.AddWithRecover(ctx, userFunc, recoverFunc)
  74. }
  75. // Size returns current goroutine count of default goroutine pool.
  76. func Size() int {
  77. return defaultPool.Size()
  78. }
  79. // Jobs returns current job count of default goroutine pool.
  80. func Jobs() int {
  81. return defaultPool.Jobs()
  82. }