Skip to content

Commit

Permalink
Merge pull request #6031 from mysteriumnetwork/fix-concurrent-map-access
Browse files Browse the repository at this point in the history
Fix concurrent map read and map write
  • Loading branch information
soffokl authored Apr 5, 2024
2 parents 3a760cd + 4b93489 commit b177e8a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions session/pingpong/consumer_totals_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import (
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog/log"

"github.com/mysteriumnetwork/node/eventbus"
"github.com/mysteriumnetwork/node/identity"
"github.com/mysteriumnetwork/node/session/pingpong/event"
"github.com/rs/zerolog/log"
)

// ConsumerTotalsStorage allows to store total promised amounts for each channel.
type ConsumerTotalsStorage struct {
createLock sync.Mutex
createLock sync.RWMutex
bus eventbus.Publisher
data map[string]*ConsumerTotalElement
}
Expand All @@ -52,17 +53,18 @@ func NewConsumerTotalsStorage(bus eventbus.Publisher) *ConsumerTotalsStorage {

// Store stores the given amount as promised for the given channel.
func (cts *ConsumerTotalsStorage) Store(chainID int64, id identity.Identity, hermesID common.Address, amount *big.Int) error {
cts.createLock.Lock()
defer cts.createLock.Unlock()

key := cts.makeKey(chainID, id, hermesID)
_, ok := cts.data[key]
if !ok {
cts.createLock.Lock()
_, ok := cts.data[key]
if !ok {
cts.data[key] = &ConsumerTotalElement{
amount: nil,
}
}
cts.createLock.Unlock()
}
element, ok := cts.data[key]
if !ok {
Expand Down Expand Up @@ -94,8 +96,8 @@ func (cts *ConsumerTotalsStorage) Store(chainID int64, id identity.Identity, her
// Get fetches the amount as promised for the given channel.
func (cts *ConsumerTotalsStorage) Get(chainID int64, id identity.Identity, hermesID common.Address) (*big.Int, error) {
key := cts.makeKey(chainID, id, hermesID)
cts.createLock.Lock()
defer cts.createLock.Unlock()
cts.createLock.RLock()
defer cts.createLock.RUnlock()

element, ok := cts.data[key]
if !ok {
Expand All @@ -112,17 +114,18 @@ func (cts *ConsumerTotalsStorage) Get(chainID int64, id identity.Identity, herme

// Add adds the given amount as promised for the given channel.
func (cts *ConsumerTotalsStorage) Add(chainID int64, id identity.Identity, hermesID common.Address, amount *big.Int) error {
cts.createLock.Lock()
defer cts.createLock.Unlock()

key := cts.makeKey(chainID, id, hermesID)
_, ok := cts.data[key]
if !ok {
cts.createLock.Lock()
_, ok := cts.data[key]
if !ok {
cts.data[key] = &ConsumerTotalElement{
amount: nil,
}
}
cts.createLock.Unlock()
}
element, ok := cts.data[key]
if !ok {
Expand Down

0 comments on commit b177e8a

Please sign in to comment.