Queue From Kubernetes

Source Code

  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package workqueue
  14. import (
  15. "sync"
  16. )
  17. type Interface interface {
  18. Add(item interface{})
  19. Len() int
  20. Get() (item interface{}, shutdown bool)
  21. Done(item interface{})
  22. ShutDown()
  23. ShuttingDown() bool
  24. }
  25. // New constructs a new work queue (see the package comment).
  26. func New() *Type {
  27. return NewNamed("")
  28. }
  29. func NewNamed(name string) *Type {
  30. return &Type{
  31. dirty: set{},
  32. processing: set{},
  33. cond: sync.NewCond(&sync.Mutex{}), // [1] 需要配合 Mutex 使用
  34. metrics: newQueueMetrics(name),
  35. }
  36. }
  37. // Type is a work queue (see the package comment).
  38. type Type struct {
  39. // queue defines the order in which we will work on items. Every
  40. // element of queue should be in the dirty set and not in the
  41. // processing set.
  42. queue []t
  43. // dirty defines all of the items that need to be processed.
  44. dirty set
  45. // Things that are currently being processed are in the processing set.
  46. // These things may be simultaneously in the dirty set. When we finish
  47. // processing something and remove it from this set, we'll check if
  48. // it's in the dirty set, and if so, add it to the queue.
  49. processing set
  50. cond *sync.Cond
  51. shuttingDown bool
  52. metrics queueMetrics
  53. }
  54. type empty struct{}
  55. type t interface{}
  56. type set map[t]empty
  57. func (s set) has(item t) bool {
  58. _, exists := s[item]
  59. return exists
  60. }
  61. func (s set) insert(item t) {
  62. s[item] = empty{}
  63. }
  64. func (s set) delete(item t) {
  65. delete(s, item)
  66. }
  67. // Add marks item as needing processing.
  68. func (q *Type) Add(item interface{}) {
  69. q.cond.L.Lock() // [2]
  70. defer q.cond.L.Unlock()
  71. if q.shuttingDown {
  72. return
  73. }
  74. if q.dirty.has(item) {
  75. return
  76. }
  77. q.metrics.add(item)
  78. q.dirty.insert(item)
  79. if q.processing.has(item) {
  80. return
  81. }
  82. q.queue = append(q.queue, item)
  83. q.cond.Signal() // [3]
  84. }
  85. // Len returns the current queue length, for informational purposes only. You
  86. // shouldn't e.g. gate a call to Add() or Get() on Len() being a particular
  87. // value, that can't be synchronized properly.
  88. func (q *Type) Len() int {
  89. q.cond.L.Lock()
  90. defer q.cond.L.Unlock()
  91. return len(q.queue)
  92. }
  93. // Get blocks until it can return an item to be processed. If shutdown = true,
  94. // the caller should end their goroutine. You must call Done with item when you
  95. // have finished processing it.
  96. func (q *Type) Get() (item interface{}, shutdown bool) {
  97. q.cond.L.Lock()
  98. defer q.cond.L.Unlock()
  99. for len(q.queue) == 0 && !q.shuttingDown {
  100. q.cond.Wait() //[4]
  101. }
  102. if len(q.queue) == 0 {
  103. // We must be shutting down.
  104. return nil, true
  105. }
  106. item, q.queue = q.queue[0], q.queue[1:]
  107. q.metrics.get(item)
  108. q.processing.insert(item)
  109. q.dirty.delete(item)
  110. return item, false
  111. }
  112. // Done marks item as done processing, and if it has been marked as dirty again
  113. // while it was being processed, it will be re-added to the queue for
  114. // re-processing.
  115. func (q *Type) Done(item interface{}) {
  116. q.cond.L.Lock()
  117. defer q.cond.L.Unlock()
  118. q.metrics.done(item)
  119. q.processing.delete(item)
  120. if q.dirty.has(item) {
  121. q.queue = append(q.queue, item)
  122. q.cond.Signal()
  123. }
  124. }
  125. // ShutDown will cause q to ignore all new items added to it. As soon as the
  126. // worker goroutines have drained the existing items in the queue, they will be
  127. // instructed to exit.
  128. func (q *Type) ShutDown() {
  129. q.cond.L.Lock()
  130. defer q.cond.L.Unlock()
  131. q.shuttingDown = true
  132. q.cond.Broadcast() // [5]
  133. }
  134. func (q *Type) ShuttingDown() bool {
  135. q.cond.L.Lock()
  136. defer q.cond.L.Unlock()
  137. return q.shuttingDown
  138. }

Cond 使用总结

  • 需要配合 sync.Locker 接口使用,在本例中使用了 sync.Mutex

  • 操作 Cond 前,需要加锁(同一个锁)

  • Signal、Broadcast、Wait 方法