From b2bbc49dce93a10ada7e63ec12731127a1eed347 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Thu, 27 Feb 2020 13:02:03 -0500 Subject: [PATCH] Modify StringSet.Delete to return if found Encapsulates this usage: if set.Contains("foo") { set.Delete("foo") ... } to be atomic with slight efficiency gain. Signed-off-by: Tom Pantelis --- pkg/routeagent/controllers/route/route.go | 3 +-- pkg/util/stringset.go | 4 +++- pkg/util/stringset_test.go | 12 +++++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/routeagent/controllers/route/route.go b/pkg/routeagent/controllers/route/route.go index 2c3f03ba6..e90a50dd9 100644 --- a/pkg/routeagent/controllers/route/route.go +++ b/pkg/routeagent/controllers/route/route.go @@ -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() diff --git a/pkg/util/stringset.go b/pkg/util/stringset.go index 69c4754aa..696614223 100644 --- a/pkg/util/stringset.go +++ b/pkg/util/stringset.go @@ -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 { diff --git a/pkg/util/stringset_test.go b/pkg/util/stringset_test.go index a6effd8da..b0fc80e97 100644 --- a/pkg/util/stringset_test.go +++ b/pkg/util/stringset_test.go @@ -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")