Skip to content

Commit

Permalink
attach client cache to the connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
tedchamb committed Jul 23, 2019
1 parent e97a841 commit dbaefad
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions azureDevops/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Connection struct {
SuppressFedAuthRedirect bool
ForceMsaPassThrough bool
Timeout *time.Duration
clientCache map[string] *Client
clientCacheLock sync.RWMutex
}

func CreateBasicAuthHeaderValue(username, password string) string {
Expand Down Expand Up @@ -65,10 +67,10 @@ func (connection Connection) GetClientByResourceAreaId(ctx context.Context, reso

func (connection Connection) GetClientByUrl(baseUrl string) *Client {
normalizedUrl := normalizeUrl(baseUrl)
azureDevOpsClient, ok := getClientCacheEntry(normalizedUrl)
azureDevOpsClient, ok := connection.getClientCacheEntry(normalizedUrl)
if !ok {
azureDevOpsClient = NewClient(connection, normalizedUrl)
setClientCacheEntry(normalizedUrl, azureDevOpsClient)
connection.setClientCacheEntry(normalizedUrl, azureDevOpsClient)
}
return azureDevOpsClient
}
Expand Down Expand Up @@ -101,20 +103,23 @@ func (connection Connection) getResourceAreaInfo(ctx context.Context, resourceAr
}

// Client Cache by Url
var clientCache = make(map[string] *Client)
var clientCacheLock = sync.RWMutex{}

func getClientCacheEntry(url string) (*Client, bool) {
clientCacheLock.RLock()
defer clientCacheLock.RUnlock()
locationsMap, ok := clientCache[url]
func (connection Connection) getClientCacheEntry(url string) (*Client, bool) {
if connection.clientCache == nil {
return nil, false
}
connection.clientCacheLock.RLock()
defer connection.clientCacheLock.RUnlock()
locationsMap, ok := connection.clientCache[url]
return locationsMap, ok
}

func setClientCacheEntry(url string, client *Client) {
clientCacheLock.Lock()
defer clientCacheLock.Unlock()
clientCache[url] = client
func (connection Connection) setClientCacheEntry(url string, client *Client) {
connection.clientCacheLock.Lock()
defer connection.clientCacheLock.Unlock()
if connection.clientCache == nil {
connection.clientCache = make(map[string] *Client)
}
connection.clientCache[url] = client
}

// Resource Area Cache by Url
Expand Down

0 comments on commit dbaefad

Please sign in to comment.