Kaynağa Gözat

Addition for Clear()

Matt Bolt 4 yıl önce
ebeveyn
işleme
a98e2594a4
1 değiştirilmiş dosya ile 15 ekleme ve 0 silme
  1. 15 0
      pkg/collections/blockingqueue.go

+ 15 - 0
pkg/collections/blockingqueue.go

@@ -30,6 +30,9 @@ type BlockingQueue interface {
 
 	// IsEmpty returns true if the queue is empty
 	IsEmpty() bool
+
+	// Clear empties the queue
+	Clear()
 }
 
 // blockingSliceQueue is an implementation of BlockingQueue which uses a slice for storage.
@@ -119,3 +122,15 @@ func (q *blockingSliceQueue) Length() int {
 func (q *blockingSliceQueue) IsEmpty() bool {
 	return q.Length() == 0
 }
+
+// Clear empties the queue
+func (q *blockingSliceQueue) Clear() {
+	q.l.Lock()
+	defer q.l.Unlock()
+
+	// seems optimal here to create a new underlying slice/array to
+	// avoid capacity ballooning, but does feel like an implementation
+	// specific detail -- we can revisit if there are other relevant
+	// use-cases
+	q.q = []interface{}{}
+}