refactor: move local check inside GetCachedStorageLocations to be more transparent
This commit is contained in:
parent
1a4890a6c0
commit
b75c8cd3fe
|
@ -125,8 +125,36 @@ func (s *StorageService) GetCachedStorageLocations(hash *encoding.Multihash, kin
|
||||||
locations[key] = storageLocation
|
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
|
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) {
|
func (s *StorageService) readStorageLocationsFromDB(hash *encoding.Multihash) (storage.StorageLocationMap, error) {
|
||||||
var locationMap storage.StorageLocationMap
|
var locationMap storage.StorageLocationMap
|
||||||
|
|
||||||
|
|
|
@ -62,17 +62,6 @@ func (s *StorageLocationProviderImpl) Start() error {
|
||||||
s.isTimedOut = false
|
s.isTimedOut = false
|
||||||
s.mutex.Unlock()
|
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() {
|
go func() {
|
||||||
requestSent := false
|
requestSent := false
|
||||||
|
|
||||||
|
@ -109,7 +98,7 @@ func (s *StorageLocationProviderImpl) Start() error {
|
||||||
s.logger.Error("Error decoding node id", zap.Error(err))
|
s.logger.Error("Error decoding node id", zap.Error(err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if containsNode(s.excludeNodes, nodeId) {
|
if containsNode(s.excludeNodes, nodeId) && requestSent {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !containsNode(s.availableNodes, nodeId) {
|
if !containsNode(s.availableNodes, nodeId) {
|
||||||
|
@ -226,22 +215,6 @@ func (s *StorageLocationProviderImpl) Downvote(uri storage.SignedStorageLocation
|
||||||
return nil
|
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 {
|
func NewStorageLocationProvider(params StorageLocationProviderParams) *StorageLocationProviderImpl {
|
||||||
if params.LocationTypes == nil {
|
if params.LocationTypes == nil {
|
||||||
params.LocationTypes = []types.StorageLocationType{
|
params.LocationTypes = []types.StorageLocationType{
|
||||||
|
|
Loading…
Reference in New Issue