generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix client caching #54
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,26 +27,32 @@ const ( | |
) | ||
|
||
type organizationClient struct { | ||
url string | ||
username string | ||
password string | ||
organizationName string | ||
client cfclient.Client | ||
} | ||
|
||
type clientIdentifier struct { | ||
URL string | ||
Username string | ||
} | ||
|
||
type spaceClient struct { | ||
url string | ||
username string | ||
password string | ||
spaceGuid string | ||
client cfclient.Client | ||
} | ||
|
||
type clientIdentifier struct { | ||
url string | ||
username string | ||
} | ||
|
||
type clientCacheEntry struct { | ||
url string | ||
username string | ||
password string | ||
client cfclient.Client | ||
} | ||
|
||
var ( | ||
cacheMutex = &sync.Mutex{} | ||
clientCache = make(map[clientIdentifier]*clientCacheEntry) | ||
) | ||
|
||
func newOrganizationClient(organizationName string, url string, username string, password string) (*organizationClient, error) { | ||
if organizationName == "" { | ||
return nil, fmt.Errorf("missing or empty organization name") | ||
|
@@ -68,7 +74,7 @@ func newOrganizationClient(organizationName string, url string, username string, | |
if err != nil { | ||
return nil, err | ||
} | ||
return &organizationClient{url: url, username: username, password: password, organizationName: organizationName, client: *c}, nil | ||
return &organizationClient{organizationName: organizationName, client: *c}, nil | ||
} | ||
|
||
func newSpaceClient(spaceGuid string, url string, username string, password string) (*spaceClient, error) { | ||
|
@@ -92,71 +98,101 @@ func newSpaceClient(spaceGuid string, url string, username string, password stri | |
if err != nil { | ||
return nil, err | ||
} | ||
return &spaceClient{url: url, username: username, password: password, spaceGuid: spaceGuid, client: *c}, nil | ||
return &spaceClient{spaceGuid: spaceGuid, client: *c}, nil | ||
} | ||
|
||
var ( | ||
spaceClientCache = make(map[clientIdentifier]*spaceClient) | ||
orgClientCache = make(map[clientIdentifier]*organizationClient) | ||
cacheMutex = &sync.Mutex{} | ||
) | ||
|
||
func NewOrganizationClient(organizationName string, url string, username string, password string) (facade.OrganizationClient, error) { | ||
cacheMutex.Lock() | ||
defer cacheMutex.Unlock() | ||
identifier := clientIdentifier{URL: url, Username: username} | ||
client, cached := orgClientCache[identifier] | ||
var err error | ||
if !cached { | ||
|
||
// look up CF client in cache | ||
identifier := clientIdentifier{url: url, username: username} | ||
cacheEntry, isInCache := clientCache[identifier] | ||
|
||
var err error = nil | ||
var client *organizationClient = nil | ||
if isInCache { | ||
// re-use CF client and wrap it as organizationClient | ||
client = &organizationClient{organizationName: organizationName, client: cacheEntry.client} | ||
if cacheEntry.password != password { | ||
// password was rotated => delete client from cache and create a new one below | ||
delete(clientCache, identifier) | ||
isInCache = false | ||
} | ||
} | ||
|
||
if !isInCache { | ||
// create new CF client and wrap it as organizationClient | ||
client, err = newOrganizationClient(organizationName, url, username, password) | ||
if err == nil { | ||
orgClientCache[identifier] = client | ||
} | ||
} else { | ||
// If the password has changed since we cached the client, we want to update it to the new one | ||
if client.password != password { | ||
client.password = password | ||
// add CF client to cache | ||
clientCache[identifier] = &clientCacheEntry{url: url, username: username, password: password, client: client.client} | ||
} | ||
} | ||
|
||
return client, err | ||
} | ||
|
||
func NewSpaceClient(spaceGuid string, url string, username string, password string) (facade.SpaceClient, error) { | ||
cacheMutex.Lock() | ||
defer cacheMutex.Unlock() | ||
identifier := clientIdentifier{URL: url, Username: username} | ||
client, cached := spaceClientCache[identifier] | ||
var err error | ||
if !cached { | ||
|
||
// look up CF client in cache | ||
identifier := clientIdentifier{url: url, username: username} | ||
cacheEntry, isInCache := clientCache[identifier] | ||
|
||
var err error = nil | ||
var client *spaceClient = nil | ||
if isInCache { | ||
// re-use CF client from cache and wrap it as spaceClient | ||
client = &spaceClient{spaceGuid: spaceGuid, client: cacheEntry.client} | ||
if cacheEntry.password != password { | ||
// password was rotated => delete client from cache and create a new one below | ||
delete(clientCache, identifier) | ||
isInCache = false | ||
} | ||
} | ||
|
||
if !isInCache { | ||
// create new CF client and wrap it as spaceClient | ||
client, err = newSpaceClient(spaceGuid, url, username, password) | ||
if err == nil { | ||
spaceClientCache[identifier] = client | ||
} | ||
} else { | ||
// If the password has changed since we cached the client, we want to update it to the new one | ||
if client.password != password { | ||
client.password = password | ||
// add CF client to cache | ||
clientCache[identifier] = &clientCacheEntry{url: url, username: username, password: password, client: client.client} | ||
Comment on lines
+140
to
+161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now I think we should merge this, but later think about factoring out this part. |
||
} | ||
} | ||
|
||
return client, err | ||
} | ||
|
||
func NewSpaceHealthChecker(spaceGuid string, url string, username string, password string) (facade.SpaceHealthChecker, error) { | ||
cacheMutex.Lock() | ||
defer cacheMutex.Unlock() | ||
identifier := clientIdentifier{URL: url, Username: username} | ||
client, cached := spaceClientCache[identifier] | ||
var err error | ||
if !cached { | ||
|
||
// look up CF client in cache | ||
identifier := clientIdentifier{url: url, username: username} | ||
cacheEntry, isInCache := clientCache[identifier] | ||
|
||
var err error = nil | ||
var client *spaceClient = nil | ||
if isInCache { | ||
// re-use CF client from cache and wrap it as spaceClient | ||
client = &spaceClient{spaceGuid: spaceGuid, client: cacheEntry.client} | ||
if cacheEntry.password != password { | ||
// password was rotated => delete client from cache and create a new one below | ||
delete(clientCache, identifier) | ||
isInCache = false | ||
} | ||
} | ||
|
||
if !isInCache { | ||
// create new CF client and wrap it as spaceClient | ||
client, err = newSpaceClient(spaceGuid, url, username, password) | ||
if err == nil { | ||
spaceClientCache[identifier] = client | ||
} | ||
} else { | ||
// If the password has changed since we cached the client, we want to update it to the new one | ||
if client.password != password { | ||
client.password = password | ||
// add CF client to cache | ||
clientCache[identifier] = &clientCacheEntry{url: url, username: username, password: password, client: client.client} | ||
} | ||
} | ||
|
||
return client, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a little bit of an odd way of doing it, but it should work.