image_concurrency_limiter.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package handler
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. )
  7. type imageConcurrencyLimiter struct {
  8. mu sync.Mutex
  9. notify chan struct{}
  10. limit int
  11. active int
  12. waiting int
  13. enabled bool
  14. }
  15. func (l *imageConcurrencyLimiter) TryAcquire(enabled bool, limit int) (func(), bool) {
  16. return l.acquire(context.Background(), enabled, limit, false, 0, 0)
  17. }
  18. func (l *imageConcurrencyLimiter) Acquire(ctx context.Context, enabled bool, limit int, wait bool, timeout time.Duration, maxWaiting int) (func(), bool) {
  19. return l.acquire(ctx, enabled, limit, wait, timeout, maxWaiting)
  20. }
  21. func (l *imageConcurrencyLimiter) acquire(ctx context.Context, enabled bool, limit int, wait bool, timeout time.Duration, maxWaiting int) (func(), bool) {
  22. if !enabled || limit <= 0 {
  23. return nil, true
  24. }
  25. if ctx == nil {
  26. ctx = context.Background()
  27. }
  28. if wait {
  29. if timeout <= 0 {
  30. return nil, false
  31. }
  32. waitCtx, cancel := context.WithTimeout(ctx, timeout)
  33. defer cancel()
  34. ctx = waitCtx
  35. }
  36. if maxWaiting < 0 {
  37. maxWaiting = 0
  38. }
  39. for {
  40. release, acquired, waitRelease, notify := l.tryAcquireLocked(enabled, limit, wait, maxWaiting)
  41. if acquired {
  42. return release, acquired
  43. }
  44. if !wait || notify == nil {
  45. return nil, false
  46. }
  47. if !l.waitForSlot(ctx, notify) {
  48. if waitRelease != nil {
  49. waitRelease()
  50. }
  51. return nil, false
  52. }
  53. if waitRelease != nil {
  54. waitRelease()
  55. }
  56. }
  57. }
  58. func (l *imageConcurrencyLimiter) tryAcquireLocked(enabled bool, limit int, wait bool, maxWaiting int) (func(), bool, func(), <-chan struct{}) {
  59. l.mu.Lock()
  60. defer l.mu.Unlock()
  61. if l.notify == nil {
  62. l.notify = make(chan struct{})
  63. }
  64. if l.enabled != enabled || l.limit != limit {
  65. l.enabled = enabled
  66. l.limit = limit
  67. }
  68. if l.active < l.limit {
  69. l.active++
  70. return l.releaseFunc(), true, nil, nil
  71. }
  72. if !wait {
  73. return nil, false, nil, nil
  74. }
  75. if maxWaiting > 0 && l.waiting >= maxWaiting {
  76. return nil, false, nil, nil
  77. }
  78. l.waiting++
  79. return nil, false, l.waiterReleaseFunc(), l.notify
  80. }
  81. func (l *imageConcurrencyLimiter) waitForSlot(ctx context.Context, notify <-chan struct{}) bool {
  82. select {
  83. case <-notify:
  84. return true
  85. case <-ctx.Done():
  86. return false
  87. }
  88. }
  89. func (l *imageConcurrencyLimiter) releaseFunc() func() {
  90. var once sync.Once
  91. return func() {
  92. once.Do(func() {
  93. l.mu.Lock()
  94. if l.active > 0 {
  95. l.active--
  96. }
  97. if l.notify != nil {
  98. close(l.notify)
  99. l.notify = make(chan struct{})
  100. }
  101. l.mu.Unlock()
  102. })
  103. }
  104. }
  105. func (l *imageConcurrencyLimiter) waiterReleaseFunc() func() {
  106. var once sync.Once
  107. return func() {
  108. once.Do(func() {
  109. l.mu.Lock()
  110. if l.waiting > 0 {
  111. l.waiting--
  112. }
  113. l.mu.Unlock()
  114. })
  115. }
  116. }