Skip to content

Commit

Permalink
feat: fix from review (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored and flemzord committed May 12, 2023
1 parent 56fe5e4 commit ea0b5c3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
15 changes: 6 additions & 9 deletions components/ledger/pkg/ledger/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ 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)
if err != nil {
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()
Expand All @@ -39,15 +40,15 @@ 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
}
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)
}
}
Expand All @@ -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,
Expand Down
18 changes: 8 additions & 10 deletions components/ledger/pkg/ledger/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 10 additions & 12 deletions components/ledger/pkg/ledger/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ea0b5c3

Please sign in to comment.