refactor: add a local param to StorageService.GetCachedStorageLocations so we don't spam the local provider store on every poll

This commit is contained in:
Derrick Hammer 2024-03-03 09:28:33 -05:00
parent 8c4ebeccd4
commit 73dc22a71e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 14 additions and 12 deletions

View File

@ -34,7 +34,7 @@ func (m MediatorDefault) SignMessageSimple(message []byte) ([]byte, error) {
}
func (m MediatorDefault) GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType) (map[string]storage.StorageLocation, error) {
return m.Services().Storage().GetCachedStorageLocations(hash, kinds)
return m.Services().Storage().GetCachedStorageLocations(hash, kinds, false)
}
func (m MediatorDefault) SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId, error) {

View File

@ -73,7 +73,7 @@ func (n *StorageService) ProviderStore() storage.ProviderStore {
return n.providerStore
}
func (s *StorageService) GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType) (map[string]storage.StorageLocation, error) {
func (s *StorageService) GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType, local bool) (map[string]storage.StorageLocation, error) {
locations := make(map[string]storage.StorageLocation)
locationMap, err := s.readStorageLocationsFromDB(hash)
@ -81,14 +81,16 @@ func (s *StorageService) GetCachedStorageLocations(hash *encoding.Multihash, kin
return nil, err
}
local := s.getLocalStorageLocation(hash, kinds)
if local != nil {
if local {
localLocation := s.getLocalStorageLocation(hash, kinds)
if localLocation != nil {
nodeIDStr, err := s.Services().P2P().NodeId().ToString()
if err != nil {
return nil, err
}
locations[nodeIDStr] = local
locations[nodeIDStr] = localLocation
}
}
if len(locationMap) == 0 {

View File

@ -10,7 +10,7 @@ import (
type StorageService interface {
SetProviderStore(store storage.ProviderStore)
ProviderStore() storage.ProviderStore
GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType) (map[string]storage.StorageLocation, error)
GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType, local bool) (map[string]storage.StorageLocation, error)
AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.NodeId, location storage.StorageLocation, message []byte) error
DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error)
DownloadBytesByCID(cid *encoding.CID) ([]byte, error)

View File

@ -33,7 +33,7 @@ type StorageLocationProviderImpl struct {
func (s *StorageLocationProviderImpl) Start() error {
var err error
s.uris, err = s.services.Storage().GetCachedStorageLocations(s.hash, s.types)
s.uris, err = s.services.Storage().GetCachedStorageLocations(s.hash, s.types, true)
if err != nil {
return err
}
@ -73,7 +73,7 @@ func (s *StorageLocationProviderImpl) Start() error {
break
}
newUris, err := s.services.Storage().GetCachedStorageLocations(s.hash, s.types)
newUris, err := s.services.Storage().GetCachedStorageLocations(s.hash, s.types, false)
if err != nil {
s.mutex.Unlock()
break