Skip to content
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

Fixed potential race condition in cachedKeyService #1198

Merged
merged 1 commit into from
Jul 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions signer/keydbstore/cachedcryptoservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type cachedKeyService struct {
signed.CryptoService
lock *sync.Mutex
lock *sync.RWMutex
cachedKeys map[string]*cachedKey
}

Expand All @@ -22,7 +22,7 @@ type cachedKey struct {
func NewCachedKeyService(baseKeyService signed.CryptoService) signed.CryptoService {
return &cachedKeyService{
CryptoService: baseKeyService,
lock: &sync.Mutex{},
lock: &sync.RWMutex{},
cachedKeys: make(map[string]*cachedKey),
}
}
Expand All @@ -47,7 +47,9 @@ func (s *cachedKeyService) AddKey(role data.RoleName, gun data.GUN, privKey data

// GetKey returns the PrivateKey given a KeyID
func (s *cachedKeyService) GetPrivateKey(keyID string) (data.PrivateKey, data.RoleName, error) {
s.lock.RLock()
cachedKeyEntry, ok := s.cachedKeys[keyID]
s.lock.RUnlock()
if ok {
return cachedKeyEntry.key, cachedKeyEntry.role, nil
}
Expand Down