Skip to content

Commit

Permalink
used RWMutex instead of Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
shriramsharma committed Jun 8, 2022
1 parent 69a4639 commit 4153b5d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions admiral/pkg/controller/admiral/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type ServiceController struct {
type serviceCache struct {
//map of dependencies key=identity value array of onboarded identities
cache map[string]*ServiceClusterEntry
mutex *sync.Mutex
mutex *sync.RWMutex
}

func (s *serviceCache) Put(service *k8sV1.Service) {
Expand Down Expand Up @@ -71,8 +71,8 @@ func (s *serviceCache) getKey(service *k8sV1.Service) string {
}

func (s *serviceCache) Get(key string) *ServiceClusterEntry {
s.mutex.Lock()
defer s.mutex.Unlock()
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.cache[key]
}

Expand Down Expand Up @@ -121,7 +121,7 @@ func NewServiceController(stopCh <-chan struct{}, handler ServiceHandler, config

podCache := serviceCache{}
podCache.cache = make(map[string]*ServiceClusterEntry)
podCache.mutex = &sync.Mutex{}
podCache.mutex = &sync.RWMutex{}

serviceController.Cache = &podCache
var err error
Expand Down
6 changes: 3 additions & 3 deletions admiral/pkg/controller/admiral/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestNewServiceController(t *testing.T) {
func TestServiceCache_Put(t *testing.T) {
serviceCache := serviceCache{}
serviceCache.cache = make(map[string]*ServiceClusterEntry)
serviceCache.mutex = &sync.Mutex{}
serviceCache.mutex = &sync.RWMutex{}

//test service cache empty with admiral ignore should skip to save in cache

Expand Down Expand Up @@ -93,7 +93,7 @@ func TestServiceCache_Put(t *testing.T) {
func TestServiceCache_GetLoadBalancer(t *testing.T) {
sc := serviceCache{}
sc.cache = make(map[string]*ServiceClusterEntry)
sc.mutex = &sync.Mutex{}
sc.mutex = &sync.RWMutex{}

service := &v1.Service{}
service.Name = "test-service"
Expand Down Expand Up @@ -246,7 +246,7 @@ func TestServiceCache_GetLoadBalancer(t *testing.T) {
func TestConcurrentGetAndPut(t *testing.T) {
serviceCache := serviceCache{}
serviceCache.cache = make(map[string]*ServiceClusterEntry)
serviceCache.mutex = &sync.Mutex{}
serviceCache.mutex = &sync.RWMutex{}

serviceCache.Put(&v1.Service{
ObjectMeta: metaV1.ObjectMeta{Name: "testname", Namespace: "testns"},
Expand Down

0 comments on commit 4153b5d

Please sign in to comment.