@@ -839,15 +839,39 @@ void RequestManager::requestFailure(std::shared_ptr<XrdAdaptor::ClientRequest> c
839839 m_disabledSources.insert (source_ptr);
840840
841841 std::unique_lock<std::recursive_mutex> sentry (m_source_mutex);
842+ // Remove the failed source from the container of active sources
842843 if (auto found = std::ranges::find_if (
843- m_activeSources, [&source_ptr](std::shared_ptr<Source> const &src) { return src.get () == source_ptr.get (); });
844+ m_activeSources, [&source_ptr](const std::shared_ptr<Source> &src) { return src.get () == source_ptr.get (); });
844845 found != m_activeSources.end ()) {
845846 auto oldSources = m_activeSources;
846847 m_activeSources.erase (found);
847848 reportSiteChange (oldSources, m_activeSources);
848849 }
850+ // Find a new source to send the request to
851+ // - First, if there is another active source, use it
852+ // - Then, if there are no active sources, if there are inactive
853+ // sources, use the best inactive source
854+ // - Then, if there are no active or inactice sources, try to open a
855+ // new connection for a new source
849856 std::shared_ptr<Source> new_source;
850- if (m_activeSources.empty ()) {
857+ if (not m_activeSources.empty ()) {
858+ new_source = m_activeSources[0 ];
859+ } else if (not m_inactiveSources.empty ()) {
860+ // similar logic as in checkSourcesImpl()
861+ // assume the "sort open delay" doesn't matter in case of a request failure
862+ auto bestInactiveSource =
863+ std::min_element (m_inactiveSources.begin (),
864+ m_inactiveSources.end (),
865+ [](const std::shared_ptr<Source> &s1, const std::shared_ptr<Source> &s2) {
866+ return s1->getQuality () < s2->getQuality ();
867+ });
868+ new_source = *bestInactiveSource;
869+
870+ auto oldSources = m_activeSources;
871+ m_activeSources.push_back (*bestInactiveSource);
872+ m_inactiveSources.erase (bestInactiveSource);
873+ reportSiteChange (oldSources, m_activeSources);
874+ } else {
851875 std::shared_future<std::shared_ptr<Source>> future = m_open_handler->open ();
852876 timespec now;
853877 GET_CLOCK_MONOTONIC (now);
@@ -891,8 +915,6 @@ void RequestManager::requestFailure(std::shared_ptr<XrdAdaptor::ClientRequest> c
891915 auto oldSources = m_activeSources;
892916 m_activeSources.push_back (new_source);
893917 reportSiteChange (oldSources, m_activeSources);
894- } else {
895- new_source = m_activeSources[0 ];
896918 }
897919 new_source->handle (c_ptr);
898920}
0 commit comments