changes to ensure that unlock is called only after lock

This commit is contained in:
vsmk98 2019-02-13 09:57:11 +08:00
parent 05b17e3ab4
commit 0b6e64f250
1 changed files with 11 additions and 2 deletions

View File

@ -35,13 +35,18 @@ var OrgKeyMap, _ = lru.New(orgKeyMapLimit)
var orgKeyLock sync.Mutex var orgKeyLock sync.Mutex
// sets default access to ReadOnly
func SetDefaultAccess() { func SetDefaultAccess() {
DefaultAccess = ReadOnly DefaultAccess = ReadOnly
} }
// Adds account access to the cache
func AddAccountAccess(acctId common.Address, access uint8) { func AddAccountAccess(acctId common.Address, access uint8) {
AcctMap.Add(acctId, &PermStruct{AcctId: acctId, AcctAccess: AccessType(access)}) AcctMap.Add(acctId, &PermStruct{AcctId: acctId, AcctAccess: AccessType(access)})
} }
// Returns the access type for an account. If not found returns
// default access
func GetAcctAccess(acctId common.Address) AccessType { func GetAcctAccess(acctId common.Address) AccessType {
if AcctMap.Len() != 0 { if AcctMap.Len() != 0 {
if val, ok := AcctMap.Get(acctId); ok { if val, ok := AcctMap.Get(acctId); ok {
@ -52,24 +57,27 @@ func GetAcctAccess(acctId common.Address) AccessType {
return DefaultAccess return DefaultAccess
} }
// Adds org key details to cache
func AddOrgKey(orgId string, key string) { func AddOrgKey(orgId string, key string) {
if OrgKeyMap.Len() != 0 { if OrgKeyMap.Len() != 0 {
if val, ok := OrgKeyMap.Get(orgId); ok { if val, ok := OrgKeyMap.Get(orgId); ok {
orgKeyLock.Lock() orgKeyLock.Lock()
defer orgKeyLock.Unlock()
// Org record exists. Append the key only // Org record exists. Append the key only
vo := val.(*OrgStruct) vo := val.(*OrgStruct)
vo.Keys = append(vo.Keys, key) vo.Keys = append(vo.Keys, key)
orgKeyLock.Unlock()
return return
} }
} }
OrgKeyMap.Add(orgId, &OrgStruct{OrgId: orgId, Keys: []string{key}}) OrgKeyMap.Add(orgId, &OrgStruct{OrgId: orgId, Keys: []string{key}})
} }
// deletes org key details from cache
func DeleteOrgKey(orgId string, key string) { func DeleteOrgKey(orgId string, key string) {
defer orgKeyLock.Unlock()
if val, ok := OrgKeyMap.Get(orgId); ok { if val, ok := OrgKeyMap.Get(orgId); ok {
orgKeyLock.Lock() orgKeyLock.Lock()
defer orgKeyLock.Unlock()
vo := val.(*OrgStruct) vo := val.(*OrgStruct)
for i, keyVal := range vo.Keys { for i, keyVal := range vo.Keys {
if keyVal == key { if keyVal == key {
@ -80,6 +88,7 @@ func DeleteOrgKey(orgId string, key string) {
} }
} }
// Givens a orgid returns the linked keys for the org
func ResolvePrivateForKeys(orgId string) []string { func ResolvePrivateForKeys(orgId string) []string {
var keys []string var keys []string
if val, ok := OrgKeyMap.Get(orgId); ok { if val, ok := OrgKeyMap.Get(orgId); ok {