def _create_session_for_request(self, infohash, tracker_url): # skip no-DHT if tracker_url == u'no-DHT': return # >> Step 1: Try to append the request to an existing session # check there is any existing session that scrapes this torrent request_handled = False for session in self._session_list: if session.tracker_url != tracker_url or session.is_failed: continue if session.has_request(infohash): # a torrent check is already there, ignore this request request_handled = True break if session.can_add_request(): session.add_request(infohash) self._update_pending_response(infohash) request_handled = True break if request_handled: self._logger.debug(u'infohash [%s] appended', hexlify(infohash)) return # >> Step 2: No session to append to, create a new one # create a new session for this request # before creating a new session, check if the tracker is alive if not self._session.lm.tracker_manager.should_check_tracker(tracker_url): self._logger.warn(u"skipping recently failed tracker %s", tracker_url) return session = None try: session = create_tracker_session(tracker_url, self._on_result_from_session) connection_established = session.create_connection() if not connection_established: raise RuntimeError(u"Could not establish connection") session.add_request(infohash) self._session_list.append(session) # update the number of responses this torrent is expecting self._update_pending_response(infohash) self._logger.debug(u"Session created for infohash %s", hexlify(infohash)) except Exception as e: self._logger.info(u"Failed to create session for tracker %s: %s", tracker_url, e) if session: session.cleanup() self._session.lm.tracker_manager.update_tracker_info(tracker_url, False)
def _create_session_for_request(self, tracker_url, timeout=20): session = create_tracker_session(tracker_url, timeout, self.socket_mgr, connection_pool=self.connection_pool) if tracker_url not in self._session_list: self._session_list[tracker_url] = [] self._session_list[tracker_url].append(session) self._logger.debug(u"Session created for tracker %s", tracker_url) return session
def _create_session_for_request(self, infohash, tracker_url): # skip no-DHT if tracker_url == u'no-DHT': return # >> Step 1: Try to append the request to an existing session # check there is any existing session that scrapes this torrent request_handled = False for session in self._session_list: if session.tracker_url != tracker_url or session.is_failed: continue if session.has_request(infohash): # a torrent check is already there, ignore this request request_handled = True break if session.can_add_request(): session.add_request(infohash) self._update_pending_response(infohash) request_handled = True break if request_handled: self._logger.debug(u'infohash [%s] appended', hexlify(infohash)) return # >> Step 2: No session to append to, create a new one # create a new session for this request # before creating a new session, check if the tracker is alive if not self._session.lm.tracker_manager.should_check_tracker( tracker_url): self._logger.warn(u"skipping recently failed tracker %s", tracker_url) return session = None try: session = create_tracker_session(tracker_url, self._on_result_from_session) connection_established = session.create_connection() if not connection_established: raise RuntimeError(u"Could not establish connection") session.add_request(infohash) self._session_list.append(session) # update the number of responses this torrent is expecting self._update_pending_response(infohash) self._logger.debug(u"Session created for infohash %s", hexlify(infohash)) except Exception as e: self._logger.info(u"Failed to create session for tracker %s: %s", tracker_url, e) if session: session.cleanup() self._session.lm.tracker_manager.update_tracker_info( tracker_url, False)