Skip to content

Commit 4257173

Browse files
committed
add high_priority flag to tracker announcements, to put them at the front of the queue. This can be done manually in force_reannounce() or will happen automatically as part of connection-boost
1 parent 195f94d commit 4257173

File tree

7 files changed

+41
-9
lines changed

7 files changed

+41
-9
lines changed

bindings/python/src/torrent_handle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ void bind_torrent_handle()
596596
;
597597

598598
s.attr("ignore_min_interval") = torrent_handle::ignore_min_interval;
599+
s.attr("high_priority") = torrent_handle::high_priority;
599600
s.attr("overwrite_existing") = torrent_handle::overwrite_existing;
600601
s.attr("piece_granularity") = torrent_handle::piece_granularity;
601602
s.attr("graceful_pause") = torrent_handle::graceful_pause;

include/libtorrent/torrent.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ namespace libtorrent {
828828
// forcefully sets next_announce to the current time
829829
void force_tracker_request(time_point, int tracker_idx, reannounce_flags_t flags);
830830
void scrape_tracker(int idx, bool user_triggered);
831-
void announce_with_tracker(event_t = event_t::none);
831+
void announce_with_tracker(event_t = event_t::none, bool high_priority = false);
832832

833833
#ifndef TORRENT_DISABLE_DHT
834834
void dht_announce();

include/libtorrent/torrent_handle.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,11 @@ namespace aux {
11661166
// and the tracker is announced immediately.
11671167
static constexpr reannounce_flags_t ignore_min_interval = 0_bit;
11681168

1169+
// by default, force-reannounce will queue the announce normally.
1170+
// If this flag is set, the announce will be put at the front of the
1171+
// tracker queue for immediate processing.
1172+
static constexpr reannounce_flags_t high_priority = 1_bit;
1173+
11691174
// ``force_reannounce()`` will force this torrent to do another tracker
11701175
// request, to receive new peers. The ``seconds`` argument specifies how
11711176
// many seconds from now to issue the tracker announces.
@@ -1179,7 +1184,7 @@ namespace aux {
11791184
// If set to -1 (which is the default), all trackers are re-announce.
11801185
//
11811186
// The ``flags`` argument can be used to affect the re-announce. See
1182-
// ignore_min_interval.
1187+
// ignore_min_interval and high_priority.
11831188
//
11841189
// ``force_dht_announce`` will announce the torrent to the DHT
11851190
// immediately.

include/libtorrent/tracker_manager.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ enum class event_t : std::uint8_t
108108
// see parse_tracker_response()
109109
static constexpr tracker_request_flags_t i2p = 1_bit;
110110

111+
// If set, this announce should be prioritized in the tracker queue.
112+
// This does not bypass concurrency caps; it only moves the request to
113+
// the front of the pending queue if we are at capacity.
114+
static constexpr tracker_request_flags_t high_priority = 2_bit;
115+
111116
std::string url;
112117
std::string trackerid;
113118
#if TORRENT_ABI_VERSION == 1

src/torrent.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,7 +2976,7 @@ namespace {
29762976
};
29772977
}
29782978

2979-
void torrent::announce_with_tracker(event_t e)
2979+
void torrent::announce_with_tracker(event_t e, bool const high_priority)
29802980
{
29812981
TORRENT_ASSERT(is_single_thread());
29822982
TORRENT_ASSERT(e == event_t::stopped || state() != torrent_status::checking_files);
@@ -3278,6 +3278,7 @@ namespace {
32783278
, print_endpoint(aep.local_endpoint).c_str());
32793279
}
32803280

3281+
if (high_priority) req.kind |= tracker_request::high_priority;
32813282
// if we're not logging session logs, don't bother creating an
32823283
// observer object just for logging
32833284
if (m_abort && m_ses.should_log())
@@ -3842,7 +3843,15 @@ namespace {
38423843
debug_log("*** found no tracker endpoints to announce");
38433844
}
38443845
#endif
3845-
update_tracker_timer(aux::time_now32());
3846+
3847+
if (flags & torrent_handle::high_priority)
3848+
{
3849+
announce_with_tracker(event_t::none, true);
3850+
}
3851+
else
3852+
{
3853+
update_tracker_timer(aux::time_now32());
3854+
}
38463855
}
38473856

38483857
#if TORRENT_ABI_VERSION == 1
@@ -10129,7 +10138,8 @@ namespace {
1012910138

1013010139
update_want_tick();
1013110140

10132-
announce_with_tracker();
10141+
bool const high_priority = m_connect_boost_counter > 0;
10142+
announce_with_tracker(event_t::none, high_priority);
1013310143

1013410144
lsd_announce();
1013510145
}
@@ -12350,7 +12360,8 @@ namespace {
1235012360
if ((!m_abort && !is_paused() && state() != torrent_status::checking_files)
1235112361
|| r.event == event_t::stopped)
1235212362
{
12353-
announce_with_tracker(r.event);
12363+
// if the tracker that failed was high-priority, make the next attempt high priority as well
12364+
announce_with_tracker(r.event, (r.kind & torrent_handle::high_priority) != 0);
1235412365
}
1235512366
else
1235612367
{

src/torrent_handle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ namespace libtorrent {
8181
constexpr pause_flags_t torrent_handle::clear_disk_cache;
8282
constexpr deadline_flags_t torrent_handle::alert_when_available;
8383
constexpr reannounce_flags_t torrent_handle::ignore_min_interval;
84+
constexpr reannounce_flags_t torrent_handle::high_priority;
8485
constexpr file_progress_flags_t torrent_handle::piece_granularity;
8586

8687
constexpr status_flags_t torrent_handle::query_distributed_copies;

src/tracker_manager.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace libtorrent {
5151

5252
constexpr tracker_request_flags_t tracker_request::scrape_request;
5353
constexpr tracker_request_flags_t tracker_request::i2p;
54+
constexpr tracker_request_flags_t tracker_request::high_priority;
5455

5556
timeout_handler::timeout_handler(io_context& ios)
5657
: m_start_time(clock_type::now())
@@ -275,11 +276,12 @@ constexpr tracker_request_flags_t tracker_request::i2p;
275276
TORRENT_ASSERT(req.num_want >= 0);
276277
TORRENT_ASSERT(!m_abort || req.event == event_t::stopped);
277278
if (m_abort && req.event != event_t::stopped) return;
279+
bool const high_priority = bool(req.kind & tracker_request::high_priority);
278280

279281
#ifndef TORRENT_DISABLE_LOGGING
280282
std::shared_ptr<request_callback> cb = c.lock();
281-
if (cb) cb->debug_log("*** QUEUE_TRACKER_REQUEST [ listen_port: %d ]"
282-
, req.listen_port);
283+
if (cb) cb->debug_log("*** QUEUE_TRACKER_REQUEST [ listen_port: %d priority: %d ]"
284+
, req.listen_port, high_priority);
283285
#endif
284286

285287
std::string const protocol = req.url.substr(0, req.url.find(':'));
@@ -298,7 +300,14 @@ constexpr tracker_request_flags_t tracker_request::i2p;
298300
}
299301
else
300302
{
301-
m_queued.push_back(std::move(con));
303+
if (high_priority)
304+
{
305+
m_queued.push_front(std::move(con));
306+
}
307+
else
308+
{
309+
m_queued.push_back(std::move(con));
310+
}
302311
m_stats_counters.set_value(counters::num_queued_tracker_announces, std::int64_t(m_queued.size()));
303312
}
304313
return;

0 commit comments

Comments
 (0)