Просмотр исходного кода

NewSummaryAllocationSet should _not_ Clone AllocationProperties, and add proper locking around Each()

Matt Bolt 4 лет назад
Родитель
Сommit
385c3d17a2
2 измененных файлов с 7 добавлено и 1 удалено
  1. 6 0
      pkg/kubecost/allocation.go
  2. 1 1
      pkg/kubecost/summaryallocation.go

+ 6 - 0
pkg/kubecost/allocation.go

@@ -1691,10 +1691,16 @@ func (as *AllocationSet) Delete(name string) {
 
 // Each invokes the given function for each Allocation in the set
 func (as *AllocationSet) Each(f func(string, *Allocation)) {
+	as.RLock()
+	defer as.RUnlock()
+
 	if as == nil {
 		return
 	}
 
+	// note: if we find that f() is causing heavy contention and blocking
+	// note: writes, we can hold a read lock while we copy the allocations,
+	// note: then execute the each over the copied map
 	for k, a := range as.allocations {
 		f(k, a)
 	}

+ 1 - 1
pkg/kubecost/summaryallocation.go

@@ -49,7 +49,7 @@ func NewSummaryAllocation(alloc *Allocation, reconcile, reconcileNetwork bool) *
 
 	sa := &SummaryAllocation{
 		Name:                   alloc.Name,
-		Properties:             alloc.Properties.Clone(),
+		Properties:             alloc.Properties,
 		Start:                  alloc.Start,
 		End:                    alloc.End,
 		CPUCoreRequestAverage:  alloc.CPUCoreRequestAverage,