Lock Only When Needed When Fetching Attesting History (#8140)

* lock when needed in attesting history

* rw
This commit is contained in:
Raul Jordan 2020-12-16 18:18:38 -06:00 committed by GitHub
parent a7cf77fc26
commit d650034734
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 3 deletions

View File

@ -34,11 +34,12 @@ func (store *Store) AttestationHistoryForPubKeyV2(ctx context.Context, publicKey
ctx, span := trace.StartSpan(ctx, "Validator.AttestationHistoryForPubKeyV2")
defer span.End()
if !featureconfig.Get().DisableAttestingHistoryDBCache {
store.lock.Lock()
defer store.lock.Unlock()
store.lock.RLock()
if history, ok := store.attestingHistoriesByPubKey[publicKey]; ok {
store.lock.RUnlock()
return history, nil
}
store.lock.RUnlock()
}
var err error
var attestationHistory EncHistoryData
@ -57,7 +58,9 @@ func (store *Store) AttestationHistoryForPubKeyV2(ctx context.Context, publicKey
return nil
})
if !featureconfig.Get().DisableAttestingHistoryDBCache {
store.lock.Lock()
store.attestingHistoriesByPubKey[publicKey] = attestationHistory
store.lock.Unlock()
}
return attestationHistory, err
}

View File

@ -24,7 +24,7 @@ var ProtectionDbFileName = "validator.db"
type Store struct {
db *bolt.DB
databasePath string
lock sync.Mutex
lock sync.RWMutex
attestingHistoriesByPubKey map[[48]byte]EncHistoryData
}