refactor: move local check inside GetCachedStorageLocations to be more transparent

This commit is contained in:
Derrick Hammer 2024-03-02 05:42:12 -05:00
parent 1a4890a6c0
commit b75c8cd3fe
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 29 additions and 28 deletions

View File

@ -125,8 +125,36 @@ func (s *StorageService) GetCachedStorageLocations(hash *encoding.Multihash, kin
locations[key] = storageLocation
}
}
local := s.getLocalStorageLocation(hash, kinds)
if local != nil {
nodeIDStr, err := s.Services().P2P().NodeId().ToString()
if err != nil {
return nil, err
}
locations[nodeIDStr] = local
}
return locations, nil
}
func (s *StorageService) getLocalStorageLocation(hash *encoding.Multihash, kinds []types.StorageLocationType) storage.StorageLocation {
if s.ProviderStore() != nil {
if s.ProviderStore().CanProvide(hash, kinds) {
location, _ := s.ProviderStore().Provide(hash, kinds)
message := storage.PrepareProvideMessage(s.Services().P2P().Config().KeyPair, hash, location)
location.SetProviderMessage(message)
return location
}
}
return nil
}
func (s *StorageService) readStorageLocationsFromDB(hash *encoding.Multihash) (storage.StorageLocationMap, error) {
var locationMap storage.StorageLocationMap

View File

@ -62,17 +62,6 @@ func (s *StorageLocationProviderImpl) Start() error {
s.isTimedOut = false
s.mutex.Unlock()
local := s.checkLocal()
if local != nil {
s.availableNodes = append([]*encoding.NodeId{local.NodeId()}, s.availableNodes...)
nodeIdStr, err := local.NodeId().ToString()
if err != nil {
s.logger.Error("Error decoding node id", zap.Error(err))
return err
}
s.uris[nodeIdStr] = local.Location()
}
go func() {
requestSent := false
@ -109,7 +98,7 @@ func (s *StorageLocationProviderImpl) Start() error {
s.logger.Error("Error decoding node id", zap.Error(err))
continue
}
if containsNode(s.excludeNodes, nodeId) {
if containsNode(s.excludeNodes, nodeId) && requestSent {
continue
}
if !containsNode(s.availableNodes, nodeId) {
@ -226,22 +215,6 @@ func (s *StorageLocationProviderImpl) Downvote(uri storage.SignedStorageLocation
return nil
}
func (s *StorageLocationProviderImpl) checkLocal() storage.SignedStorageLocation {
if s.services.Storage().ProviderStore() != nil {
if s.services.Storage().ProviderStore().CanProvide(s.hash, s.types) {
location, _ := s.services.Storage().ProviderStore().Provide(s.hash, s.types)
message := storage.PrepareProvideMessage(s.services.P2P().Config().KeyPair, s.hash, location)
location.SetProviderMessage(message)
return storage.NewSignedStorageLocation(s.services.P2P().NodeId(), location)
}
}
return nil
}
func NewStorageLocationProvider(params StorageLocationProviderParams) *StorageLocationProviderImpl {
if params.LocationTypes == nil {
params.LocationTypes = []types.StorageLocationType{