Skip to content

Commit

Permalink
Modify StringSet.Delete to return if found
Browse files Browse the repository at this point in the history
Encapsulates this usage:

  if set.Contains("foo") {
      set.Delete("foo")
      ...
  }

to be atomic with slight efficiency gain.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis authored and mangelajo committed Apr 2, 2020
1 parent 50f6e09 commit b2bbc49
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 1 addition & 2 deletions pkg/routeagent/controllers/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,7 @@ func (r *Controller) handleRemovedPod(obj interface{}) {
klog.V(log.DEBUG).Infof("Removing podIP in route controller %v", obj)
pod := obj.(*k8sv1.Pod)

if r.remoteVTEPs.Contains(pod.Status.HostIP) {
r.remoteVTEPs.Delete(pod.Status.HostIP)
if r.remoteVTEPs.Delete(pod.Status.HostIP) {
r.gwVxLanMutex.Lock()
defer r.gwVxLanMutex.Unlock()

Expand Down
4 changes: 3 additions & 1 deletion pkg/util/stringset.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ func (set *StringSet) Size() int {
return len(set.set)
}

func (set *StringSet) Delete(s string) {
func (set *StringSet) Delete(s string) bool {
set.syncMutex.Lock()
defer set.syncMutex.Unlock()

_, found := set.set[s]
delete(set.set, s)
return found
}

func (set *StringSet) Elements() []string {
Expand Down
12 changes: 9 additions & 3 deletions pkg/util/stringset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ var _ = Describe("StringSet", func() {
})
})

When("a string is deleted", func() {
It("should no longer be observed in the set", func() {
set.Delete("192.168.2.0/24")
When("an existing string is deleted", func() {
It("should return true and no longer be observed in the set", func() {
Expect(set.Delete("192.168.2.0/24")).To(BeTrue())
Expect(set.Contains("192.168.2.0/24")).To(BeFalse())
Expect(set.Size()).To(Equal(1))
})
})

When("a non-existent string is deleted", func() {
It("should return false", func() {
Expect(set.Delete("192.168.5.0/24")).To(BeFalse())
})
})

When("a string is re-added", func() {
It("should be observed in the set", func() {
set.Delete("192.168.2.0/24")
Expand Down

0 comments on commit b2bbc49

Please sign in to comment.