Skip to content

Commit

Permalink
[Sets] Add Read lock to new Union and ShallowCopy (#564)
Browse files Browse the repository at this point in the history
* add read locks to shallow copy and unionSortedSet

* add changelog
  • Loading branch information
saiskee committed May 20, 2024
1 parent 4022e6c commit 54489e5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changelog/v0.40.1/sets-union-addRlock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
changelog:
- type: NON_USER_FACING
description: >
"Adds Read Lock to ShallowCopy and Union methods on sets"
skipCI: "false"
4 changes: 4 additions & 0 deletions contrib/pkg/sets/v2/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ func (s *resourceSet[T]) Union(set ResourceSet[T]) ResourceSet[T] {
// Assuming that the argument set is sorted by resource id,
// this method will efficiently union the two sets together and return the unioned set.
func (s *resourceSet[T]) unionSortedSet(set ResourceSet[T]) *resourceSet[T] {
s.lock.RLock()
defer s.lock.RUnlock()
merged := make([]T, 0, len(s.set)+set.Len())
idx := 0

Expand Down Expand Up @@ -373,6 +375,8 @@ func (oldSet *resourceSet[T]) Clone() ResourceSet[T] {
}

func (oldSet *resourceSet[T]) ShallowCopy() ResourceSet[T] {
oldSet.lock.RLock()
defer oldSet.lock.RUnlock()
newSet := make([]T, len(oldSet.set))
copy(newSet, oldSet.set)
return &resourceSet[T]{
Expand Down

0 comments on commit 54489e5

Please sign in to comment.