Skip to content

Commit

Permalink
upgrade mutex locking to use only RLock if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
catsby committed Sep 6, 2018
1 parent b01aaad commit 7b00112
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions builtin/logical/aws/secret_access_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ func (b *backend) secretTokenCreate(ctx context.Context, s logical.Storage,
displayName, policyName, policy string,
lifeTimeInSeconds int64) (*logical.Response, error) {

b.clientMutex.Lock()
defer b.clientMutex.Unlock()
b.clientMutex.RLock()
unlockFunc := b.clientMutex.RUnlock
defer func() { unlockFunc() }()
if b.stsClient == nil {
// Upgrade the lock for writing
b.clientMutex.RUnlock()
b.clientMutex.Lock()
unlockFunc = b.clientMutex.Unlock

stsClient, err := clientSTS(ctx, s)
if err != nil {
return logical.ErrorResponse(err.Error()), nil
Expand Down Expand Up @@ -174,9 +180,15 @@ func (b *backend) secretAccessKeysCreate(
s logical.Storage,
displayName, policyName string, role *awsRoleEntry) (*logical.Response, error) {

b.clientMutex.Lock()
defer b.clientMutex.Unlock()
b.clientMutex.RLock()
unlockFunc := b.clientMutex.RUnlock
defer func() { unlockFunc() }()
if b.iamClient == nil {
// Upgrade the lock for writing
b.clientMutex.RUnlock()
b.clientMutex.Lock()
unlockFunc = b.clientMutex.Unlock

iamClient, err := clientIAM(ctx, s)
if err != nil {
return logical.ErrorResponse(err.Error()), nil
Expand Down

0 comments on commit 7b00112

Please sign in to comment.