From 98dc238bac78c6cbbf128bc0df4ef3ad67ace1ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Wed, 8 Feb 2023 15:40:18 +0100 Subject: [PATCH] chore: use defer to unlock when possible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/email/summary/model.go | 2 +- pkg/email/violations/model.go | 8 ++++---- pkg/kubernetes/fixtures_test.go | 2 +- pkg/report/store.go | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/email/summary/model.go b/pkg/email/summary/model.go index c73205a7..eb68d609 100644 --- a/pkg/email/summary/model.go +++ b/pkg/email/summary/model.go @@ -33,6 +33,7 @@ func (s *Source) AddClusterSummary(sum v1alpha2.PolicyReportSummary) { func (s *Source) AddNamespacedSummary(ns string, sum v1alpha2.PolicyReportSummary) { s.mx.Lock() + defer s.mx.Unlock() if d, ok := s.NamespaceScopeSummary[ns]; ok { d.Skip += sum.Skip d.Pass += sum.Pass @@ -48,7 +49,6 @@ func (s *Source) AddNamespacedSummary(ns string, sum v1alpha2.PolicyReportSummar Error: sum.Error, } } - s.mx.Unlock() } func NewSource(name string, clusterReports bool) *Source { diff --git a/pkg/email/violations/model.go b/pkg/email/violations/model.go index cb5a5e98..a4522c64 100644 --- a/pkg/email/violations/model.go +++ b/pkg/email/violations/model.go @@ -58,8 +58,8 @@ type Source struct { func (s *Source) AddClusterResults(result []Result) { s.crMX.Lock() + defer s.crMX.Unlock() s.ClusterResults[result[0].Status] = append(s.ClusterResults[result[0].Status], result...) - s.crMX.Unlock() } func (s *Source) AddClusterPassed(results int) { @@ -68,12 +68,13 @@ func (s *Source) AddClusterPassed(results int) { func (s *Source) AddNamespacedPassed(ns string, results int) { s.passMX.Lock() + defer s.passMX.Unlock() s.NamespacePassed[ns] += results - s.passMX.Unlock() } func (s *Source) AddNamespacedResults(ns string, result []Result) { s.nrMX.Lock() + defer s.nrMX.Unlock() if nr, ok := s.NamespaceResults[ns]; ok { s.NamespaceResults[ns][result[0].Status] = append(nr[result[0].Status], result...) } else { @@ -85,11 +86,11 @@ func (s *Source) AddNamespacedResults(ns string, result []Result) { s.NamespaceResults[ns][result[0].Status] = result } - s.nrMX.Unlock() } func (s Source) InitResults(ns string) { s.nrMX.Lock() + defer s.nrMX.Unlock() if _, ok := s.NamespaceResults[ns]; !ok { s.NamespaceResults[ns] = map[string][]Result{ v1alpha2.StatusWarn: make([]Result, 0), @@ -97,7 +98,6 @@ func (s Source) InitResults(ns string) { v1alpha2.StatusError: make([]Result, 0), } } - s.nrMX.Unlock() } func NewSource(name string, clusterReports bool) *Source { diff --git a/pkg/kubernetes/fixtures_test.go b/pkg/kubernetes/fixtures_test.go index 6d417bf4..65f75fc3 100644 --- a/pkg/kubernetes/fixtures_test.go +++ b/pkg/kubernetes/fixtures_test.go @@ -22,8 +22,8 @@ type store struct { func (s *store) Add(r report.LifecycleEvent) { s.rwm.Lock() + defer s.rwm.Unlock() s.store = append(s.store, r) - s.rwm.Unlock() } func (s *store) Get(index int) report.LifecycleEvent { diff --git a/pkg/report/store.go b/pkg/report/store.go index 1519a658..5f48dcbc 100644 --- a/pkg/report/store.go +++ b/pkg/report/store.go @@ -48,16 +48,16 @@ func (s *policyReportStore) Get(id string) (v1alpha2.ReportInterface, bool) { func (s *policyReportStore) Add(r v1alpha2.ReportInterface) error { s.rwm.Lock() + defer s.rwm.Unlock() s.store[GetType(r)][r.GetID()] = r - s.rwm.Unlock() return nil } func (s *policyReportStore) Update(r v1alpha2.ReportInterface) error { s.rwm.Lock() + defer s.rwm.Unlock() s.store[GetType(r)][r.GetID()] = r - s.rwm.Unlock() return nil } @@ -65,8 +65,8 @@ func (s *policyReportStore) Update(r v1alpha2.ReportInterface) error { func (s *policyReportStore) Remove(id string) error { if r, ok := s.Get(id); ok { s.rwm.Lock() + defer s.rwm.Unlock() delete(s.store[GetType(r)], id) - s.rwm.Unlock() } return nil @@ -74,11 +74,11 @@ func (s *policyReportStore) Remove(id string) error { func (s *policyReportStore) CleanUp() error { s.rwm.Lock() + defer s.rwm.Unlock() s.store = map[ResourceType]map[string]v1alpha2.ReportInterface{ PolicyReportType: {}, ClusterPolicyReportType: {}, } - s.rwm.Unlock() return nil }