Skip to content

Commit

Permalink
ISSUE-146 in use a mutex before writing to or reading spec map
Browse files Browse the repository at this point in the history
  • Loading branch information
jcollins-axway committed Sep 25, 2023
1 parent b9f1c60 commit aacd207
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions discovery/pkg/apigee/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package apigee
import (
"fmt"
"strings"
"sync"
"time"

"github.com/Axway/agent-sdk/pkg/apic"
Expand All @@ -12,6 +13,7 @@ import (
type agentCache struct {
cache cache.Cache
specEndpointToKeys map[string][]specCacheItem
mutex *sync.Mutex
}

type specCacheItem struct {
Expand All @@ -25,6 +27,7 @@ func newAgentCache() *agentCache {
return &agentCache{
cache: cache.New(),
specEndpointToKeys: make(map[string][]specCacheItem),
mutex: &sync.Mutex{},
}
}

Expand All @@ -43,6 +46,8 @@ func (a *agentCache) AddSpecToCache(id, path, name string, modDate time.Time, en
a.cache.SetWithSecondaryKey(specPrimaryKey(name), path, item)
a.cache.SetSecondaryKey(specPrimaryKey(name), strings.ToLower(name))
a.cache.SetSecondaryKey(specPrimaryKey(name), id)
a.mutex.Lock()
defer a.mutex.Unlock()
for _, ep := range endpoints {
if _, found := a.specEndpointToKeys[ep]; !found {
a.specEndpointToKeys[ep] = []specCacheItem{}
Expand Down Expand Up @@ -90,6 +95,8 @@ func (a *agentCache) GetSpecWithName(name string) (*specCacheItem, error) {

// GetSpecPathWithEndpoint - returns the lat modified spec found with this endpoint
func (a *agentCache) GetSpecPathWithEndpoint(endpoint string) (string, error) {
a.mutex.Lock()
defer a.mutex.Unlock()
items, found := a.specEndpointToKeys[endpoint]
if !found {
return "", fmt.Errorf("no spec found for endpoint: %s", endpoint)
Expand Down

0 comments on commit aacd207

Please sign in to comment.