Skip to content

Commit

Permalink
Merge pull request #6030 from mysteriumnetwork/smalle-lock-time-for-h…
Browse files Browse the repository at this point in the history
…ermes-call

Do not lock hermes caller while doing slow http requests
  • Loading branch information
soffokl authored Apr 5, 2024
2 parents 41bffb1 + 3fcff51 commit 3a760cd
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions session/pingpong/hermes_caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/cenkalti/backoff/v4"
"github.com/ethereum/go-ethereum/common"

"github.com/mysteriumnetwork/node/identity"
"github.com/mysteriumnetwork/node/requests"
"github.com/mysteriumnetwork/payments/crypto"
Expand Down Expand Up @@ -106,7 +107,7 @@ type HermesCaller struct {
// hermesCallerCache represents the cache for call responses
type hermesCallerCache struct {
data map[string]hermesCallerCacheData
lock sync.Mutex
lock sync.RWMutex
}

// hermesCallerCacheData represents the cache data
Expand Down Expand Up @@ -311,8 +312,6 @@ func (ac *HermesCaller) RefreshLatestProviderPromise(chainID int64, id string, h

// GetConsumerData gets consumer data from hermes, use a negative cacheTime to force update
func (ac *HermesCaller) GetConsumerData(chainID int64, id string, cacheTime time.Duration) (HermesUserInfo, error) {
ac.cache.lock.Lock()
defer ac.cache.lock.Unlock()
if cacheTime > 0 {
cachedResponse, cachedError, ok := ac.getResponseFromCache(chainID, id, cacheTime)
if ok {
Expand All @@ -327,12 +326,8 @@ func (ac *HermesCaller) GetConsumerData(chainID int64, id string, cacheTime time
err = ac.doRequest(req, &resp)
if err != nil {
if errors.Is(err, ErrHermesNotFound) {
//also save not found status
ac.cache.data[getCacheKey(chainID, id)] = hermesCallerCacheData{
updatedAt: time.Now(),
info: nil,
err: err,
}
// also save not found status
ac.setCacheData(chainID, id, nil, err)
}
return HermesUserInfo{}, fmt.Errorf("could not request consumer data from hermes: %w", err)
}
Expand All @@ -347,13 +342,20 @@ func (ac *HermesCaller) GetConsumerData(chainID int64, id string, cacheTime time
return HermesUserInfo{}, fmt.Errorf("could not check promise validity: %w", err)
}

ac.setCacheData(chainID, id, &data, nil)

return data, nil
}

func (ac *HermesCaller) setCacheData(chainID int64, id string, data *HermesUserInfo, err error) {
ac.cache.lock.Lock()
defer ac.cache.lock.Unlock()

ac.cache.data[getCacheKey(chainID, id)] = hermesCallerCacheData{
updatedAt: time.Now(),
info: &data,
err: nil,
info: data,
err: err,
}

return data, nil
}

// GetProviderData gets provider data from hermes
Expand Down Expand Up @@ -431,6 +433,9 @@ func (ac *HermesCaller) doRequest(req *http.Request, to any) error {
}

func (ac *HermesCaller) getResponseFromCache(chainID int64, identity string, cacheDuration time.Duration) (HermesUserInfo, error, bool) {
ac.cache.lock.RLock()
defer ac.cache.lock.RUnlock()

cacheKey := getCacheKey(chainID, identity)
cachedResponse, ok := ac.cache.data[cacheKey]
if ok && cachedResponse.updatedAt.Add(cacheDuration).After(time.Now()) {
Expand Down

0 comments on commit 3a760cd

Please sign in to comment.