Limit active pieces and expose file list

This commit is contained in:
ookami125 2026-06-17 02:23:15 -04:00
parent ff8e0f5f5b
commit 6dc711cf57
6 changed files with 117 additions and 3 deletions

View file

@ -10,6 +10,7 @@
#define ENDGAME_BLOCKS 8 /* switch to endgame when this few remain */
#define ENDGAME_COPIES 2 /* at most two peers race a missing block */
#define PIECE_INFLIGHT_SOFT_CAP 64
#define ACTIVE_PIECE_SOFT_CAP 64
/* per-piece in-progress state, lazily allocated and freed on completion */
typedef struct {
@ -36,6 +37,7 @@ struct naut_download {
naut_bitfield have;
uint32_t *avail; /* [num_pieces] swarm availability count */
pstate **ps; /* [num_pieces] in-progress state or NULL */
uint32_t active_pieces;
uint32_t cur_piece; /* sequential cursor for next_request() */
uint32_t pick_cursor; /* rotating start point for rarest-first ties */
@ -88,6 +90,7 @@ static pstate *ensure_ps(naut_download *d, uint32_t p) {
return NULL;
}
d->ps[p] = s;
d->active_pieces++;
return s;
}
static void free_ps(naut_download *d, uint32_t p) {
@ -95,6 +98,7 @@ static void free_ps(naut_download *d, uint32_t p) {
if (!s) return;
free(s->recv_bits); free(s->req_count); free(s->buf); free(s);
d->ps[p] = NULL;
if (d->active_pieces) d->active_pieces--;
}
naut_download *naut_download_create(const naut_metainfo *mi, naut_storage *st) {
@ -230,13 +234,15 @@ bool naut_download_pick_for_peer(naut_download *d, const naut_bitfield *peer_hav
uint32_t p = (d->pick_cursor + n) % d->num_pieces;
if (naut_bitfield_test(&d->have, p) || !d->ps[p]) continue;
if (p >= peer_have->nbits || !naut_bitfield_test(peer_have, p)) continue;
if (!d->endgame &&
if (!d->endgame && d->active_pieces < ACTIVE_PIECE_SOFT_CAP &&
piece_inflight(d->ps[p]) >= PIECE_INFLIGHT_SOFT_CAP)
continue;
uint32_t b = first_unreq(d->ps[p]);
if (b != UINT32_MAX) return hand_out(d, p, b, index, begin, length);
}
/* pass 2: start the rarest new piece the peer has */
if (!d->endgame && d->active_pieces >= ACTIVE_PIECE_SOFT_CAP)
return false;
uint32_t best = UINT32_MAX, best_av = UINT32_MAX;
for (uint32_t n = 0; n < d->num_pieces; n++) {
uint32_t p = (d->pick_cursor + n) % d->num_pieces;