From ea0b5c361ab5059b2a674452a011b1828def23c8 Mon Sep 17 00:00:00 2001 From: Ragot Geoffrey Date: Wed, 22 Mar 2023 12:25:21 +0100 Subject: [PATCH] feat: fix from review (#168) --- components/ledger/pkg/ledger/cache/cache.go | 15 ++++++-------- components/ledger/pkg/ledger/lock/lock.go | 18 ++++++++--------- components/ledger/pkg/ledger/resolver.go | 22 ++++++++++----------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/components/ledger/pkg/ledger/cache/cache.go b/components/ledger/pkg/ledger/cache/cache.go index 43045a261..535030192 100644 --- a/components/ledger/pkg/ledger/cache/cache.go +++ b/components/ledger/pkg/ledger/cache/cache.go @@ -18,7 +18,7 @@ func (c *Cache) GetAccountWithVolumes(ctx context.Context, address string) (*cor address = strings.TrimPrefix(address, "@") - rawAccount, err := c.cache.Get(c.accountKey(address)) + rawAccount, err := c.cache.Get(address) if err != nil { // TODO: Rename later ? account, err := c.store.ComputeAccount(ctx, address) @@ -26,10 +26,11 @@ func (c *Cache) GetAccountWithVolumes(ctx context.Context, address string) (*cor return nil, err } - if err := c.cache.Set(c.accountKey(account.Address), account); err != nil { + if err := c.cache.Set(account.Address, account); err != nil { panic(err) } + *account = account.Copy() return account, nil } cp := rawAccount.(*core.AccountWithVolumes).Copy() @@ -39,7 +40,7 @@ func (c *Cache) GetAccountWithVolumes(ctx context.Context, address string) (*cor func (c *Cache) Update(accounts core.AccountsAssetsVolumes) { for address, volumes := range accounts { - rawAccount, err := c.cache.Get(c.accountKey(address)) + rawAccount, err := c.cache.Get(address) if err != nil { // Cannot update cache, item maybe evicted continue @@ -47,7 +48,7 @@ func (c *Cache) Update(accounts core.AccountsAssetsVolumes) { account := rawAccount.(*core.AccountWithVolumes) account.Volumes = volumes account.Balances = volumes.Balances() - if err := c.cache.Set(c.accountKey(address), account); err != nil { + if err := c.cache.Set(address, account); err != nil { panic(err) } } @@ -59,14 +60,10 @@ func (c *Cache) UpdateAccountMetadata(ctx context.Context, address string, m cor return err } account.Metadata = account.Metadata.Merge(m) - _ = c.cache.Set(c.accountKey(address), account) + _ = c.cache.Set(address, account) return nil } -func (c *Cache) accountKey(address string) string { - return c.store.Name() + "-" + address -} - func New(store storage.LedgerStore) *Cache { return &Cache{ store: store, diff --git a/components/ledger/pkg/ledger/lock/lock.go b/components/ledger/pkg/ledger/lock/lock.go index eecdce229..1c55df7cd 100644 --- a/components/ledger/pkg/ledger/lock/lock.go +++ b/components/ledger/pkg/ledger/lock/lock.go @@ -29,18 +29,16 @@ func (d *InMemory) Lock(ctx context.Context, ledger string, accounts ...string) d.globalLock.RLock() lock, ok := d.locks[ledger] d.globalLock.RUnlock() - if ok { - goto ret - } - - d.globalLock.Lock() - lock, ok = d.locks[ledger] // Double check, the lock can have been acquired by another go routing between RUnlock and Lock if !ok { - lock = &sync.Mutex{} - d.locks[ledger] = lock + d.globalLock.Lock() + lock, ok = d.locks[ledger] // Double check, the lock can have been acquired by another go routing between RUnlock and Lock + if !ok { + lock = &sync.Mutex{} + d.locks[ledger] = lock + } + d.globalLock.Unlock() } - d.globalLock.Unlock() -ret: + unlocked := false lock.Lock() return func(ctx context.Context) { diff --git a/components/ledger/pkg/ledger/resolver.go b/components/ledger/pkg/ledger/resolver.go index 92406a621..43dcf1f77 100644 --- a/components/ledger/pkg/ledger/resolver.go +++ b/components/ledger/pkg/ledger/resolver.go @@ -49,26 +49,24 @@ func (r *Resolver) GetLedger(ctx context.Context, name string) (*Ledger, error) r.lock.RLock() _, ok := r.initializedStores[name] r.lock.RUnlock() - if ok { - goto ret - } - - r.lock.Lock() - defer r.lock.Unlock() + if !ok { + r.lock.Lock() + defer r.lock.Unlock() - if _, ok = r.initializedStores[name]; !ok { - _, err = store.Initialize(ctx) - if err != nil { - return nil, errors.Wrap(err, "initializing ledger store") + if _, ok = r.initializedStores[name]; !ok { + _, err = store.Initialize(ctx) + if err != nil { + return nil, errors.Wrap(err, "initializing ledger store") + } + r.initializedStores[name] = struct{}{} } - r.initializedStores[name] = struct{}{} } -ret: cache, err := r.cacheManager.ForLedger(ctx, name) if err != nil { return nil, err } + runner, err := r.runnerManager.ForLedger(ctx, name) if err != nil { return nil, err