improve concurrency story of manifest state

This commit is contained in:
vyzo 2022-04-21 13:53:30 +03:00
parent 1366901ded
commit 75f00e92f5

View File

@ -25,10 +25,7 @@ var manifests map[Version]*manifest.Manifest
var actorMeta map[cid.Cid]actorEntry var actorMeta map[cid.Cid]actorEntry
var ( var (
loadOnce sync.Once manifestMx sync.RWMutex
loadError error
manifestMx sync.Mutex
) )
type actorEntry struct { type actorEntry struct {
@ -44,17 +41,18 @@ func AddManifest(av Version, manifestCid cid.Cid) {
} }
func GetManifest(av Version) (cid.Cid, bool) { func GetManifest(av Version) (cid.Cid, bool) {
manifestMx.Lock() manifestMx.RLock()
defer manifestMx.Unlock() defer manifestMx.RUnlock()
c, ok := manifestCids[av] c, ok := manifestCids[av]
return c, ok return c, ok
} }
func LoadManifests(ctx context.Context, store cbor.IpldStore) error { func LoadManifests(ctx context.Context, store cbor.IpldStore) error {
// tests may invoke this concurrently, so we wrap it in a sync.Once manifestMx.Lock()
loadOnce.Do(func() { loadError = loadManifests(ctx, store) }) defer manifestMx.Unlock()
return loadError
return loadManifests(ctx, store)
} }
func loadManifests(ctx context.Context, store cbor.IpldStore) error { func loadManifests(ctx context.Context, store cbor.IpldStore) error {
@ -87,6 +85,9 @@ func loadManifests(ctx context.Context, store cbor.IpldStore) error {
} }
func GetActorCodeID(av Version, name string) (cid.Cid, bool) { func GetActorCodeID(av Version, name string) (cid.Cid, bool) {
manifestMx.RLock()
defer manifestMx.RUnlock()
mf, ok := manifests[av] mf, ok := manifests[av]
if ok { if ok {
return mf.Get(name) return mf.Get(name)
@ -96,6 +97,9 @@ func GetActorCodeID(av Version, name string) (cid.Cid, bool) {
} }
func GetActorMetaByCode(c cid.Cid) (string, Version, bool) { func GetActorMetaByCode(c cid.Cid) (string, Version, bool) {
manifestMx.RLock()
defer manifestMx.RUnlock()
entry, ok := actorMeta[c] entry, ok := actorMeta[c]
if !ok { if !ok {
return "", -1, false return "", -1, false